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