001: /**
002: * Copyright (c) 2000-2008 Liferay, Inc. All rights reserved.
003: *
004: * Permission is hereby granted, free of charge, to any person obtaining a copy
005: * of this software and associated documentation files (the "Software"), to deal
006: * in the Software without restriction, including without limitation the rights
007: * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
008: * copies of the Software, and to permit persons to whom the Software is
009: * furnished to do so, subject to the following conditions:
010: *
011: * The above copyright notice and this permission notice shall be included in
012: * all copies or substantial portions of the Software.
013: *
014: * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
015: * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
016: * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
017: * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
018: * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
019: * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
020: * SOFTWARE.
021: */package com.liferay.portal.service.impl;
022:
023: import com.liferay.portal.AccountNameException;
024: import com.liferay.portal.CompanyMxException;
025: import com.liferay.portal.CompanyVirtualHostException;
026: import com.liferay.portal.CompanyWebIdException;
027: import com.liferay.portal.NoSuchAccountException;
028: import com.liferay.portal.NoSuchCompanyException;
029: import com.liferay.portal.NoSuchLayoutSetException;
030: import com.liferay.portal.NoSuchUserException;
031: import com.liferay.portal.PortalException;
032: import com.liferay.portal.SystemException;
033: import com.liferay.portal.kernel.search.Hits;
034: import com.liferay.portal.kernel.util.StringPool;
035: import com.liferay.portal.kernel.util.Validator;
036: import com.liferay.portal.lucene.LuceneFields;
037: import com.liferay.portal.lucene.LuceneUtil;
038: import com.liferay.portal.model.Account;
039: import com.liferay.portal.model.Company;
040: import com.liferay.portal.model.Contact;
041: import com.liferay.portal.model.Group;
042: import com.liferay.portal.model.Organization;
043: import com.liferay.portal.model.Role;
044: import com.liferay.portal.model.User;
045: import com.liferay.portal.model.impl.CompanyImpl;
046: import com.liferay.portal.model.impl.ContactImpl;
047: import com.liferay.portal.model.impl.CountryImpl;
048: import com.liferay.portal.model.impl.GroupImpl;
049: import com.liferay.portal.model.impl.ListTypeImpl;
050: import com.liferay.portal.model.impl.OrganizationImpl;
051: import com.liferay.portal.model.impl.RegionImpl;
052: import com.liferay.portal.model.impl.RoleImpl;
053: import com.liferay.portal.service.base.CompanyLocalServiceBaseImpl;
054: import com.liferay.portal.util.PortalInstances;
055: import com.liferay.portal.util.PrefsPropsUtil;
056: import com.liferay.portal.util.PropsUtil;
057: import com.liferay.portal.util.PropsValues;
058: import com.liferay.util.Encryptor;
059: import com.liferay.util.EncryptorException;
060: import com.liferay.util.Normalizer;
061: import com.liferay.util.lucene.HitsImpl;
062:
063: import java.io.File;
064: import java.io.IOException;
065:
066: import java.util.Calendar;
067: import java.util.Date;
068: import java.util.List;
069: import java.util.Locale;
070:
071: import javax.portlet.PortletException;
072: import javax.portlet.PortletPreferences;
073:
074: import org.apache.lucene.search.BooleanClause;
075: import org.apache.lucene.search.BooleanQuery;
076: import org.apache.lucene.search.Searcher;
077:
078: /**
079: * <a href="CompanyLocalServiceImpl.java.html"><b><i>View Source</i></b></a>
080: *
081: * @author Brian Wing Shun Chan
082: *
083: */
084: public class CompanyLocalServiceImpl extends
085: CompanyLocalServiceBaseImpl {
086:
087: public Company addCompany(String webId, String virtualHost,
088: String mx) throws PortalException, SystemException {
089:
090: // Company
091:
092: virtualHost = getVirtualHost(virtualHost);
093:
094: if ((Validator.isNull(webId))
095: || (webId.equals(CompanyImpl.DEFAULT_WEB_ID))
096: || (companyPersistence.fetchByWebId(webId) != null)) {
097:
098: throw new CompanyWebIdException();
099: }
100:
101: validate(webId, virtualHost, mx);
102:
103: Company company = checkCompany(webId, mx);
104:
105: company.setVirtualHost(virtualHost);
106: company.setMx(mx);
107:
108: companyPersistence.update(company);
109:
110: // Lucene
111:
112: LuceneUtil.checkLuceneDir(company.getCompanyId());
113:
114: return company;
115: }
116:
117: public Company checkCompany(String webId) throws PortalException,
118: SystemException {
119:
120: String mx = webId;
121:
122: return checkCompany(webId, mx);
123: }
124:
125: public Company checkCompany(String webId, String mx)
126: throws PortalException, SystemException {
127:
128: // Company
129:
130: Date now = new Date();
131:
132: Company company = companyPersistence.fetchByWebId(webId);
133:
134: if (company == null) {
135: String virtualHost = webId;
136:
137: if (webId.equals(CompanyImpl.DEFAULT_WEB_ID)) {
138: virtualHost = PortalInstances.DEFAULT_VIRTUAL_HOST;
139: }
140:
141: String name = webId;
142: String legalName = null;
143: String legalId = null;
144: String legalType = null;
145: String sicCode = null;
146: String tickerSymbol = null;
147: String industry = null;
148: String type = null;
149: String size = null;
150:
151: long companyId = counterLocalService.increment();
152:
153: company = companyPersistence.create(companyId);
154:
155: try {
156: company.setKeyObj(Encryptor.generateKey());
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);
166:
167: updateCompany(companyId, virtualHost, mx, name, legalName,
168: legalId, legalType, sicCode, tickerSymbol,
169: industry, type, size);
170:
171: // Demo settings
172:
173: if (webId.equals("liferay.net")) {
174: company = companyPersistence.findByWebId(webId);
175:
176: company.setVirtualHost("demo.liferay.net");
177:
178: companyPersistence.update(company);
179:
180: updateSecurity(companyId, CompanyImpl.AUTH_TYPE_EA,
181: true, true, true, true, false, true);
182:
183: PortletPreferences prefs = PrefsPropsUtil
184: .getPreferences(companyId);
185:
186: try {
187: prefs.setValue(PropsUtil.ADMIN_EMAIL_FROM_NAME,
188: "Liferay Demo");
189: prefs.setValue(PropsUtil.ADMIN_EMAIL_FROM_ADDRESS,
190: "test@liferay.net");
191:
192: prefs.store();
193: } catch (IOException ioe) {
194: throw new SystemException(ioe);
195: } catch (PortletException pe) {
196: throw new SystemException(pe);
197: }
198: }
199: }
200:
201: long companyId = company.getCompanyId();
202:
203: // Key
204:
205: checkCompanyKey(companyId);
206:
207: // Default user
208:
209: User defaultUser = null;
210:
211: try {
212: defaultUser = userLocalService.getDefaultUser(companyId);
213:
214: if (!defaultUser.isAgreedToTermsOfUse()) {
215: defaultUser.setAgreedToTermsOfUse(true);
216:
217: userPersistence.update(defaultUser);
218: }
219: } catch (NoSuchUserException nsue) {
220: long userId = counterLocalService.increment();
221:
222: defaultUser = userPersistence.create(userId);
223:
224: defaultUser.setCompanyId(companyId);
225: defaultUser.setCreateDate(now);
226: defaultUser.setModifiedDate(now);
227: defaultUser.setDefaultUser(true);
228: defaultUser.setContactId(counterLocalService.increment());
229: defaultUser.setPassword("password");
230: defaultUser.setScreenName(String.valueOf(defaultUser
231: .getUserId()));
232: defaultUser.setEmailAddress("default@" + company.getMx());
233: defaultUser.setLanguageId(null);
234: defaultUser.setTimeZoneId(null);
235: defaultUser.setGreeting("Welcome!");
236: defaultUser.setLoginDate(now);
237: defaultUser.setFailedLoginAttempts(0);
238: defaultUser.setAgreedToTermsOfUse(true);
239: defaultUser.setActive(true);
240:
241: userPersistence.update(defaultUser);
242:
243: // Contact
244:
245: Contact defaultContact = contactPersistence
246: .create(defaultUser.getContactId());
247:
248: defaultContact.setCompanyId(defaultUser.getCompanyId());
249: defaultContact.setUserId(defaultUser.getUserId());
250: defaultContact.setUserName(StringPool.BLANK);
251: defaultContact.setCreateDate(now);
252: defaultContact.setModifiedDate(now);
253: defaultContact.setAccountId(company.getAccountId());
254: defaultContact
255: .setParentContactId(ContactImpl.DEFAULT_PARENT_CONTACT_ID);
256: defaultContact.setFirstName(StringPool.BLANK);
257: defaultContact.setMiddleName(StringPool.BLANK);
258: defaultContact.setLastName(StringPool.BLANK);
259: defaultContact.setMale(true);
260: defaultContact.setBirthday(now);
261:
262: contactPersistence.update(defaultContact);
263: }
264:
265: // System groups
266:
267: groupLocalService.checkSystemGroups(companyId);
268:
269: // Default password policy
270:
271: passwordPolicyLocalService
272: .checkDefaultPasswordPolicy(companyId);
273:
274: // System roles
275:
276: roleLocalService.checkSystemRoles(companyId);
277:
278: // Default user must have the Guest role
279:
280: Role guestRole = roleLocalService.getRole(companyId,
281: RoleImpl.GUEST);
282:
283: roleLocalService.setUserRoles(defaultUser.getUserId(),
284: new long[] { guestRole.getRoleId() });
285:
286: // Default admin
287:
288: if (userPersistence.countByCompanyId(companyId) == 1) {
289: long creatorUserId = 0;
290: boolean autoPassword = false;
291: String password1 = PropsValues.DEFAULT_ADMIN_PASSWORD;
292: String password2 = password1;
293: boolean autoScreenName = false;
294: String screenName = PropsValues.DEFAULT_ADMIN_SCREEN_NAME;
295: String emailAddress = PropsValues.DEFAULT_ADMIN_EMAIL_ADDRESS_PREFIX
296: + "@" + mx;
297: Locale locale = defaultUser.getLocale();
298: String firstName = PropsValues.DEFAULT_ADMIN_FIRST_NAME;
299: String middleName = PropsValues.DEFAULT_ADMIN_MIDDLE_NAME;
300: String lastName = PropsValues.DEFAULT_ADMIN_LAST_NAME;
301: int prefixId = 0;
302: int suffixId = 0;
303: boolean male = true;
304: int birthdayMonth = Calendar.JANUARY;
305: int birthdayDay = 1;
306: int birthdayYear = 1970;
307: String jobTitle = StringPool.BLANK;
308: long[] organizationIds = new long[0];
309:
310: User user = userLocalService.addUser(creatorUserId,
311: companyId, autoPassword, password1, password2,
312: autoScreenName, screenName, emailAddress, locale,
313: firstName, middleName, lastName, prefixId,
314: suffixId, male, birthdayMonth, birthdayDay,
315: birthdayYear, jobTitle, organizationIds, false);
316:
317: Group guestGroup = groupLocalService.getGroup(companyId,
318: GroupImpl.GUEST);
319:
320: long[] groupIds = new long[] { guestGroup.getGroupId() };
321:
322: groupLocalService.addUserGroups(user.getUserId(), groupIds);
323:
324: Role adminRole = roleLocalService.getRole(companyId,
325: RoleImpl.ADMINISTRATOR);
326:
327: Role powerUserRole = roleLocalService.getRole(companyId,
328: RoleImpl.POWER_USER);
329:
330: long[] roleIds = new long[] { adminRole.getRoleId(),
331: powerUserRole.getRoleId() };
332:
333: roleLocalService.setUserRoles(user.getUserId(), roleIds);
334:
335: Organization organization = organizationLocalService
336: .addOrganization(
337: user.getUserId(),
338: OrganizationImpl.DEFAULT_PARENT_ORGANIZATION_ID,
339: "Test Organization",
340: OrganizationImpl.TYPE_REGULAR, true,
341: RegionImpl.DEFAULT_REGION_ID,
342: CountryImpl.DEFAULT_COUNTRY_ID,
343: ListTypeImpl.ORGANIZATION_STATUS_DEFAULT,
344: StringPool.BLANK);
345:
346: organizationLocalService.addOrganization(user.getUserId(),
347: organization.getOrganizationId(), "Test Location",
348: OrganizationImpl.TYPE_LOCATION, true,
349: RegionImpl.DEFAULT_REGION_ID,
350: CountryImpl.DEFAULT_COUNTRY_ID,
351: ListTypeImpl.ORGANIZATION_STATUS_DEFAULT,
352: StringPool.BLANK);
353: }
354:
355: return company;
356: }
357:
358: public void checkCompanyKey(long companyId) throws PortalException,
359: SystemException {
360:
361: Company company = companyPersistence
362: .findByPrimaryKey(companyId);
363:
364: if (company.getKeyObj() == null) {
365: try {
366: company.setKeyObj(Encryptor.generateKey());
367: } catch (EncryptorException ee) {
368: throw new SystemException(ee);
369: }
370: }
371:
372: companyPersistence.update(company);
373: }
374:
375: public List getCompanies() throws SystemException {
376: return companyPersistence.findAll();
377: }
378:
379: public Company getCompanyById(long companyId)
380: throws PortalException, SystemException {
381:
382: return companyPersistence.findByPrimaryKey(companyId);
383: }
384:
385: public Company getCompanyByLogoId(long logoId)
386: throws PortalException, SystemException {
387:
388: return companyPersistence.findByLogoId(logoId);
389: }
390:
391: public Company getCompanyByMx(String mx) throws PortalException,
392: SystemException {
393:
394: return companyPersistence.findByMx(mx);
395: }
396:
397: public Company getCompanyByVirtualHost(String virtualHost)
398: throws PortalException, SystemException {
399:
400: virtualHost = getVirtualHost(virtualHost);
401:
402: return companyPersistence.findByVirtualHost(virtualHost);
403: }
404:
405: public Company getCompanyByWebId(String webId)
406: throws PortalException, SystemException {
407:
408: return companyPersistence.findByWebId(webId);
409: }
410:
411: public Hits search(long companyId, String keywords)
412: throws SystemException {
413:
414: return search(companyId, null, 0, null, keywords);
415: }
416:
417: public Hits search(long companyId, String portletId, long groupId,
418: String type, String keywords) throws SystemException {
419:
420: Searcher searcher = null;
421:
422: try {
423: HitsImpl hits = new HitsImpl();
424:
425: BooleanQuery contextQuery = new BooleanQuery();
426:
427: LuceneUtil.addRequiredTerm(contextQuery,
428: LuceneFields.COMPANY_ID, companyId);
429:
430: if (Validator.isNotNull(portletId)) {
431: LuceneUtil.addRequiredTerm(contextQuery,
432: LuceneFields.PORTLET_ID, portletId);
433: }
434:
435: if (groupId > 0) {
436: LuceneUtil.addRequiredTerm(contextQuery,
437: LuceneFields.GROUP_ID, groupId);
438: }
439:
440: if (Validator.isNotNull(type)) {
441: LuceneUtil.addRequiredTerm(contextQuery,
442: LuceneFields.TYPE, type);
443: }
444:
445: BooleanQuery searchQuery = new BooleanQuery();
446:
447: if (Validator.isNotNull(keywords)) {
448: LuceneUtil.addTerm(searchQuery, LuceneFields.TITLE,
449: keywords);
450: LuceneUtil.addTerm(searchQuery, LuceneFields.CONTENT,
451: keywords);
452: LuceneUtil.addTerm(searchQuery,
453: LuceneFields.DESCRIPTION, keywords);
454: LuceneUtil.addTerm(searchQuery,
455: LuceneFields.PROPERTIES, keywords);
456: LuceneUtil.addTerm(searchQuery, LuceneFields.TAG_ENTRY,
457: keywords);
458: }
459:
460: BooleanQuery fullQuery = new BooleanQuery();
461:
462: fullQuery.add(contextQuery, BooleanClause.Occur.MUST);
463:
464: if (searchQuery.clauses().size() > 0) {
465: fullQuery.add(searchQuery, BooleanClause.Occur.MUST);
466: }
467:
468: searcher = LuceneUtil.getSearcher(companyId);
469:
470: hits.recordHits(searcher.search(fullQuery), searcher);
471:
472: return hits;
473: } catch (Exception e) {
474: return LuceneUtil.closeSearcher(searcher, keywords, e);
475: }
476: }
477:
478: public Company updateCompany(long companyId, String virtualHost,
479: String mx) throws PortalException, SystemException {
480:
481: virtualHost = getVirtualHost(virtualHost);
482:
483: Company company = companyPersistence
484: .findByPrimaryKey(companyId);
485:
486: validate(company.getWebId(), virtualHost, mx);
487:
488: company.setVirtualHost(virtualHost);
489:
490: if (PropsValues.MAIL_MX_UPDATE) {
491: company.setMx(mx);
492: }
493:
494: companyPersistence.update(company);
495:
496: return company;
497: }
498:
499: public Company updateCompany(long companyId, String virtualHost,
500: String mx, String name, String legalName, String legalId,
501: String legalType, String sicCode, String tickerSymbol,
502: String industry, String type, String size)
503: throws PortalException, SystemException {
504:
505: // Company
506:
507: virtualHost = getVirtualHost(virtualHost);
508: Date now = new Date();
509:
510: Company company = companyPersistence
511: .findByPrimaryKey(companyId);
512:
513: validate(company.getWebId(), virtualHost, mx);
514: validate(name);
515:
516: company.setVirtualHost(virtualHost);
517:
518: if (PropsValues.MAIL_MX_UPDATE) {
519: company.setMx(mx);
520: }
521:
522: companyPersistence.update(company);
523:
524: // Account
525:
526: Account account = null;
527:
528: try {
529: account = accountPersistence.findByPrimaryKey(company
530: .getAccountId());
531: } catch (NoSuchAccountException nsae) {
532: long accountId = counterLocalService.increment();
533:
534: account = accountPersistence.create(accountId);
535:
536: account.setCreateDate(now);
537: account.setCompanyId(companyId);
538: account.setUserId(0);
539: account.setUserName(StringPool.BLANK);
540:
541: company.setAccountId(accountId);
542:
543: companyPersistence.update(company);
544: }
545:
546: account.setModifiedDate(now);
547: account.setName(name);
548: account.setLegalName(legalName);
549: account.setLegalId(legalId);
550: account.setLegalType(legalType);
551: account.setSicCode(sicCode);
552: account.setTickerSymbol(tickerSymbol);
553: account.setIndustry(industry);
554: account.setType(type);
555: account.setSize(size);
556:
557: accountPersistence.update(account);
558:
559: return company;
560: }
561:
562: public void updateDisplay(long companyId, String languageId,
563: String timeZoneId) throws PortalException, SystemException {
564:
565: User user = userLocalService.getDefaultUser(companyId);
566:
567: user.setLanguageId(languageId);
568: user.setTimeZoneId(timeZoneId);
569:
570: userPersistence.update(user);
571: }
572:
573: public void updateLogo(long companyId, File file)
574: throws PortalException, SystemException {
575:
576: Company company = companyPersistence
577: .findByPrimaryKey(companyId);
578:
579: long logoId = company.getLogoId();
580:
581: if (logoId <= 0) {
582: logoId = counterLocalService.increment();
583:
584: company.setLogoId(logoId);
585: }
586:
587: ImageLocalUtil.updateImage(logoId, file);
588: }
589:
590: public void updateSecurity(long companyId, String authType,
591: boolean autoLogin, boolean sendPassword, boolean strangers,
592: boolean strangersWithMx, boolean strangersVerify,
593: boolean communityLogo) throws PortalException,
594: SystemException {
595:
596: PortletPreferences prefs = PrefsPropsUtil
597: .getPreferences(companyId);
598:
599: try {
600: prefs.setValue(PropsUtil.COMPANY_SECURITY_AUTH_TYPE,
601: authType);
602: prefs.setValue(PropsUtil.COMPANY_SECURITY_AUTO_LOGIN,
603: String.valueOf(autoLogin));
604: prefs.setValue(PropsUtil.COMPANY_SECURITY_SEND_PASSWORD,
605: String.valueOf(sendPassword));
606: prefs.setValue(PropsUtil.COMPANY_SECURITY_STRANGERS, String
607: .valueOf(strangers));
608: prefs.setValue(
609: PropsUtil.COMPANY_SECURITY_STRANGERS_WITH_MX,
610: String.valueOf(strangersWithMx));
611: prefs.setValue(PropsUtil.COMPANY_SECURITY_STRANGERS_VERIFY,
612: String.valueOf(strangersVerify));
613: prefs.setValue(PropsUtil.COMPANY_SECURITY_COMMUNITY_LOGO,
614: String.valueOf(communityLogo));
615:
616: prefs.store();
617: } catch (IOException ioe) {
618: throw new SystemException(ioe);
619: } catch (PortletException pe) {
620: throw new SystemException(pe);
621: }
622: }
623:
624: protected String getVirtualHost(String virtualHost) {
625: return Normalizer.normalizeToAscii(virtualHost.trim()
626: .toLowerCase());
627: }
628:
629: protected void validate(String name) throws PortalException {
630: if (Validator.isNull(name)) {
631: throw new AccountNameException();
632: }
633: }
634:
635: protected void validate(String webId, String virtualHost, String mx)
636: throws PortalException, SystemException {
637:
638: if (Validator.isNull(virtualHost)) {
639: throw new CompanyVirtualHostException();
640: } else if (virtualHost
641: .equals(PortalInstances.DEFAULT_VIRTUAL_HOST)
642: && !webId.equals(CompanyImpl.DEFAULT_WEB_ID)) {
643:
644: throw new CompanyVirtualHostException();
645: } else {
646: try {
647: Company virtualHostCompany = getCompanyByVirtualHost(virtualHost);
648:
649: if (!virtualHostCompany.getWebId().equals(webId)) {
650: throw new CompanyVirtualHostException();
651: }
652: } catch (NoSuchCompanyException nsce) {
653: }
654:
655: try {
656: layoutSetLocalService.getLayoutSet(virtualHost);
657:
658: throw new CompanyVirtualHostException();
659: } catch (NoSuchLayoutSetException nslse) {
660: }
661: }
662:
663: if (Validator.isNull(mx)) {
664: throw new CompanyMxException();
665: }
666: }
667:
668: }
|