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