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.service.ServiceContext;
58 import com.liferay.portal.service.base.CompanyLocalServiceBaseImpl;
59 import com.liferay.portal.util.PrefsPropsUtil;
60 import com.liferay.portal.util.PropsValues;
61 import com.liferay.util.Encryptor;
62 import com.liferay.util.EncryptorException;
63
64 import java.io.File;
65 import java.io.IOException;
66 import java.io.InputStream;
67
68 import java.util.Calendar;
69 import java.util.Date;
70 import java.util.List;
71 import java.util.Locale;
72
73 import javax.portlet.PortletException;
74 import javax.portlet.PortletPreferences;
75
76
82 public class CompanyLocalServiceImpl extends CompanyLocalServiceBaseImpl {
83
84 public Company addCompany(
85 String webId, String virtualHost, String mx, String shardName,
86 boolean system)
87 throws PortalException, SystemException {
88
89
91 virtualHost = virtualHost.trim().toLowerCase();
92
93 if ((Validator.isNull(webId)) ||
94 (webId.equals(PropsValues.COMPANY_DEFAULT_WEB_ID)) ||
95 (companyPersistence.fetchByWebId(webId) != null)) {
96
97 throw new CompanyWebIdException();
98 }
99
100 validate(webId, virtualHost, mx);
101
102 Company company = checkCompany(webId, mx, shardName);
103
104 company.setVirtualHost(virtualHost);
105 company.setMx(mx);
106 company.setSystem(system);
107
108 companyPersistence.update(company, false);
109
110 return company;
111 }
112
113 public Company checkCompany(String webId)
114 throws PortalException, SystemException {
115
116 String mx = webId;
117
118 return companyLocalService.checkCompany(
119 webId, mx, PropsValues.SHARD_DEFAULT_NAME);
120 }
121
122 public Company checkCompany(String webId, String mx, String shardName)
123 throws PortalException, SystemException {
124
125
127 Date now = new Date();
128
129 Company company = companyPersistence.fetchByWebId(webId);
130
131 if (company == null) {
132 String virtualHost = webId;
133
134 if (webId.equals(PropsValues.COMPANY_DEFAULT_WEB_ID)) {
135 virtualHost = _DEFAULT_VIRTUAL_HOST;
136 }
137
138 String homeURL = null;
139 String name = webId;
140 String legalName = null;
141 String legalId = null;
142 String legalType = null;
143 String sicCode = null;
144 String tickerSymbol = null;
145 String industry = null;
146 String type = null;
147 String size = null;
148
149 long companyId = counterLocalService.increment();
150
151 company = companyPersistence.create(companyId);
152
153 try {
154 company.setKeyObj(Encryptor.generateKey());
155 }
156 catch (EncryptorException ee) {
157 throw new SystemException(ee);
158 }
159
160 company.setWebId(webId);
161 company.setVirtualHost(virtualHost);
162 company.setMx(mx);
163
164 companyPersistence.update(company, false);
165
166
168 shardLocalService.addShard(
169 Company.class.getName(), companyId, shardName);
170
171
173 updateCompany(
174 companyId, virtualHost, mx, homeURL, name, legalName, legalId,
175 legalType, sicCode, tickerSymbol, industry, type, size);
176
177
179 if (webId.equals("liferay.net")) {
180 company = companyPersistence.findByWebId(webId);
181
182 company.setVirtualHost("demo.liferay.net");
183
184 companyPersistence.update(company, false);
185
186 updateSecurity(
187 companyId, CompanyConstants.AUTH_TYPE_EA, true, true, true,
188 true, false, true);
189
190 PortletPreferences preferences = PrefsPropsUtil.getPreferences(
191 companyId);
192
193 try {
194 preferences.setValue(
195 PropsKeys.ADMIN_EMAIL_FROM_NAME, "Liferay Demo");
196 preferences.setValue(
197 PropsKeys.ADMIN_EMAIL_FROM_ADDRESS, "test@liferay.net");
198
199 preferences.store();
200 }
201 catch (IOException ioe) {
202 throw new SystemException(ioe);
203 }
204 catch (PortletException pe) {
205 throw new SystemException(pe);
206 }
207 }
208 }
209
210 long companyId = company.getCompanyId();
211
212
214 checkCompanyKey(companyId);
215
216
218 User defaultUser = null;
219
220 try {
221 defaultUser = userLocalService.getDefaultUser(companyId);
222
223 if (!defaultUser.isAgreedToTermsOfUse()) {
224 defaultUser.setAgreedToTermsOfUse(true);
225
226 userPersistence.update(defaultUser, false);
227 }
228 }
229 catch (NoSuchUserException nsue) {
230 long userId = counterLocalService.increment();
231
232 defaultUser = userPersistence.create(userId);
233
234 defaultUser.setCompanyId(companyId);
235 defaultUser.setCreateDate(now);
236 defaultUser.setModifiedDate(now);
237 defaultUser.setDefaultUser(true);
238 defaultUser.setContactId(counterLocalService.increment());
239 defaultUser.setPassword("password");
240 defaultUser.setScreenName(String.valueOf(defaultUser.getUserId()));
241 defaultUser.setEmailAddress("default@" + company.getMx());
242 defaultUser.setLanguageId(LocaleUtil.getDefault().toString());
243 defaultUser.setTimeZoneId(TimeZoneUtil.getDefault().getID());
244 defaultUser.setGreeting(
245 LanguageUtil.format(
246 defaultUser.getLocale(), "welcome-x", StringPool.BLANK,
247 false));
248 defaultUser.setLoginDate(now);
249 defaultUser.setFailedLoginAttempts(0);
250 defaultUser.setAgreedToTermsOfUse(true);
251 defaultUser.setActive(true);
252
253 userPersistence.update(defaultUser, false);
254
255
257 Contact defaultContact = contactPersistence.create(
258 defaultUser.getContactId());
259
260 defaultContact.setCompanyId(defaultUser.getCompanyId());
261 defaultContact.setUserId(defaultUser.getUserId());
262 defaultContact.setUserName(StringPool.BLANK);
263 defaultContact.setCreateDate(now);
264 defaultContact.setModifiedDate(now);
265 defaultContact.setAccountId(company.getAccountId());
266 defaultContact.setParentContactId(
267 ContactConstants.DEFAULT_PARENT_CONTACT_ID);
268 defaultContact.setFirstName(StringPool.BLANK);
269 defaultContact.setMiddleName(StringPool.BLANK);
270 defaultContact.setLastName(StringPool.BLANK);
271 defaultContact.setMale(true);
272 defaultContact.setBirthday(now);
273
274 contactPersistence.update(defaultContact, false);
275 }
276
277
279 roleLocalService.checkSystemRoles(companyId);
280
281
283 groupLocalService.checkSystemGroups(companyId);
284
285
287 passwordPolicyLocalService.checkDefaultPasswordPolicy(companyId);
288
289
291 Role guestRole = roleLocalService.getRole(
292 companyId, RoleConstants.GUEST);
293
294 roleLocalService.setUserRoles(
295 defaultUser.getUserId(), new long[] {guestRole.getRoleId()});
296
297
299 if (userPersistence.countByCompanyId(companyId) == 1) {
300 long creatorUserId = 0;
301 boolean autoPassword = false;
302 String password1 = PropsValues.DEFAULT_ADMIN_PASSWORD;
303 String password2 = password1;
304 boolean autoScreenName = false;
305 String screenName = PropsValues.DEFAULT_ADMIN_SCREEN_NAME;
306 String emailAddress =
307 PropsValues.DEFAULT_ADMIN_EMAIL_ADDRESS_PREFIX + "@" + mx;
308 String openId = StringPool.BLANK;
309 Locale locale = defaultUser.getLocale();
310 String firstName = PropsValues.DEFAULT_ADMIN_FIRST_NAME;
311 String middleName = PropsValues.DEFAULT_ADMIN_MIDDLE_NAME;
312 String lastName = PropsValues.DEFAULT_ADMIN_LAST_NAME;
313 int prefixId = 0;
314 int suffixId = 0;
315 boolean male = true;
316 int birthdayMonth = Calendar.JANUARY;
317 int birthdayDay = 1;
318 int birthdayYear = 1970;
319 String jobTitle = StringPool.BLANK;
320
321 Group guestGroup = groupLocalService.getGroup(
322 companyId, GroupConstants.GUEST);
323
324 long[] groupIds = new long[] {guestGroup.getGroupId()};
325
326 long[] organizationIds = null;
327
328 Role adminRole = roleLocalService.getRole(
329 companyId, RoleConstants.ADMINISTRATOR);
330
331 Role powerUserRole = roleLocalService.getRole(
332 companyId, RoleConstants.POWER_USER);
333
334 long[] roleIds = new long[] {
335 adminRole.getRoleId(), powerUserRole.getRoleId()
336 };
337
338 long[] userGroupIds = null;
339 boolean sendEmail = false;
340 ServiceContext serviceContext = new ServiceContext();
341
342 userLocalService.addUser(
343 creatorUserId, companyId, autoPassword, password1, password2,
344 autoScreenName, screenName, emailAddress, openId, locale,
345 firstName, middleName, lastName, prefixId, suffixId, male,
346 birthdayMonth, birthdayDay, birthdayYear, jobTitle, groupIds,
347 organizationIds, roleIds, userGroupIds, sendEmail,
348 serviceContext);
349 }
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.TAGS_CATEGORIES, keywords);
488 searchQuery.addTerm(Field.TAGS_ENTRIES, keywords, true);
489 }
490
491 BooleanQuery fullQuery = BooleanQueryFactoryUtil.create();
492
493 fullQuery.add(contextQuery, BooleanClauseOccur.MUST);
494
495 if (searchQuery.clauses().size() > 0) {
496 fullQuery.add(searchQuery, BooleanClauseOccur.MUST);
497 }
498
499 return SearchEngineUtil.search(
500 companyId, groupId, userId, null, fullQuery, start, end);
501 }
502 catch (Exception e) {
503 throw new SystemException(e);
504 }
505 }
506
507 public Company updateCompany(long companyId, String virtualHost, String mx)
508 throws PortalException, SystemException {
509
510 virtualHost = virtualHost.trim().toLowerCase();
511
512 Company company = companyPersistence.findByPrimaryKey(companyId);
513
514 validate(company.getWebId(), virtualHost, mx);
515
516 company.setVirtualHost(virtualHost);
517
518 if (PropsValues.MAIL_MX_UPDATE) {
519 company.setMx(mx);
520 }
521
522 companyPersistence.update(company, false);
523
524 return company;
525 }
526
527 public Company updateCompany(
528 long companyId, String virtualHost, String mx, String homeURL,
529 String name, String legalName, String legalId, String legalType,
530 String sicCode, String tickerSymbol, String industry, String type,
531 String size)
532 throws PortalException, SystemException {
533
534
536 virtualHost = virtualHost.trim().toLowerCase();
537 Date now = new Date();
538
539 Company company = companyPersistence.findByPrimaryKey(companyId);
540
541 validate(company.getWebId(), virtualHost, mx);
542 validate(name);
543
544 company.setVirtualHost(virtualHost);
545
546 if (PropsValues.MAIL_MX_UPDATE) {
547 company.setMx(mx);
548 }
549
550 company.setHomeURL(homeURL);
551
552 companyPersistence.update(company, false);
553
554
556 Account account = accountPersistence.fetchByPrimaryKey(
557 company.getAccountId());
558
559 if (account == null) {
560 long accountId = counterLocalService.increment();
561
562 account = accountPersistence.create(accountId);
563
564 account.setCreateDate(now);
565 account.setCompanyId(companyId);
566 account.setUserId(0);
567 account.setUserName(StringPool.BLANK);
568
569 company.setAccountId(accountId);
570
571 companyPersistence.update(company, false);
572 }
573
574 account.setModifiedDate(now);
575 account.setName(name);
576 account.setLegalName(legalName);
577 account.setLegalId(legalId);
578 account.setLegalType(legalType);
579 account.setSicCode(sicCode);
580 account.setTickerSymbol(tickerSymbol);
581 account.setIndustry(industry);
582 account.setType(type);
583 account.setSize(size);
584
585 accountPersistence.update(account, false);
586
587 return company;
588 }
589
590 public void updateDisplay(
591 long companyId, String languageId, String timeZoneId)
592 throws PortalException, SystemException {
593
594 User user = userLocalService.getDefaultUser(companyId);
595
596 user.setLanguageId(languageId);
597 user.setTimeZoneId(timeZoneId);
598
599 userPersistence.update(user, false);
600 }
601
602 public void updateLogo(long companyId, byte[] bytes)
603 throws PortalException, SystemException {
604
605 long logoId = getLogoId(companyId);
606
607 imageLocalService.updateImage(logoId, bytes);
608 }
609
610 public void updateLogo(long companyId, File file)
611 throws PortalException, SystemException {
612
613 long logoId = getLogoId(companyId);
614
615 imageLocalService.updateImage(logoId, file);
616 }
617
618 public void updateLogo(long companyId, InputStream is)
619 throws PortalException, SystemException {
620
621 long logoId = getLogoId(companyId);
622
623 imageLocalService.updateImage(logoId, is);
624 }
625
626 public void updatePreferences(long companyId, UnicodeProperties properties)
627 throws SystemException {
628
629 PortletPreferences preferences = PrefsPropsUtil.getPreferences(
630 companyId);
631
632 try {
633 for (String key : properties.keySet()) {
634 String value = properties.getProperty(key);
635
636 preferences.setValue(key, value);
637 }
638
639 preferences.store();
640
641 if (properties.containsKey(PropsKeys.LOCALES)) {
642 String oldLocales = preferences.getValue(
643 PropsKeys.LOCALES, StringPool.BLANK);
644 String newLocales = properties.getProperty(PropsKeys.LOCALES);
645
646 if (!Validator.equals(oldLocales, newLocales)) {
647 LanguageUtil.resetAvailableLocales(companyId);
648 }
649 }
650 }
651 catch (Exception e) {
652 throw new SystemException(e);
653 }
654 }
655
656 public void updateSecurity(
657 long companyId, String authType, boolean autoLogin,
658 boolean sendPassword, boolean strangers, boolean strangersWithMx,
659 boolean strangersVerify, boolean communityLogo)
660 throws SystemException {
661
662 PortletPreferences preferences = PrefsPropsUtil.getPreferences(
663 companyId);
664
665 try {
666 preferences.setValue(
667 PropsKeys.COMPANY_SECURITY_AUTH_TYPE, authType);
668 preferences.setValue(
669 PropsKeys.COMPANY_SECURITY_AUTO_LOGIN,
670 String.valueOf(autoLogin));
671 preferences.setValue(
672 PropsKeys.COMPANY_SECURITY_SEND_PASSWORD,
673 String.valueOf(sendPassword));
674 preferences.setValue(
675 PropsKeys.COMPANY_SECURITY_STRANGERS,
676 String.valueOf(strangers));
677 preferences.setValue(
678 PropsKeys.COMPANY_SECURITY_STRANGERS_WITH_MX,
679 String.valueOf(strangersWithMx));
680 preferences.setValue(
681 PropsKeys.COMPANY_SECURITY_STRANGERS_VERIFY,
682 String.valueOf(strangersVerify));
683 preferences.setValue(
684 PropsKeys.COMPANY_SECURITY_COMMUNITY_LOGO,
685 String.valueOf(communityLogo));
686
687 preferences.store();
688 }
689 catch (IOException ioe) {
690 throw new SystemException(ioe);
691 }
692 catch (PortletException pe) {
693 throw new SystemException(pe);
694 }
695 }
696
697 protected long getLogoId(long companyId)
698 throws PortalException, SystemException {
699
700 Company company = companyPersistence.findByPrimaryKey(companyId);
701
702 long logoId = company.getLogoId();
703
704 if (logoId <= 0) {
705 logoId = counterLocalService.increment();
706
707 company.setLogoId(logoId);
708
709 companyPersistence.update(company, false);
710 }
711
712 return logoId;
713 }
714
715 protected void validate(String name) throws PortalException {
716 if (Validator.isNull(name)) {
717 throw new AccountNameException();
718 }
719 }
720
721 protected void validate(String webId, String virtualHost, String mx)
722 throws PortalException, SystemException {
723
724 if (Validator.isNull(virtualHost)) {
725 throw new CompanyVirtualHostException();
726 }
727 else if (virtualHost.equals(_DEFAULT_VIRTUAL_HOST) &&
728 !webId.equals(PropsValues.COMPANY_DEFAULT_WEB_ID)) {
729
730 throw new CompanyVirtualHostException();
731 }
732 else if (!Validator.isDomain(virtualHost)) {
733 throw new CompanyVirtualHostException();
734 }
735 else {
736 try {
737 Company virtualHostCompany = getCompanyByVirtualHost(
738 virtualHost);
739
740 if ((virtualHostCompany != null) &&
741 (!virtualHostCompany.getWebId().equals(webId))) {
742
743 throw new CompanyVirtualHostException();
744 }
745 }
746 catch (NoSuchCompanyException nsce) {
747 }
748
749 try {
750 layoutSetLocalService.getLayoutSet(virtualHost);
751
752 throw new CompanyVirtualHostException();
753 }
754 catch (NoSuchLayoutSetException nslse) {
755 }
756 }
757
758 if (Validator.isNull(mx)) {
759 throw new CompanyMxException();
760 }
761 else if (!Validator.isDomain(mx)) {
762 throw new CompanyMxException();
763 }
764 }
765
766 private static final String _DEFAULT_VIRTUAL_HOST = "localhost";
767
768 }