1
14
15 package com.liferay.portal.service.impl;
16
17 import com.liferay.portal.AccountNameException;
18 import com.liferay.portal.CompanyMxException;
19 import com.liferay.portal.CompanyVirtualHostException;
20 import com.liferay.portal.CompanyWebIdException;
21 import com.liferay.portal.NoSuchCompanyException;
22 import com.liferay.portal.NoSuchLayoutSetException;
23 import com.liferay.portal.NoSuchShardException;
24 import com.liferay.portal.NoSuchUserException;
25 import com.liferay.portal.kernel.exception.PortalException;
26 import com.liferay.portal.kernel.exception.SystemException;
27 import com.liferay.portal.kernel.language.LanguageUtil;
28 import com.liferay.portal.kernel.search.BooleanClauseOccur;
29 import com.liferay.portal.kernel.search.BooleanQuery;
30 import com.liferay.portal.kernel.search.BooleanQueryFactoryUtil;
31 import com.liferay.portal.kernel.search.Field;
32 import com.liferay.portal.kernel.search.Hits;
33 import com.liferay.portal.kernel.search.SearchEngineUtil;
34 import com.liferay.portal.kernel.util.LocaleUtil;
35 import com.liferay.portal.kernel.util.PropsKeys;
36 import com.liferay.portal.kernel.util.StringPool;
37 import com.liferay.portal.kernel.util.TimeZoneUtil;
38 import com.liferay.portal.kernel.util.UnicodeProperties;
39 import com.liferay.portal.kernel.util.Validator;
40 import com.liferay.portal.model.Account;
41 import com.liferay.portal.model.Company;
42 import com.liferay.portal.model.CompanyConstants;
43 import com.liferay.portal.model.Contact;
44 import com.liferay.portal.model.ContactConstants;
45 import com.liferay.portal.model.Group;
46 import com.liferay.portal.model.GroupConstants;
47 import com.liferay.portal.model.Role;
48 import com.liferay.portal.model.RoleConstants;
49 import com.liferay.portal.model.User;
50 import com.liferay.portal.service.ServiceContext;
51 import com.liferay.portal.service.base.CompanyLocalServiceBaseImpl;
52 import com.liferay.portal.util.PrefsPropsUtil;
53 import com.liferay.portal.util.PropsValues;
54 import com.liferay.util.Encryptor;
55 import com.liferay.util.EncryptorException;
56
57 import java.io.File;
58 import java.io.IOException;
59 import java.io.InputStream;
60
61 import java.util.Calendar;
62 import java.util.Date;
63 import java.util.List;
64 import java.util.Locale;
65
66 import javax.portlet.PortletException;
67 import javax.portlet.PortletPreferences;
68
69
75 public class CompanyLocalServiceImpl extends CompanyLocalServiceBaseImpl {
76
77 public Company addCompany(
78 String webId, String virtualHost, String mx, String shardName,
79 boolean system, int maxUsers)
80 throws PortalException, SystemException {
81
82
84 virtualHost = virtualHost.trim().toLowerCase();
85
86 if ((Validator.isNull(webId)) ||
87 (webId.equals(PropsValues.COMPANY_DEFAULT_WEB_ID)) ||
88 (companyPersistence.fetchByWebId(webId) != null)) {
89
90 throw new CompanyWebIdException();
91 }
92
93 validate(webId, virtualHost, mx);
94
95 Company company = checkCompany(webId, mx, shardName);
96
97 company.setVirtualHost(virtualHost);
98 company.setMx(mx);
99 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
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
162 shardLocalService.addShard(
163 Company.class.getName(), companyId, shardName);
164
165
167 updateCompany(
168 companyId, virtualHost, mx, homeURL, name, legalName, legalId,
169 legalType, sicCode, tickerSymbol, industry, type, size);
170
171
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
218 checkCompanyKey(companyId);
219
220
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
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
283 roleLocalService.checkSystemRoles(companyId);
284
285
287 groupLocalService.checkSystemGroups(companyId);
288
289
291 groupLocalService.checkCompanyGroup(companyId);
292
293
295 passwordPolicyLocalService.checkDefaultPasswordPolicy(companyId);
296
297
299 Role guestRole = roleLocalService.getRole(
300 companyId, RoleConstants.GUEST);
301
302 roleLocalService.setUserRoles(
303 defaultUser.getUserId(), new long[] {guestRole.getRoleId()});
304
305
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 String openId = StringPool.BLANK;
317 Locale locale = defaultUser.getLocale();
318 String firstName = PropsValues.DEFAULT_ADMIN_FIRST_NAME;
319 String middleName = PropsValues.DEFAULT_ADMIN_MIDDLE_NAME;
320 String lastName = PropsValues.DEFAULT_ADMIN_LAST_NAME;
321 int prefixId = 0;
322 int suffixId = 0;
323 boolean male = true;
324 int birthdayMonth = Calendar.JANUARY;
325 int birthdayDay = 1;
326 int birthdayYear = 1970;
327 String jobTitle = StringPool.BLANK;
328
329 Group guestGroup = groupLocalService.getGroup(
330 companyId, GroupConstants.GUEST);
331
332 long[] groupIds = new long[] {guestGroup.getGroupId()};
333
334 long[] organizationIds = null;
335
336 Role adminRole = roleLocalService.getRole(
337 companyId, RoleConstants.ADMINISTRATOR);
338
339 Role powerUserRole = roleLocalService.getRole(
340 companyId, RoleConstants.POWER_USER);
341
342 long[] roleIds = new long[] {
343 adminRole.getRoleId(), powerUserRole.getRoleId()
344 };
345
346 long[] userGroupIds = null;
347 boolean sendEmail = false;
348 ServiceContext serviceContext = new ServiceContext();
349
350 userLocalService.addUser(
351 creatorUserId, companyId, autoPassword, password1, password2,
352 autoScreenName, screenName, emailAddress, openId, locale,
353 firstName, middleName, lastName, prefixId, suffixId, male,
354 birthdayMonth, birthdayDay, birthdayYear, jobTitle, groupIds,
355 organizationIds, roleIds, userGroupIds, sendEmail,
356 serviceContext);
357 }
358
359
361 portletLocalService.checkPortlets(companyId);
362
363 return company;
364 }
365
366 public void checkCompanyKey(long companyId)
367 throws PortalException, SystemException {
368
369 Company company = companyPersistence.findByPrimaryKey(companyId);
370
371 if ((Validator.isNull(company.getKey())) &&
372 (company.getKeyObj() == null)) {
373
374 try {
375 company.setKeyObj(Encryptor.generateKey());
376 }
377 catch (EncryptorException ee) {
378 throw new SystemException(ee);
379 }
380
381 companyPersistence.update(company, false);
382 }
383 }
384
385 public void deleteLogo(long companyId)
386 throws PortalException, SystemException {
387
388 Company company = companyPersistence.findByPrimaryKey(companyId);
389
390 long logoId = company.getLogoId();
391
392 if (logoId > 0) {
393 company.setLogoId(0);
394
395 companyPersistence.update(company, false);
396
397 imageLocalService.deleteImage(logoId);
398 }
399 }
400
401 public List<Company> getCompanies() throws SystemException {
402 return companyPersistence.findAll();
403 }
404
405 public List<Company> getCompanies(boolean system) throws SystemException {
406 return companyPersistence.findBySystem(system);
407 }
408
409 public int getCompaniesCount(boolean system) throws SystemException {
410 return companyPersistence.countBySystem(system);
411 }
412
413 public Company getCompanyById(long companyId)
414 throws PortalException, SystemException {
415
416 return companyPersistence.findByPrimaryKey(companyId);
417 }
418
419 public Company getCompanyByLogoId(long logoId)
420 throws PortalException, SystemException {
421
422 return companyPersistence.findByLogoId(logoId);
423 }
424
425 public Company getCompanyByMx(String mx)
426 throws PortalException, SystemException {
427
428 return companyPersistence.findByMx(mx);
429 }
430
431 public Company getCompanyByVirtualHost(String virtualHost)
432 throws PortalException, SystemException {
433
434 virtualHost = virtualHost.trim().toLowerCase();
435
436 return companyPersistence.findByVirtualHost(virtualHost);
437 }
438
439 public Company getCompanyByWebId(String webId)
440 throws PortalException, SystemException {
441
442 return companyPersistence.findByWebId(webId);
443 }
444
445 public void removePreferences(long companyId, String[] keys)
446 throws SystemException {
447
448 PortletPreferences preferences = PrefsPropsUtil.getPreferences(
449 companyId);
450
451 try {
452 for (String key : keys) {
453 preferences.reset(key);
454 }
455
456 preferences.store();
457 }
458 catch (Exception e) {
459 throw new SystemException(e);
460 }
461 }
462
463 public Hits search(
464 long companyId, long userId, String keywords, int start, int end)
465 throws SystemException {
466
467 return search(companyId, userId, null, 0, null, keywords, start, end);
468 }
469
470 public Hits search(
471 long companyId, long userId, String portletId, long groupId,
472 String type, String keywords, int start, int end)
473 throws SystemException {
474
475 try {
476 BooleanQuery contextQuery = BooleanQueryFactoryUtil.create();
477
478 contextQuery.addRequiredTerm(Field.COMPANY_ID, companyId);
479
480 if (Validator.isNotNull(portletId)) {
481 contextQuery.addRequiredTerm(Field.PORTLET_ID, portletId);
482 }
483
484 if (groupId > 0) {
485 contextQuery.addRequiredTerm(Field.GROUP_ID, groupId);
486 }
487
488 if (Validator.isNotNull(type)) {
489 contextQuery.addRequiredTerm(Field.TYPE, type);
490 }
491
492 BooleanQuery searchQuery = BooleanQueryFactoryUtil.create();
493
494 searchQuery.addTerms(_KEYWORDS_FIELDS, keywords);
495
496 BooleanQuery fullQuery = BooleanQueryFactoryUtil.create();
497
498 fullQuery.add(contextQuery, BooleanClauseOccur.MUST);
499
500 if (searchQuery.clauses().size() > 0) {
501 fullQuery.add(searchQuery, BooleanClauseOccur.MUST);
502 }
503
504 return SearchEngineUtil.search(
505 companyId, new long[] {groupId}, userId, null, fullQuery, start,
506 end);
507 }
508 catch (Exception e) {
509 throw new SystemException(e);
510 }
511 }
512
513 public Company updateCompany(
514 long companyId, String virtualHost, String mx, int maxUsers)
515 throws PortalException, SystemException {
516
517 virtualHost = virtualHost.trim().toLowerCase();
518
519 Company company = companyPersistence.findByPrimaryKey(companyId);
520
521 validate(company.getWebId(), virtualHost, mx);
522
523 company.setVirtualHost(virtualHost);
524
525 if (PropsValues.MAIL_MX_UPDATE) {
526 company.setMx(mx);
527 }
528
529 company.setMaxUsers(maxUsers);
530
531 companyPersistence.update(company, false);
532
533 return company;
534 }
535
536 public Company updateCompany(
537 long companyId, String virtualHost, String mx, String homeURL,
538 String name, String legalName, String legalId, String legalType,
539 String sicCode, String tickerSymbol, String industry, String type,
540 String size)
541 throws PortalException, SystemException {
542
543
545 virtualHost = virtualHost.trim().toLowerCase();
546 Date now = new Date();
547
548 Company company = companyPersistence.findByPrimaryKey(companyId);
549
550 validate(company.getWebId(), virtualHost, mx);
551 validate(name);
552
553 company.setVirtualHost(virtualHost);
554
555 if (PropsValues.MAIL_MX_UPDATE) {
556 company.setMx(mx);
557 }
558
559 company.setHomeURL(homeURL);
560
561 companyPersistence.update(company, false);
562
563
565 Account account = accountPersistence.fetchByPrimaryKey(
566 company.getAccountId());
567
568 if (account == null) {
569 long accountId = counterLocalService.increment();
570
571 account = accountPersistence.create(accountId);
572
573 account.setCreateDate(now);
574 account.setCompanyId(companyId);
575 account.setUserId(0);
576 account.setUserName(StringPool.BLANK);
577
578 company.setAccountId(accountId);
579
580 companyPersistence.update(company, false);
581 }
582
583 account.setModifiedDate(now);
584 account.setName(name);
585 account.setLegalName(legalName);
586 account.setLegalId(legalId);
587 account.setLegalType(legalType);
588 account.setSicCode(sicCode);
589 account.setTickerSymbol(tickerSymbol);
590 account.setIndustry(industry);
591 account.setType(type);
592 account.setSize(size);
593
594 accountPersistence.update(account, false);
595
596 return company;
597 }
598
599 public void updateDisplay(
600 long companyId, String languageId, String timeZoneId)
601 throws PortalException, SystemException {
602
603 User user = userLocalService.getDefaultUser(companyId);
604
605 user.setLanguageId(languageId);
606 user.setTimeZoneId(timeZoneId);
607
608 userPersistence.update(user, false);
609 }
610
611 public void updateLogo(long companyId, byte[] bytes)
612 throws PortalException, SystemException {
613
614 long logoId = getLogoId(companyId);
615
616 imageLocalService.updateImage(logoId, bytes);
617 }
618
619 public void updateLogo(long companyId, File file)
620 throws PortalException, SystemException {
621
622 long logoId = getLogoId(companyId);
623
624 imageLocalService.updateImage(logoId, file);
625 }
626
627 public void updateLogo(long companyId, InputStream is)
628 throws PortalException, SystemException {
629
630 long logoId = getLogoId(companyId);
631
632 imageLocalService.updateImage(logoId, is);
633 }
634
635 public void updatePreferences(long companyId, UnicodeProperties properties)
636 throws SystemException {
637
638 PortletPreferences preferences = PrefsPropsUtil.getPreferences(
639 companyId);
640
641 try {
642 if (properties.containsKey(PropsKeys.LOCALES)) {
643 String oldLocales = preferences.getValue(
644 PropsKeys.LOCALES, StringPool.BLANK);
645 String newLocales = properties.getProperty(PropsKeys.LOCALES);
646
647 if (!Validator.equals(oldLocales, newLocales)) {
648 LanguageUtil.resetAvailableLocales(companyId);
649 }
650 }
651
652 for (String key : properties.keySet()) {
653 String value = properties.getProperty(key);
654
655 preferences.setValue(key, value);
656 }
657
658 preferences.store();
659 }
660 catch (Exception e) {
661 throw new SystemException(e);
662 }
663 }
664
665 public void updateSecurity(
666 long companyId, String authType, boolean autoLogin,
667 boolean sendPassword, boolean strangers, boolean strangersWithMx,
668 boolean strangersVerify, boolean communityLogo)
669 throws SystemException {
670
671 PortletPreferences preferences = PrefsPropsUtil.getPreferences(
672 companyId);
673
674 try {
675 preferences.setValue(
676 PropsKeys.COMPANY_SECURITY_AUTH_TYPE, authType);
677 preferences.setValue(
678 PropsKeys.COMPANY_SECURITY_AUTO_LOGIN,
679 String.valueOf(autoLogin));
680 preferences.setValue(
681 PropsKeys.COMPANY_SECURITY_SEND_PASSWORD,
682 String.valueOf(sendPassword));
683 preferences.setValue(
684 PropsKeys.COMPANY_SECURITY_STRANGERS,
685 String.valueOf(strangers));
686 preferences.setValue(
687 PropsKeys.COMPANY_SECURITY_STRANGERS_WITH_MX,
688 String.valueOf(strangersWithMx));
689 preferences.setValue(
690 PropsKeys.COMPANY_SECURITY_STRANGERS_VERIFY,
691 String.valueOf(strangersVerify));
692 preferences.setValue(
693 PropsKeys.COMPANY_SECURITY_COMMUNITY_LOGO,
694 String.valueOf(communityLogo));
695
696 preferences.store();
697 }
698 catch (IOException ioe) {
699 throw new SystemException(ioe);
700 }
701 catch (PortletException pe) {
702 throw new SystemException(pe);
703 }
704 }
705
706 protected long getLogoId(long companyId)
707 throws PortalException, SystemException {
708
709 Company company = companyPersistence.findByPrimaryKey(companyId);
710
711 long logoId = company.getLogoId();
712
713 if (logoId <= 0) {
714 logoId = counterLocalService.increment();
715
716 company.setLogoId(logoId);
717
718 companyPersistence.update(company, false);
719 }
720
721 return logoId;
722 }
723
724 protected void validate(String name) throws PortalException {
725 if (Validator.isNull(name)) {
726 throw new AccountNameException();
727 }
728 }
729
730 protected void validate(String webId, String virtualHost, String mx)
731 throws PortalException, SystemException {
732
733 if (Validator.isNull(virtualHost)) {
734 throw new CompanyVirtualHostException();
735 }
736 else if (virtualHost.equals(_DEFAULT_VIRTUAL_HOST) &&
737 !webId.equals(PropsValues.COMPANY_DEFAULT_WEB_ID)) {
738
739 throw new CompanyVirtualHostException();
740 }
741 else if (!Validator.isDomain(virtualHost)) {
742 throw new CompanyVirtualHostException();
743 }
744 else {
745 try {
746 Company virtualHostCompany = getCompanyByVirtualHost(
747 virtualHost);
748
749 if ((virtualHostCompany != null) &&
750 (!virtualHostCompany.getWebId().equals(webId))) {
751
752 throw new CompanyVirtualHostException();
753 }
754 }
755 catch (NoSuchCompanyException nsce) {
756 }
757
758 try {
759 layoutSetLocalService.getLayoutSet(virtualHost);
760
761 throw new CompanyVirtualHostException();
762 }
763 catch (NoSuchLayoutSetException nslse) {
764 }
765 }
766
767 if (Validator.isNull(mx)) {
768 throw new CompanyMxException();
769 }
770 else if (!Validator.isDomain(mx)) {
771 throw new CompanyMxException();
772 }
773 }
774
775 private static final String _DEFAULT_VIRTUAL_HOST = "localhost";
776
777 private static final String[] _KEYWORDS_FIELDS = {
778 Field.ASSET_TAG_NAMES, Field.CONTENT, Field.DESCRIPTION,
779 Field.PROPERTIES, Field.TITLE
780 };
781
782 }