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.Portal;
53 import com.liferay.portal.util.PrefsPropsUtil;
54 import com.liferay.portal.util.PropsValues;
55 import com.liferay.util.Encryptor;
56 import com.liferay.util.EncryptorException;
57
58 import java.io.File;
59 import java.io.IOException;
60 import java.io.InputStream;
61
62 import java.util.Calendar;
63 import java.util.Date;
64 import java.util.List;
65 import java.util.Locale;
66
67 import javax.portlet.PortletException;
68 import javax.portlet.PortletPreferences;
69
70
76 public class CompanyLocalServiceImpl extends CompanyLocalServiceBaseImpl {
77
78 public Company addCompany(
79 String webId, String virtualHost, String mx, String shardName,
80 boolean system)
81 throws PortalException, SystemException {
82
83
85 virtualHost = virtualHost.trim().toLowerCase();
86
87 if ((Validator.isNull(webId)) ||
88 (webId.equals(PropsValues.COMPANY_DEFAULT_WEB_ID)) ||
89 (companyPersistence.fetchByWebId(webId) != null)) {
90
91 throw new CompanyWebIdException();
92 }
93
94 validate(webId, virtualHost, mx);
95
96 Company company = checkCompany(webId, mx, shardName);
97
98 company.setVirtualHost(virtualHost);
99 company.setMx(mx);
100 company.setSystem(system);
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 passwordPolicyLocalService.checkDefaultPasswordPolicy(companyId);
292
293
295 Role guestRole = roleLocalService.getRole(
296 companyId, RoleConstants.GUEST);
297
298 roleLocalService.setUserRoles(
299 defaultUser.getUserId(), new long[] {guestRole.getRoleId()});
300
301
303 if (userPersistence.countByCompanyId(companyId) == 1) {
304 long creatorUserId = 0;
305 boolean autoPassword = false;
306 String password1 = PropsValues.DEFAULT_ADMIN_PASSWORD;
307 String password2 = password1;
308 boolean autoScreenName = false;
309 String screenName = PropsValues.DEFAULT_ADMIN_SCREEN_NAME;
310 String emailAddress =
311 PropsValues.DEFAULT_ADMIN_EMAIL_ADDRESS_PREFIX + "@" + mx;
312 String openId = StringPool.BLANK;
313 Locale locale = defaultUser.getLocale();
314 String firstName = PropsValues.DEFAULT_ADMIN_FIRST_NAME;
315 String middleName = PropsValues.DEFAULT_ADMIN_MIDDLE_NAME;
316 String lastName = PropsValues.DEFAULT_ADMIN_LAST_NAME;
317 int prefixId = 0;
318 int suffixId = 0;
319 boolean male = true;
320 int birthdayMonth = Calendar.JANUARY;
321 int birthdayDay = 1;
322 int birthdayYear = 1970;
323 String jobTitle = StringPool.BLANK;
324
325 Group guestGroup = groupLocalService.getGroup(
326 companyId, GroupConstants.GUEST);
327
328 long[] groupIds = new long[] {guestGroup.getGroupId()};
329
330 long[] organizationIds = null;
331
332 Role adminRole = roleLocalService.getRole(
333 companyId, RoleConstants.ADMINISTRATOR);
334
335 Role powerUserRole = roleLocalService.getRole(
336 companyId, RoleConstants.POWER_USER);
337
338 long[] roleIds = new long[] {
339 adminRole.getRoleId(), powerUserRole.getRoleId()
340 };
341
342 long[] userGroupIds = null;
343 boolean sendEmail = false;
344 ServiceContext serviceContext = new ServiceContext();
345
346 userLocalService.addUser(
347 creatorUserId, companyId, autoPassword, password1, password2,
348 autoScreenName, screenName, emailAddress, openId, locale,
349 firstName, middleName, lastName, prefixId, suffixId, male,
350 birthdayMonth, birthdayDay, birthdayYear, jobTitle, groupIds,
351 organizationIds, roleIds, userGroupIds, sendEmail,
352 serviceContext);
353 }
354
355 return company;
356 }
357
358 public void checkCompanyKey(long companyId)
359 throws PortalException, SystemException {
360
361 Company company = companyPersistence.findByPrimaryKey(companyId);
362
363 if ((Validator.isNull(company.getKey())) &&
364 (company.getKeyObj() == null)) {
365
366 try {
367 company.setKeyObj(Encryptor.generateKey());
368 }
369 catch (EncryptorException ee) {
370 throw new SystemException(ee);
371 }
372
373 companyPersistence.update(company, false);
374 }
375 }
376
377 public void deleteLogo(long companyId)
378 throws PortalException, SystemException {
379
380 Company company = companyPersistence.findByPrimaryKey(companyId);
381
382 long logoId = company.getLogoId();
383
384 if (logoId > 0) {
385 company.setLogoId(0);
386
387 companyPersistence.update(company, false);
388
389 imageLocalService.deleteImage(logoId);
390 }
391 }
392
393 public List<Company> getCompanies() throws SystemException {
394 return companyPersistence.findAll();
395 }
396
397 public List<Company> getCompanies(boolean system) throws SystemException {
398 return companyPersistence.findBySystem(system);
399 }
400
401 public int getCompaniesCount(boolean system) throws SystemException {
402 return companyPersistence.countBySystem(system);
403 }
404
405 public Company getCompanyById(long companyId)
406 throws PortalException, SystemException {
407
408 return companyPersistence.findByPrimaryKey(companyId);
409 }
410
411 public Company getCompanyByLogoId(long logoId)
412 throws PortalException, SystemException {
413
414 return companyPersistence.findByLogoId(logoId);
415 }
416
417 public Company getCompanyByMx(String mx)
418 throws PortalException, SystemException {
419
420 return companyPersistence.findByMx(mx);
421 }
422
423 public Company getCompanyByVirtualHost(String virtualHost)
424 throws PortalException, SystemException {
425
426 virtualHost = virtualHost.trim().toLowerCase();
427
428 return companyPersistence.findByVirtualHost(virtualHost);
429 }
430
431 public Company getCompanyByWebId(String webId)
432 throws PortalException, SystemException {
433
434 return companyPersistence.findByWebId(webId);
435 }
436
437 public void removePreferences(long companyId, String[] keys)
438 throws SystemException {
439
440 PortletPreferences preferences = PrefsPropsUtil.getPreferences(
441 companyId);
442
443 try {
444 for (String key : keys) {
445 preferences.reset(key);
446 }
447
448 preferences.store();
449 }
450 catch (Exception e) {
451 throw new SystemException(e);
452 }
453 }
454
455 public Hits search(
456 long companyId, long userId, String keywords, int start, int end)
457 throws SystemException {
458
459 return search(companyId, userId, null, 0, null, keywords, start, end);
460 }
461
462 public Hits search(
463 long companyId, long userId, String portletId, long groupId,
464 String type, String keywords, int start, int end)
465 throws SystemException {
466
467 try {
468 BooleanQuery contextQuery = BooleanQueryFactoryUtil.create();
469
470 contextQuery.addRequiredTerm(Field.COMPANY_ID, companyId);
471
472 if (Validator.isNotNull(portletId)) {
473 contextQuery.addRequiredTerm(Field.PORTLET_ID, portletId);
474 }
475
476 if (groupId > 0) {
477 contextQuery.addRequiredTerm(Field.GROUP_ID, groupId);
478 }
479
480 if (Validator.isNotNull(type)) {
481 contextQuery.addRequiredTerm(Field.TYPE, type);
482 }
483
484 BooleanQuery searchQuery = BooleanQueryFactoryUtil.create();
485
486 searchQuery.addTerms(_KEYWORDS_FIELDS, keywords);
487
488 searchQuery.addExactTerm(Field.TAGS_ENTRIES, keywords);
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 if (!value.equals(Portal.TEMP_OBFUSCATION_VALUE)) {
646 preferences.setValue(key, value);
647 }
648 }
649
650 preferences.store();
651 }
652 catch (Exception e) {
653 throw new SystemException(e);
654 }
655 }
656
657 public void updateSecurity(
658 long companyId, String authType, boolean autoLogin,
659 boolean sendPassword, boolean strangers, boolean strangersWithMx,
660 boolean strangersVerify, boolean communityLogo)
661 throws SystemException {
662
663 PortletPreferences preferences = PrefsPropsUtil.getPreferences(
664 companyId);
665
666 try {
667 preferences.setValue(
668 PropsKeys.COMPANY_SECURITY_AUTH_TYPE, authType);
669 preferences.setValue(
670 PropsKeys.COMPANY_SECURITY_AUTO_LOGIN,
671 String.valueOf(autoLogin));
672 preferences.setValue(
673 PropsKeys.COMPANY_SECURITY_SEND_PASSWORD,
674 String.valueOf(sendPassword));
675 preferences.setValue(
676 PropsKeys.COMPANY_SECURITY_STRANGERS,
677 String.valueOf(strangers));
678 preferences.setValue(
679 PropsKeys.COMPANY_SECURITY_STRANGERS_WITH_MX,
680 String.valueOf(strangersWithMx));
681 preferences.setValue(
682 PropsKeys.COMPANY_SECURITY_STRANGERS_VERIFY,
683 String.valueOf(strangersVerify));
684 preferences.setValue(
685 PropsKeys.COMPANY_SECURITY_COMMUNITY_LOGO,
686 String.valueOf(communityLogo));
687
688 preferences.store();
689 }
690 catch (IOException ioe) {
691 throw new SystemException(ioe);
692 }
693 catch (PortletException pe) {
694 throw new SystemException(pe);
695 }
696 }
697
698 protected long getLogoId(long companyId)
699 throws PortalException, SystemException {
700
701 Company company = companyPersistence.findByPrimaryKey(companyId);
702
703 long logoId = company.getLogoId();
704
705 if (logoId <= 0) {
706 logoId = counterLocalService.increment();
707
708 company.setLogoId(logoId);
709
710 companyPersistence.update(company, false);
711 }
712
713 return logoId;
714 }
715
716 protected void validate(String name) throws PortalException {
717 if (Validator.isNull(name)) {
718 throw new AccountNameException();
719 }
720 }
721
722 protected void validate(String webId, String virtualHost, String mx)
723 throws PortalException, SystemException {
724
725 if (Validator.isNull(virtualHost)) {
726 throw new CompanyVirtualHostException();
727 }
728 else if (virtualHost.equals(_DEFAULT_VIRTUAL_HOST) &&
729 !webId.equals(PropsValues.COMPANY_DEFAULT_WEB_ID)) {
730
731 throw new CompanyVirtualHostException();
732 }
733 else if (!Validator.isDomain(virtualHost)) {
734 throw new CompanyVirtualHostException();
735 }
736 else {
737 try {
738 Company virtualHostCompany = getCompanyByVirtualHost(
739 virtualHost);
740
741 if ((virtualHostCompany != null) &&
742 (!virtualHostCompany.getWebId().equals(webId))) {
743
744 throw new CompanyVirtualHostException();
745 }
746 }
747 catch (NoSuchCompanyException nsce) {
748 }
749
750 try {
751 layoutSetLocalService.getLayoutSet(virtualHost);
752
753 throw new CompanyVirtualHostException();
754 }
755 catch (NoSuchLayoutSetException nslse) {
756 }
757 }
758
759 if (Validator.isNull(mx)) {
760 throw new CompanyMxException();
761 }
762 else if (!Validator.isDomain(mx)) {
763 throw new CompanyMxException();
764 }
765 }
766
767 private static final String _DEFAULT_VIRTUAL_HOST = "localhost";
768
769 private static final String[] _KEYWORDS_FIELDS = {
770 Field.CONTENT, Field.DESCRIPTION, Field.PROPERTIES,
771 Field.TAGS_CATEGORIES, Field.TITLE
772 };
773
774 }