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