001
014
015 package com.liferay.portal.service.impl;
016
017 import com.liferay.portal.AccountNameException;
018 import com.liferay.portal.CompanyMxException;
019 import com.liferay.portal.CompanyVirtualHostException;
020 import com.liferay.portal.CompanyWebIdException;
021 import com.liferay.portal.NoSuchCompanyException;
022 import com.liferay.portal.NoSuchLayoutSetException;
023 import com.liferay.portal.NoSuchShardException;
024 import com.liferay.portal.NoSuchUserException;
025 import com.liferay.portal.kernel.exception.PortalException;
026 import com.liferay.portal.kernel.exception.SystemException;
027 import com.liferay.portal.kernel.language.LanguageUtil;
028 import com.liferay.portal.kernel.search.BooleanClauseOccur;
029 import com.liferay.portal.kernel.search.BooleanQuery;
030 import com.liferay.portal.kernel.search.BooleanQueryFactoryUtil;
031 import com.liferay.portal.kernel.search.Field;
032 import com.liferay.portal.kernel.search.Hits;
033 import com.liferay.portal.kernel.search.SearchEngineUtil;
034 import com.liferay.portal.kernel.util.LocaleUtil;
035 import com.liferay.portal.kernel.util.PropsKeys;
036 import com.liferay.portal.kernel.util.StringPool;
037 import com.liferay.portal.kernel.util.TimeZoneUtil;
038 import com.liferay.portal.kernel.util.UnicodeProperties;
039 import com.liferay.portal.kernel.util.Validator;
040 import com.liferay.portal.model.Account;
041 import com.liferay.portal.model.Company;
042 import com.liferay.portal.model.CompanyConstants;
043 import com.liferay.portal.model.Contact;
044 import com.liferay.portal.model.ContactConstants;
045 import com.liferay.portal.model.Group;
046 import com.liferay.portal.model.GroupConstants;
047 import com.liferay.portal.model.Role;
048 import com.liferay.portal.model.RoleConstants;
049 import com.liferay.portal.model.User;
050 import com.liferay.portal.service.ServiceContext;
051 import com.liferay.portal.service.base.CompanyLocalServiceBaseImpl;
052 import com.liferay.portal.util.Portal;
053 import com.liferay.portal.util.PrefsPropsUtil;
054 import com.liferay.portal.util.PropsValues;
055 import com.liferay.util.Encryptor;
056 import com.liferay.util.EncryptorException;
057
058 import java.io.File;
059 import java.io.IOException;
060 import java.io.InputStream;
061
062 import java.util.Calendar;
063 import java.util.Date;
064 import java.util.List;
065 import java.util.Locale;
066 import java.util.Map;
067
068 import javax.portlet.PortletException;
069 import javax.portlet.PortletPreferences;
070
071
075 public class CompanyLocalServiceImpl extends CompanyLocalServiceBaseImpl {
076
077 public Company addCompany(
078 String webId, String virtualHost, String mx, String shardName,
079 boolean system, int maxUsers)
080 throws PortalException, SystemException {
081
082
083
084 virtualHost = virtualHost.trim().toLowerCase();
085
086 if ((Validator.isNull(webId)) ||
087 (webId.equals(PropsValues.COMPANY_DEFAULT_WEB_ID)) ||
088 (companyPersistence.fetchByWebId(webId) != null)) {
089
090 throw new CompanyWebIdException();
091 }
092
093 validate(webId, virtualHost, mx);
094
095 Company company = checkCompany(webId, mx, shardName);
096
097 company.setVirtualHost(virtualHost);
098 company.setMx(mx);
099 company.setSystem(system);
100 company.setMaxUsers(maxUsers);
101
102 companyPersistence.update(company, false);
103
104 return company;
105 }
106
107 public Company checkCompany(String webId)
108 throws PortalException, SystemException {
109
110 String mx = webId;
111
112 return companyLocalService.checkCompany(
113 webId, mx, PropsValues.SHARD_DEFAULT_NAME);
114 }
115
116 public Company checkCompany(String webId, String mx, String shardName)
117 throws PortalException, SystemException {
118
119
120
121 Date now = new Date();
122
123 Company company = companyPersistence.fetchByWebId(webId);
124
125 if (company == null) {
126 String virtualHost = webId;
127
128 if (webId.equals(PropsValues.COMPANY_DEFAULT_WEB_ID)) {
129 virtualHost = _DEFAULT_VIRTUAL_HOST;
130 }
131
132 String homeURL = null;
133 String name = webId;
134 String legalName = null;
135 String legalId = null;
136 String legalType = null;
137 String sicCode = null;
138 String tickerSymbol = null;
139 String industry = null;
140 String type = null;
141 String size = null;
142
143 long companyId = counterLocalService.increment();
144
145 company = companyPersistence.create(companyId);
146
147 try {
148 company.setKeyObj(Encryptor.generateKey());
149 }
150 catch (EncryptorException ee) {
151 throw new SystemException(ee);
152 }
153
154 company.setWebId(webId);
155 company.setVirtualHost(virtualHost);
156 company.setMx(mx);
157
158 companyPersistence.update(company, false);
159
160
161
162 shardLocalService.addShard(
163 Company.class.getName(), companyId, shardName);
164
165
166
167 updateCompany(
168 companyId, virtualHost, mx, homeURL, name, legalName, legalId,
169 legalType, sicCode, tickerSymbol, industry, type, size);
170
171
172
173 if (webId.equals("liferay.net")) {
174 company = companyPersistence.findByWebId(webId);
175
176 company.setVirtualHost("demo.liferay.net");
177
178 companyPersistence.update(company, false);
179
180 updateSecurity(
181 companyId, CompanyConstants.AUTH_TYPE_EA, true, true, true,
182 true, false, true);
183
184 PortletPreferences preferences = PrefsPropsUtil.getPreferences(
185 companyId);
186
187 try {
188 preferences.setValue(
189 PropsKeys.ADMIN_EMAIL_FROM_NAME, "Liferay Demo");
190 preferences.setValue(
191 PropsKeys.ADMIN_EMAIL_FROM_ADDRESS, "test@liferay.net");
192
193 preferences.store();
194 }
195 catch (IOException ioe) {
196 throw new SystemException(ioe);
197 }
198 catch (PortletException pe) {
199 throw new SystemException(pe);
200 }
201 }
202 }
203 else {
204 try {
205 shardLocalService.getShard(
206 Company.class.getName(), company.getCompanyId());
207 }
208 catch (NoSuchShardException nsse) {
209 shardLocalService.addShard(
210 Company.class.getName(), company.getCompanyId(), shardName);
211 }
212 }
213
214 long companyId = company.getCompanyId();
215
216
217
218 checkCompanyKey(companyId);
219
220
221
222 User defaultUser = null;
223
224 try {
225 defaultUser = userLocalService.getDefaultUser(companyId);
226
227 if (!defaultUser.isAgreedToTermsOfUse()) {
228 defaultUser.setAgreedToTermsOfUse(true);
229
230 userPersistence.update(defaultUser, false);
231 }
232 }
233 catch (NoSuchUserException nsue) {
234 long userId = counterLocalService.increment();
235
236 defaultUser = userPersistence.create(userId);
237
238 defaultUser.setCompanyId(companyId);
239 defaultUser.setCreateDate(now);
240 defaultUser.setModifiedDate(now);
241 defaultUser.setDefaultUser(true);
242 defaultUser.setContactId(counterLocalService.increment());
243 defaultUser.setPassword("password");
244 defaultUser.setScreenName(String.valueOf(defaultUser.getUserId()));
245 defaultUser.setEmailAddress("default@" + company.getMx());
246 defaultUser.setLanguageId(LocaleUtil.getDefault().toString());
247 defaultUser.setTimeZoneId(TimeZoneUtil.getDefault().getID());
248 defaultUser.setGreeting(
249 LanguageUtil.format(
250 defaultUser.getLocale(), "welcome-x", StringPool.BLANK,
251 false));
252 defaultUser.setLoginDate(now);
253 defaultUser.setFailedLoginAttempts(0);
254 defaultUser.setAgreedToTermsOfUse(true);
255 defaultUser.setActive(true);
256
257 userPersistence.update(defaultUser, false);
258
259
260
261 Contact defaultContact = contactPersistence.create(
262 defaultUser.getContactId());
263
264 defaultContact.setCompanyId(defaultUser.getCompanyId());
265 defaultContact.setUserId(defaultUser.getUserId());
266 defaultContact.setUserName(StringPool.BLANK);
267 defaultContact.setCreateDate(now);
268 defaultContact.setModifiedDate(now);
269 defaultContact.setAccountId(company.getAccountId());
270 defaultContact.setParentContactId(
271 ContactConstants.DEFAULT_PARENT_CONTACT_ID);
272 defaultContact.setFirstName(StringPool.BLANK);
273 defaultContact.setMiddleName(StringPool.BLANK);
274 defaultContact.setLastName(StringPool.BLANK);
275 defaultContact.setMale(true);
276 defaultContact.setBirthday(now);
277
278 contactPersistence.update(defaultContact, false);
279 }
280
281
282
283 roleLocalService.checkSystemRoles(companyId);
284
285
286
287 groupLocalService.checkSystemGroups(companyId);
288
289
290
291 groupLocalService.checkCompanyGroup(companyId);
292
293
294
295 passwordPolicyLocalService.checkDefaultPasswordPolicy(companyId);
296
297
298
299 Role guestRole = roleLocalService.getRole(
300 companyId, RoleConstants.GUEST);
301
302 roleLocalService.setUserRoles(
303 defaultUser.getUserId(), new long[] {guestRole.getRoleId()});
304
305
306
307 if (userPersistence.countByCompanyId(companyId) == 1) {
308 long creatorUserId = 0;
309 boolean autoPassword = false;
310 String password1 = PropsValues.DEFAULT_ADMIN_PASSWORD;
311 String password2 = password1;
312 boolean autoScreenName = false;
313 String screenName = PropsValues.DEFAULT_ADMIN_SCREEN_NAME;
314 String emailAddress =
315 PropsValues.DEFAULT_ADMIN_EMAIL_ADDRESS_PREFIX + "@" + mx;
316 long facebookId = 0;
317 String openId = StringPool.BLANK;
318 Locale locale = defaultUser.getLocale();
319 String firstName = PropsValues.DEFAULT_ADMIN_FIRST_NAME;
320 String middleName = PropsValues.DEFAULT_ADMIN_MIDDLE_NAME;
321 String lastName = PropsValues.DEFAULT_ADMIN_LAST_NAME;
322 int prefixId = 0;
323 int suffixId = 0;
324 boolean male = true;
325 int birthdayMonth = Calendar.JANUARY;
326 int birthdayDay = 1;
327 int birthdayYear = 1970;
328 String jobTitle = StringPool.BLANK;
329
330 Group guestGroup = groupLocalService.getGroup(
331 companyId, GroupConstants.GUEST);
332
333 long[] groupIds = new long[] {guestGroup.getGroupId()};
334
335 long[] organizationIds = null;
336
337 Role adminRole = roleLocalService.getRole(
338 companyId, RoleConstants.ADMINISTRATOR);
339
340 Role powerUserRole = roleLocalService.getRole(
341 companyId, RoleConstants.POWER_USER);
342
343 long[] roleIds = new long[] {
344 adminRole.getRoleId(), powerUserRole.getRoleId()
345 };
346
347 long[] userGroupIds = null;
348 boolean sendEmail = false;
349 ServiceContext serviceContext = new ServiceContext();
350
351 userLocalService.addUser(
352 creatorUserId, companyId, autoPassword, password1, password2,
353 autoScreenName, screenName, emailAddress, facebookId, openId,
354 locale, firstName, middleName, lastName, prefixId, suffixId,
355 male, birthdayMonth, birthdayDay, birthdayYear, jobTitle,
356 groupIds, organizationIds, roleIds, userGroupIds, sendEmail,
357 serviceContext);
358 }
359
360
361
362 portletLocalService.checkPortlets(companyId);
363
364 return company;
365 }
366
367 public void checkCompanyKey(long companyId)
368 throws PortalException, SystemException {
369
370 Company company = companyPersistence.findByPrimaryKey(companyId);
371
372 if ((Validator.isNull(company.getKey())) &&
373 (company.getKeyObj() == null)) {
374
375 try {
376 company.setKeyObj(Encryptor.generateKey());
377 }
378 catch (EncryptorException ee) {
379 throw new SystemException(ee);
380 }
381
382 companyPersistence.update(company, false);
383 }
384 }
385
386 public void deleteLogo(long companyId)
387 throws PortalException, SystemException {
388
389 Company company = companyPersistence.findByPrimaryKey(companyId);
390
391 long logoId = company.getLogoId();
392
393 if (logoId > 0) {
394 company.setLogoId(0);
395
396 companyPersistence.update(company, false);
397
398 imageLocalService.deleteImage(logoId);
399 }
400 }
401
402 public List<Company> getCompanies() throws SystemException {
403 return companyPersistence.findAll();
404 }
405
406 public List<Company> getCompanies(boolean system) throws SystemException {
407 return companyPersistence.findBySystem(system);
408 }
409
410 public int getCompaniesCount(boolean system) throws SystemException {
411 return companyPersistence.countBySystem(system);
412 }
413
414 public Company getCompanyById(long companyId)
415 throws PortalException, SystemException {
416
417 return companyPersistence.findByPrimaryKey(companyId);
418 }
419
420 public Company getCompanyByLogoId(long logoId)
421 throws PortalException, SystemException {
422
423 return companyPersistence.findByLogoId(logoId);
424 }
425
426 public Company getCompanyByMx(String mx)
427 throws PortalException, SystemException {
428
429 return companyPersistence.findByMx(mx);
430 }
431
432 public Company getCompanyByVirtualHost(String virtualHost)
433 throws PortalException, SystemException {
434
435 virtualHost = virtualHost.trim().toLowerCase();
436
437 return companyPersistence.findByVirtualHost(virtualHost);
438 }
439
440 public Company getCompanyByWebId(String webId)
441 throws PortalException, SystemException {
442
443 return companyPersistence.findByWebId(webId);
444 }
445
446 public void removePreferences(long companyId, String[] keys)
447 throws SystemException {
448
449 PortletPreferences preferences = PrefsPropsUtil.getPreferences(
450 companyId);
451
452 try {
453 for (String key : keys) {
454 preferences.reset(key);
455 }
456
457 preferences.store();
458 }
459 catch (Exception e) {
460 throw new SystemException(e);
461 }
462 }
463
464 public Hits search(
465 long companyId, long userId, String keywords, int start, int end)
466 throws SystemException {
467
468 return search(companyId, userId, null, 0, null, keywords, start, end);
469 }
470
471 public Hits search(
472 long companyId, long userId, String portletId, long groupId,
473 String type, String keywords, int start, int end)
474 throws SystemException {
475
476 try {
477 BooleanQuery contextQuery = BooleanQueryFactoryUtil.create();
478
479 contextQuery.addRequiredTerm(Field.COMPANY_ID, companyId);
480
481 if (Validator.isNotNull(portletId)) {
482 contextQuery.addRequiredTerm(Field.PORTLET_ID, portletId);
483 }
484
485 if (groupId > 0) {
486 contextQuery.addRequiredTerm(Field.GROUP_ID, groupId);
487 }
488
489 if (Validator.isNotNull(type)) {
490 contextQuery.addRequiredTerm(Field.TYPE, type);
491 }
492
493 BooleanQuery searchQuery = BooleanQueryFactoryUtil.create();
494
495 searchQuery.addTerms(_KEYWORDS_FIELDS, keywords);
496
497 BooleanQuery fullQuery = BooleanQueryFactoryUtil.create();
498
499 fullQuery.add(contextQuery, BooleanClauseOccur.MUST);
500
501 if (searchQuery.clauses().size() > 0) {
502 fullQuery.add(searchQuery, BooleanClauseOccur.MUST);
503 }
504
505 return SearchEngineUtil.search(
506 companyId, new long[] {groupId}, userId, null, fullQuery, start,
507 end);
508 }
509 catch (Exception e) {
510 throw new SystemException(e);
511 }
512 }
513
514 public Company updateCompany(
515 long companyId, String virtualHost, String mx, int maxUsers)
516 throws PortalException, SystemException {
517
518 virtualHost = virtualHost.trim().toLowerCase();
519
520 Company company = companyPersistence.findByPrimaryKey(companyId);
521
522 validate(company.getWebId(), virtualHost, mx);
523
524 company.setVirtualHost(virtualHost);
525
526 if (PropsValues.MAIL_MX_UPDATE) {
527 company.setMx(mx);
528 }
529
530 company.setMaxUsers(maxUsers);
531
532 companyPersistence.update(company, false);
533
534 return company;
535 }
536
537 public Company updateCompany(
538 long companyId, String virtualHost, String mx, String homeURL,
539 String name, String legalName, String legalId, String legalType,
540 String sicCode, String tickerSymbol, String industry, String type,
541 String size)
542 throws PortalException, SystemException {
543
544
545
546 virtualHost = virtualHost.trim().toLowerCase();
547 Date now = new Date();
548
549 Company company = companyPersistence.findByPrimaryKey(companyId);
550
551 validate(company.getWebId(), virtualHost, mx);
552 validate(name);
553
554 company.setVirtualHost(virtualHost);
555
556 if (PropsValues.MAIL_MX_UPDATE) {
557 company.setMx(mx);
558 }
559
560 company.setHomeURL(homeURL);
561
562 companyPersistence.update(company, false);
563
564
565
566 Account account = accountPersistence.fetchByPrimaryKey(
567 company.getAccountId());
568
569 if (account == null) {
570 long accountId = counterLocalService.increment();
571
572 account = accountPersistence.create(accountId);
573
574 account.setCreateDate(now);
575 account.setCompanyId(companyId);
576 account.setUserId(0);
577 account.setUserName(StringPool.BLANK);
578
579 company.setAccountId(accountId);
580
581 companyPersistence.update(company, false);
582 }
583
584 account.setModifiedDate(now);
585 account.setName(name);
586 account.setLegalName(legalName);
587 account.setLegalId(legalId);
588 account.setLegalType(legalType);
589 account.setSicCode(sicCode);
590 account.setTickerSymbol(tickerSymbol);
591 account.setIndustry(industry);
592 account.setType(type);
593 account.setSize(size);
594
595 accountPersistence.update(account, false);
596
597 return company;
598 }
599
600 public void updateDisplay(
601 long companyId, String languageId, String timeZoneId)
602 throws PortalException, SystemException {
603
604 User user = userLocalService.getDefaultUser(companyId);
605
606 user.setLanguageId(languageId);
607 user.setTimeZoneId(timeZoneId);
608
609 userPersistence.update(user, false);
610 }
611
612 public void updateLogo(long companyId, byte[] bytes)
613 throws PortalException, SystemException {
614
615 long logoId = getLogoId(companyId);
616
617 imageLocalService.updateImage(logoId, bytes);
618 }
619
620 public void updateLogo(long companyId, File file)
621 throws PortalException, SystemException {
622
623 long logoId = getLogoId(companyId);
624
625 imageLocalService.updateImage(logoId, file);
626 }
627
628 public void updateLogo(long companyId, InputStream is)
629 throws PortalException, SystemException {
630
631 long logoId = getLogoId(companyId);
632
633 imageLocalService.updateImage(logoId, is);
634 }
635
636 public void updatePreferences(long companyId, UnicodeProperties properties)
637 throws SystemException {
638
639 PortletPreferences preferences = PrefsPropsUtil.getPreferences(
640 companyId);
641
642 try {
643 if (properties.containsKey(PropsKeys.LOCALES)) {
644 String oldLocales = preferences.getValue(
645 PropsKeys.LOCALES, StringPool.BLANK);
646 String newLocales = properties.getProperty(PropsKeys.LOCALES);
647
648 if (!Validator.equals(oldLocales, newLocales)) {
649 LanguageUtil.resetAvailableLocales(companyId);
650 }
651 }
652
653 for (Map.Entry<String, String> entry : properties.entrySet()) {
654 String key = entry.getKey();
655 String value = entry.getValue();
656
657 if (!value.equals(Portal.TEMP_OBFUSCATION_VALUE)) {
658 preferences.setValue(key, value);
659 }
660 }
661
662 preferences.store();
663 }
664 catch (Exception e) {
665 throw new SystemException(e);
666 }
667 }
668
669 public void updateSecurity(
670 long companyId, String authType, boolean autoLogin,
671 boolean sendPassword, boolean strangers, boolean strangersWithMx,
672 boolean strangersVerify, boolean communityLogo)
673 throws SystemException {
674
675 PortletPreferences preferences = PrefsPropsUtil.getPreferences(
676 companyId);
677
678 try {
679 preferences.setValue(
680 PropsKeys.COMPANY_SECURITY_AUTH_TYPE, authType);
681 preferences.setValue(
682 PropsKeys.COMPANY_SECURITY_AUTO_LOGIN,
683 String.valueOf(autoLogin));
684 preferences.setValue(
685 PropsKeys.COMPANY_SECURITY_SEND_PASSWORD,
686 String.valueOf(sendPassword));
687 preferences.setValue(
688 PropsKeys.COMPANY_SECURITY_STRANGERS,
689 String.valueOf(strangers));
690 preferences.setValue(
691 PropsKeys.COMPANY_SECURITY_STRANGERS_WITH_MX,
692 String.valueOf(strangersWithMx));
693 preferences.setValue(
694 PropsKeys.COMPANY_SECURITY_STRANGERS_VERIFY,
695 String.valueOf(strangersVerify));
696 preferences.setValue(
697 PropsKeys.COMPANY_SECURITY_COMMUNITY_LOGO,
698 String.valueOf(communityLogo));
699
700 preferences.store();
701 }
702 catch (IOException ioe) {
703 throw new SystemException(ioe);
704 }
705 catch (PortletException pe) {
706 throw new SystemException(pe);
707 }
708 }
709
710 protected long getLogoId(long companyId)
711 throws PortalException, SystemException {
712
713 Company company = companyPersistence.findByPrimaryKey(companyId);
714
715 long logoId = company.getLogoId();
716
717 if (logoId <= 0) {
718 logoId = counterLocalService.increment();
719
720 company.setLogoId(logoId);
721
722 companyPersistence.update(company, false);
723 }
724
725 return logoId;
726 }
727
728 protected void validate(String name) throws PortalException {
729 if (Validator.isNull(name)) {
730 throw new AccountNameException();
731 }
732 }
733
734 protected void validate(String webId, String virtualHost, String mx)
735 throws PortalException, SystemException {
736
737 if (Validator.isNull(virtualHost)) {
738 throw new CompanyVirtualHostException();
739 }
740 else if (virtualHost.equals(_DEFAULT_VIRTUAL_HOST) &&
741 !webId.equals(PropsValues.COMPANY_DEFAULT_WEB_ID)) {
742
743 throw new CompanyVirtualHostException();
744 }
745 else if (!Validator.isDomain(virtualHost)) {
746 throw new CompanyVirtualHostException();
747 }
748 else {
749 try {
750 Company virtualHostCompany = getCompanyByVirtualHost(
751 virtualHost);
752
753 if ((virtualHostCompany != null) &&
754 (!virtualHostCompany.getWebId().equals(webId))) {
755
756 throw new CompanyVirtualHostException();
757 }
758 }
759 catch (NoSuchCompanyException nsce) {
760 }
761
762 try {
763 layoutSetLocalService.getLayoutSet(virtualHost);
764
765 throw new CompanyVirtualHostException();
766 }
767 catch (NoSuchLayoutSetException nslse) {
768 }
769 }
770
771 if (Validator.isNull(mx)) {
772 throw new CompanyMxException();
773 }
774 else if (!Validator.isDomain(mx)) {
775 throw new CompanyMxException();
776 }
777 }
778
779 private static final String _DEFAULT_VIRTUAL_HOST = "localhost";
780
781 private static final String[] _KEYWORDS_FIELDS = {
782 Field.ASSET_TAG_NAMES, Field.CONTENT, Field.DESCRIPTION,
783 Field.PROPERTIES, Field.TITLE
784 };
785
786 }