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