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