001
014
015 package com.liferay.portal.service.impl;
016
017 import com.liferay.portal.AccountNameException;
018 import com.liferay.portal.CompanyMxException;
019 import com.liferay.portal.CompanyVirtualHostException;
020 import com.liferay.portal.CompanyWebIdException;
021 import com.liferay.portal.NoSuchCompanyException;
022 import com.liferay.portal.NoSuchLayoutSetException;
023 import com.liferay.portal.NoSuchShardException;
024 import com.liferay.portal.NoSuchUserException;
025 import com.liferay.portal.kernel.exception.PortalException;
026 import com.liferay.portal.kernel.exception.SystemException;
027 import com.liferay.portal.kernel.language.LanguageUtil;
028 import com.liferay.portal.kernel.search.BooleanClauseOccur;
029 import com.liferay.portal.kernel.search.BooleanQuery;
030 import com.liferay.portal.kernel.search.BooleanQueryFactoryUtil;
031 import com.liferay.portal.kernel.search.Field;
032 import com.liferay.portal.kernel.search.Hits;
033 import com.liferay.portal.kernel.search.SearchEngineUtil;
034 import com.liferay.portal.kernel.util.LocaleUtil;
035 import com.liferay.portal.kernel.util.PropsKeys;
036 import com.liferay.portal.kernel.util.StringPool;
037 import com.liferay.portal.kernel.util.TimeZoneUtil;
038 import com.liferay.portal.kernel.util.UnicodeProperties;
039 import com.liferay.portal.kernel.util.Validator;
040 import com.liferay.portal.model.Account;
041 import com.liferay.portal.model.Company;
042 import com.liferay.portal.model.CompanyConstants;
043 import com.liferay.portal.model.Contact;
044 import com.liferay.portal.model.ContactConstants;
045 import com.liferay.portal.model.Group;
046 import com.liferay.portal.model.GroupConstants;
047 import com.liferay.portal.model.Role;
048 import com.liferay.portal.model.RoleConstants;
049 import com.liferay.portal.model.User;
050 import com.liferay.portal.service.ServiceContext;
051 import com.liferay.portal.service.base.CompanyLocalServiceBaseImpl;
052 import com.liferay.portal.util.Portal;
053 import com.liferay.portal.util.PrefsPropsUtil;
054 import com.liferay.portal.util.PropsValues;
055 import com.liferay.util.Encryptor;
056 import com.liferay.util.EncryptorException;
057
058 import java.io.File;
059 import java.io.IOException;
060 import java.io.InputStream;
061
062 import java.util.Calendar;
063 import java.util.Date;
064 import java.util.List;
065 import java.util.Locale;
066
067 import javax.portlet.PortletException;
068 import javax.portlet.PortletPreferences;
069
070
074 public class CompanyLocalServiceImpl extends CompanyLocalServiceBaseImpl {
075
076 public Company addCompany(
077 String webId, String virtualHost, String mx, String shardName,
078 boolean system, int maxUsers)
079 throws PortalException, SystemException {
080
081
082
083 virtualHost = virtualHost.trim().toLowerCase();
084
085 if ((Validator.isNull(webId)) ||
086 (webId.equals(PropsValues.COMPANY_DEFAULT_WEB_ID)) ||
087 (companyPersistence.fetchByWebId(webId) != null)) {
088
089 throw new CompanyWebIdException();
090 }
091
092 validate(webId, virtualHost, mx);
093
094 Company company = checkCompany(webId, mx, shardName);
095
096 company.setVirtualHost(virtualHost);
097 company.setMx(mx);
098 company.setSystem(system);
099 company.setMaxUsers(maxUsers);
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
119
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
160
161 shardLocalService.addShard(
162 Company.class.getName(), companyId, shardName);
163
164
165
166 updateCompany(
167 companyId, virtualHost, mx, homeURL, name, legalName, legalId,
168 legalType, sicCode, tickerSymbol, industry, type, size);
169
170
171
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
216
217 checkCompanyKey(companyId);
218
219
220
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
259
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
281
282 roleLocalService.checkSystemRoles(companyId);
283
284
285
286 groupLocalService.checkSystemGroups(companyId);
287
288
289
290 groupLocalService.checkCompanyGroup(companyId);
291
292
293
294 passwordPolicyLocalService.checkDefaultPasswordPolicy(companyId);
295
296
297
298 Role guestRole = roleLocalService.getRole(
299 companyId, RoleConstants.GUEST);
300
301 roleLocalService.setUserRoles(
302 defaultUser.getUserId(), new long[] {guestRole.getRoleId()});
303
304
305
306 if (userPersistence.countByCompanyId(companyId) == 1) {
307 long creatorUserId = 0;
308 boolean autoPassword = false;
309 String password1 = PropsValues.DEFAULT_ADMIN_PASSWORD;
310 String password2 = password1;
311 boolean autoScreenName = false;
312 String screenName = PropsValues.DEFAULT_ADMIN_SCREEN_NAME;
313 String emailAddress =
314 PropsValues.DEFAULT_ADMIN_EMAIL_ADDRESS_PREFIX + "@" + mx;
315 long facebookId = 0;
316 String openId = StringPool.BLANK;
317 Locale locale = defaultUser.getLocale();
318 String firstName = PropsValues.DEFAULT_ADMIN_FIRST_NAME;
319 String middleName = PropsValues.DEFAULT_ADMIN_MIDDLE_NAME;
320 String lastName = PropsValues.DEFAULT_ADMIN_LAST_NAME;
321 int prefixId = 0;
322 int suffixId = 0;
323 boolean male = true;
324 int birthdayMonth = Calendar.JANUARY;
325 int birthdayDay = 1;
326 int birthdayYear = 1970;
327 String jobTitle = StringPool.BLANK;
328
329 Group guestGroup = groupLocalService.getGroup(
330 companyId, GroupConstants.GUEST);
331
332 long[] groupIds = new long[] {guestGroup.getGroupId()};
333
334 long[] organizationIds = null;
335
336 Role adminRole = roleLocalService.getRole(
337 companyId, RoleConstants.ADMINISTRATOR);
338
339 Role powerUserRole = roleLocalService.getRole(
340 companyId, RoleConstants.POWER_USER);
341
342 long[] roleIds = new long[] {
343 adminRole.getRoleId(), powerUserRole.getRoleId()
344 };
345
346 long[] userGroupIds = null;
347 boolean sendEmail = false;
348 ServiceContext serviceContext = new ServiceContext();
349
350 userLocalService.addUser(
351 creatorUserId, companyId, autoPassword, password1, password2,
352 autoScreenName, screenName, emailAddress, facebookId, openId,
353 locale, firstName, middleName, lastName, prefixId, suffixId,
354 male, birthdayMonth, birthdayDay, birthdayYear, jobTitle,
355 groupIds, organizationIds, roleIds, userGroupIds, sendEmail,
356 serviceContext);
357 }
358
359
360
361 portletLocalService.checkPortlets(companyId);
362
363 return company;
364 }
365
366 public void checkCompanyKey(long companyId)
367 throws PortalException, SystemException {
368
369 Company company = companyPersistence.findByPrimaryKey(companyId);
370
371 if ((Validator.isNull(company.getKey())) &&
372 (company.getKeyObj() == null)) {
373
374 try {
375 company.setKeyObj(Encryptor.generateKey());
376 }
377 catch (EncryptorException ee) {
378 throw new SystemException(ee);
379 }
380
381 companyPersistence.update(company, false);
382 }
383 }
384
385 public void deleteLogo(long companyId)
386 throws PortalException, SystemException {
387
388 Company company = companyPersistence.findByPrimaryKey(companyId);
389
390 long logoId = company.getLogoId();
391
392 if (logoId > 0) {
393 company.setLogoId(0);
394
395 companyPersistence.update(company, false);
396
397 imageLocalService.deleteImage(logoId);
398 }
399 }
400
401 public List<Company> getCompanies() throws SystemException {
402 return companyPersistence.findAll();
403 }
404
405 public List<Company> getCompanies(boolean system) throws SystemException {
406 return companyPersistence.findBySystem(system);
407 }
408
409 public int getCompaniesCount(boolean system) throws SystemException {
410 return companyPersistence.countBySystem(system);
411 }
412
413 public Company getCompanyById(long companyId)
414 throws PortalException, SystemException {
415
416 return companyPersistence.findByPrimaryKey(companyId);
417 }
418
419 public Company getCompanyByLogoId(long logoId)
420 throws PortalException, SystemException {
421
422 return companyPersistence.findByLogoId(logoId);
423 }
424
425 public Company getCompanyByMx(String mx)
426 throws PortalException, SystemException {
427
428 return companyPersistence.findByMx(mx);
429 }
430
431 public Company getCompanyByVirtualHost(String virtualHost)
432 throws PortalException, SystemException {
433
434 virtualHost = virtualHost.trim().toLowerCase();
435
436 return companyPersistence.findByVirtualHost(virtualHost);
437 }
438
439 public Company getCompanyByWebId(String webId)
440 throws PortalException, SystemException {
441
442 return companyPersistence.findByWebId(webId);
443 }
444
445 public void removePreferences(long companyId, String[] keys)
446 throws SystemException {
447
448 PortletPreferences preferences = PrefsPropsUtil.getPreferences(
449 companyId);
450
451 try {
452 for (String key : keys) {
453 preferences.reset(key);
454 }
455
456 preferences.store();
457 }
458 catch (Exception e) {
459 throw new SystemException(e);
460 }
461 }
462
463 public Hits search(
464 long companyId, long userId, String keywords, int start, int end)
465 throws SystemException {
466
467 return search(companyId, userId, null, 0, null, keywords, start, end);
468 }
469
470 public Hits search(
471 long companyId, long userId, String portletId, long groupId,
472 String type, String keywords, int start, int end)
473 throws SystemException {
474
475 try {
476 BooleanQuery contextQuery = BooleanQueryFactoryUtil.create();
477
478 contextQuery.addRequiredTerm(Field.COMPANY_ID, companyId);
479
480 if (Validator.isNotNull(portletId)) {
481 contextQuery.addRequiredTerm(Field.PORTLET_ID, portletId);
482 }
483
484 if (groupId > 0) {
485 contextQuery.addRequiredTerm(Field.GROUP_ID, groupId);
486 }
487
488 if (Validator.isNotNull(type)) {
489 contextQuery.addRequiredTerm(Field.TYPE, type);
490 }
491
492 BooleanQuery searchQuery = BooleanQueryFactoryUtil.create();
493
494 searchQuery.addTerms(_KEYWORDS_FIELDS, keywords);
495
496 BooleanQuery fullQuery = BooleanQueryFactoryUtil.create();
497
498 fullQuery.add(contextQuery, BooleanClauseOccur.MUST);
499
500 if (searchQuery.clauses().size() > 0) {
501 fullQuery.add(searchQuery, BooleanClauseOccur.MUST);
502 }
503
504 return SearchEngineUtil.search(
505 companyId, new long[] {groupId}, userId, null, fullQuery, start,
506 end);
507 }
508 catch (Exception e) {
509 throw new SystemException(e);
510 }
511 }
512
513 public Company updateCompany(
514 long companyId, String virtualHost, String mx, int maxUsers)
515 throws PortalException, SystemException {
516
517 virtualHost = virtualHost.trim().toLowerCase();
518
519 Company company = companyPersistence.findByPrimaryKey(companyId);
520
521 validate(company.getWebId(), virtualHost, mx);
522
523 company.setVirtualHost(virtualHost);
524
525 if (PropsValues.MAIL_MX_UPDATE) {
526 company.setMx(mx);
527 }
528
529 company.setMaxUsers(maxUsers);
530
531 companyPersistence.update(company, false);
532
533 return company;
534 }
535
536 public Company updateCompany(
537 long companyId, String virtualHost, String mx, String homeURL,
538 String name, String legalName, String legalId, String legalType,
539 String sicCode, String tickerSymbol, String industry, String type,
540 String size)
541 throws PortalException, SystemException {
542
543
544
545 virtualHost = virtualHost.trim().toLowerCase();
546 Date now = new Date();
547
548 Company company = companyPersistence.findByPrimaryKey(companyId);
549
550 validate(company.getWebId(), virtualHost, mx);
551 validate(name);
552
553 company.setVirtualHost(virtualHost);
554
555 if (PropsValues.MAIL_MX_UPDATE) {
556 company.setMx(mx);
557 }
558
559 company.setHomeURL(homeURL);
560
561 companyPersistence.update(company, false);
562
563
564
565 Account account = accountPersistence.fetchByPrimaryKey(
566 company.getAccountId());
567
568 if (account == null) {
569 long accountId = counterLocalService.increment();
570
571 account = accountPersistence.create(accountId);
572
573 account.setCreateDate(now);
574 account.setCompanyId(companyId);
575 account.setUserId(0);
576 account.setUserName(StringPool.BLANK);
577
578 company.setAccountId(accountId);
579
580 companyPersistence.update(company, false);
581 }
582
583 account.setModifiedDate(now);
584 account.setName(name);
585 account.setLegalName(legalName);
586 account.setLegalId(legalId);
587 account.setLegalType(legalType);
588 account.setSicCode(sicCode);
589 account.setTickerSymbol(tickerSymbol);
590 account.setIndustry(industry);
591 account.setType(type);
592 account.setSize(size);
593
594 accountPersistence.update(account, false);
595
596 return company;
597 }
598
599 public void updateDisplay(
600 long companyId, String languageId, String timeZoneId)
601 throws PortalException, SystemException {
602
603 User user = userLocalService.getDefaultUser(companyId);
604
605 user.setLanguageId(languageId);
606 user.setTimeZoneId(timeZoneId);
607
608 userPersistence.update(user, false);
609 }
610
611 public void updateLogo(long companyId, byte[] bytes)
612 throws PortalException, SystemException {
613
614 long logoId = getLogoId(companyId);
615
616 imageLocalService.updateImage(logoId, bytes);
617 }
618
619 public void updateLogo(long companyId, File file)
620 throws PortalException, SystemException {
621
622 long logoId = getLogoId(companyId);
623
624 imageLocalService.updateImage(logoId, file);
625 }
626
627 public void updateLogo(long companyId, InputStream is)
628 throws PortalException, SystemException {
629
630 long logoId = getLogoId(companyId);
631
632 imageLocalService.updateImage(logoId, is);
633 }
634
635 public void updatePreferences(long companyId, UnicodeProperties properties)
636 throws SystemException {
637
638 PortletPreferences preferences = PrefsPropsUtil.getPreferences(
639 companyId);
640
641 try {
642 if (properties.containsKey(PropsKeys.LOCALES)) {
643 String oldLocales = preferences.getValue(
644 PropsKeys.LOCALES, StringPool.BLANK);
645 String newLocales = properties.getProperty(PropsKeys.LOCALES);
646
647 if (!Validator.equals(oldLocales, newLocales)) {
648 LanguageUtil.resetAvailableLocales(companyId);
649 }
650 }
651
652 for (String key : properties.keySet()) {
653 String value = properties.getProperty(key);
654
655 if (!value.equals(Portal.TEMP_OBFUSCATION_VALUE)) {
656 preferences.setValue(key, value);
657 }
658 }
659
660 preferences.store();
661 }
662 catch (Exception e) {
663 throw new SystemException(e);
664 }
665 }
666
667 public void updateSecurity(
668 long companyId, String authType, boolean autoLogin,
669 boolean sendPassword, boolean strangers, boolean strangersWithMx,
670 boolean strangersVerify, boolean communityLogo)
671 throws SystemException {
672
673 PortletPreferences preferences = PrefsPropsUtil.getPreferences(
674 companyId);
675
676 try {
677 preferences.setValue(
678 PropsKeys.COMPANY_SECURITY_AUTH_TYPE, authType);
679 preferences.setValue(
680 PropsKeys.COMPANY_SECURITY_AUTO_LOGIN,
681 String.valueOf(autoLogin));
682 preferences.setValue(
683 PropsKeys.COMPANY_SECURITY_SEND_PASSWORD,
684 String.valueOf(sendPassword));
685 preferences.setValue(
686 PropsKeys.COMPANY_SECURITY_STRANGERS,
687 String.valueOf(strangers));
688 preferences.setValue(
689 PropsKeys.COMPANY_SECURITY_STRANGERS_WITH_MX,
690 String.valueOf(strangersWithMx));
691 preferences.setValue(
692 PropsKeys.COMPANY_SECURITY_STRANGERS_VERIFY,
693 String.valueOf(strangersVerify));
694 preferences.setValue(
695 PropsKeys.COMPANY_SECURITY_COMMUNITY_LOGO,
696 String.valueOf(communityLogo));
697
698 preferences.store();
699 }
700 catch (IOException ioe) {
701 throw new SystemException(ioe);
702 }
703 catch (PortletException pe) {
704 throw new SystemException(pe);
705 }
706 }
707
708 protected long getLogoId(long companyId)
709 throws PortalException, SystemException {
710
711 Company company = companyPersistence.findByPrimaryKey(companyId);
712
713 long logoId = company.getLogoId();
714
715 if (logoId <= 0) {
716 logoId = counterLocalService.increment();
717
718 company.setLogoId(logoId);
719
720 companyPersistence.update(company, false);
721 }
722
723 return logoId;
724 }
725
726 protected void validate(String name) throws PortalException {
727 if (Validator.isNull(name)) {
728 throw new AccountNameException();
729 }
730 }
731
732 protected void validate(String webId, String virtualHost, String mx)
733 throws PortalException, SystemException {
734
735 if (Validator.isNull(virtualHost)) {
736 throw new CompanyVirtualHostException();
737 }
738 else if (virtualHost.equals(_DEFAULT_VIRTUAL_HOST) &&
739 !webId.equals(PropsValues.COMPANY_DEFAULT_WEB_ID)) {
740
741 throw new CompanyVirtualHostException();
742 }
743 else if (!Validator.isDomain(virtualHost)) {
744 throw new CompanyVirtualHostException();
745 }
746 else {
747 try {
748 Company virtualHostCompany = getCompanyByVirtualHost(
749 virtualHost);
750
751 if ((virtualHostCompany != null) &&
752 (!virtualHostCompany.getWebId().equals(webId))) {
753
754 throw new CompanyVirtualHostException();
755 }
756 }
757 catch (NoSuchCompanyException nsce) {
758 }
759
760 try {
761 layoutSetLocalService.getLayoutSet(virtualHost);
762
763 throw new CompanyVirtualHostException();
764 }
765 catch (NoSuchLayoutSetException nslse) {
766 }
767 }
768
769 if (Validator.isNull(mx)) {
770 throw new CompanyMxException();
771 }
772 else if (!Validator.isDomain(mx)) {
773 throw new CompanyMxException();
774 }
775 }
776
777 private static final String _DEFAULT_VIRTUAL_HOST = "localhost";
778
779 private static final String[] _KEYWORDS_FIELDS = {
780 Field.ASSET_TAG_NAMES, Field.CONTENT, Field.DESCRIPTION,
781 Field.PROPERTIES, Field.TITLE
782 };
783
784 }