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.DuplicateOrganizationException;
024: import com.liferay.portal.NoSuchOrganizationException;
025: import com.liferay.portal.OrganizationNameException;
026: import com.liferay.portal.OrganizationParentException;
027: import com.liferay.portal.PortalException;
028: import com.liferay.portal.RequiredOrganizationException;
029: import com.liferay.portal.SystemException;
030: import com.liferay.portal.kernel.util.OrderByComparator;
031: import com.liferay.portal.kernel.util.StringPool;
032: import com.liferay.portal.kernel.util.Validator;
033: import com.liferay.portal.model.Group;
034: import com.liferay.portal.model.Location;
035: import com.liferay.portal.model.Organization;
036: import com.liferay.portal.model.Role;
037: import com.liferay.portal.model.User;
038: import com.liferay.portal.model.impl.ListTypeImpl;
039: import com.liferay.portal.model.impl.OrganizationImpl;
040: import com.liferay.portal.model.impl.ResourceImpl;
041: import com.liferay.portal.model.impl.RoleImpl;
042: import com.liferay.portal.security.permission.PermissionCacheUtil;
043: import com.liferay.portal.service.base.OrganizationLocalServiceBaseImpl;
044: import com.liferay.portal.util.PropsValues;
045: import com.liferay.portal.util.comparator.OrganizationNameComparator;
046: import com.liferay.util.UniqueList;
047:
048: import java.rmi.RemoteException;
049:
050: import java.util.ArrayList;
051: import java.util.Iterator;
052: import java.util.LinkedHashMap;
053: import java.util.List;
054:
055: /**
056: * <a href="OrganizationLocalServiceImpl.java.html"><b><i>View Source</i></b>
057: * </a>
058: *
059: * @author Brian Wing Shun Chan
060: * @author Jorge Ferrer
061: *
062: */
063: public class OrganizationLocalServiceImpl extends
064: OrganizationLocalServiceBaseImpl {
065:
066: public void addGroupOrganizations(long groupId,
067: long[] organizationIds) throws PortalException,
068: SystemException {
069:
070: groupPersistence.addOrganizations(groupId, organizationIds);
071:
072: PermissionCacheUtil.clearCache();
073: }
074:
075: public Organization addOrganization(long userId,
076: long parentOrganizationId, String name, int type,
077: boolean recursable, long regionId, long countryId,
078: int statusId, String comments) throws PortalException,
079: SystemException {
080:
081: // Organization
082:
083: User user = userPersistence.findByPrimaryKey(userId);
084: parentOrganizationId = getParentOrganizationId(user
085: .getCompanyId(), parentOrganizationId);
086: recursable = true;
087:
088: validate(user.getCompanyId(), parentOrganizationId, name, type,
089: countryId, statusId);
090:
091: long organizationId = counterLocalService.increment();
092:
093: Organization organization = organizationPersistence
094: .create(organizationId);
095:
096: organization.setCompanyId(user.getCompanyId());
097: organization.setParentOrganizationId(parentOrganizationId);
098: organization.setName(name);
099:
100: if (type == OrganizationImpl.TYPE_LOCATION) {
101: organization.setLocation(true);
102: } else {
103: organization.setLocation(false);
104: }
105:
106: organization.setRecursable(recursable);
107: organization.setRegionId(regionId);
108: organization.setCountryId(countryId);
109: organization.setStatusId(statusId);
110: organization.setComments(comments);
111:
112: organizationPersistence.update(organization);
113:
114: // Group
115:
116: Group group = groupLocalService.addGroup(userId,
117: Organization.class.getName(), organizationId, null,
118: null, 0, null, true);
119:
120: // Role
121:
122: Role role = roleLocalService.getRole(organization
123: .getCompanyId(), RoleImpl.ORGANIZATION_OWNER);
124:
125: userGroupRoleLocalService.addUserGroupRoles(userId, group
126: .getGroupId(), new long[] { role.getRoleId() });
127:
128: // User
129:
130: userPersistence.addOrganization(userId, organizationId);
131:
132: // Resources
133:
134: addOrganizationResources(userId, organization);
135:
136: return organization;
137: }
138:
139: public void addOrganizationResources(long userId,
140: Organization organization) throws PortalException,
141: SystemException {
142:
143: String name = Organization.class.getName();
144:
145: if (organization.isLocation()) {
146: name = Location.class.getName();
147: }
148:
149: resourceLocalService.addResources(organization.getCompanyId(),
150: 0, userId, name, organization.getOrganizationId(),
151: false, false, false);
152: }
153:
154: public void addPasswordPolicyOrganizations(long passwordPolicyId,
155: long[] organizationIds) throws PortalException,
156: SystemException {
157:
158: passwordPolicyRelLocalService.addPasswordPolicyRels(
159: passwordPolicyId, Organization.class.getName(),
160: organizationIds);
161: }
162:
163: public void deleteOrganization(long organizationId)
164: throws PortalException, SystemException {
165:
166: Organization organization = organizationPersistence
167: .findByPrimaryKey(organizationId);
168:
169: deleteOrganization(organization);
170: }
171:
172: public void deleteOrganization(Organization organization)
173: throws PortalException, SystemException {
174:
175: if ((organizationPersistence.containsUsers(organization
176: .getOrganizationId()))
177: || (organizationPersistence.countByC_P(organization
178: .getCompanyId(), organization
179: .getOrganizationId()) > 0)) {
180:
181: throw new RequiredOrganizationException();
182: }
183:
184: // Addresses
185:
186: addressLocalService.deleteAddresses(
187: organization.getCompanyId(), Organization.class
188: .getName(), organization.getOrganizationId());
189:
190: // Email addresses
191:
192: emailAddressLocalService.deleteEmailAddresses(organization
193: .getCompanyId(), Organization.class.getName(),
194: organization.getOrganizationId());
195:
196: // Password policy relation
197:
198: passwordPolicyRelLocalService.deletePasswordPolicyRel(
199: Organization.class.getName(), organization
200: .getOrganizationId());
201:
202: // Phone
203:
204: phoneLocalService.deletePhones(organization.getCompanyId(),
205: Organization.class.getName(), organization
206: .getOrganizationId());
207:
208: // Website
209:
210: websiteLocalService.deleteWebsites(organization.getCompanyId(),
211: Organization.class.getName(), organization
212: .getOrganizationId());
213:
214: // Group
215:
216: Group group = organization.getGroup();
217:
218: groupLocalService.deleteGroup(group.getGroupId());
219:
220: // Resources
221:
222: String name = Organization.class.getName();
223:
224: if (organization.isLocation()) {
225: name = Location.class.getName();
226: }
227:
228: resourceLocalService.deleteResource(
229: organization.getCompanyId(), name,
230: ResourceImpl.SCOPE_INDIVIDUAL, organization
231: .getOrganizationId());
232:
233: // Organization
234:
235: organizationPersistence
236: .remove(organization.getOrganizationId());
237:
238: // Permission cache
239:
240: PermissionCacheUtil.clearCache();
241: }
242:
243: public List getGroupOrganizations(long groupId)
244: throws PortalException, SystemException {
245:
246: return groupPersistence.getOrganizations(groupId);
247: }
248:
249: /**
250: * Gets a list of organizations that a user has access to administrate. This
251: * includes organizations that a user belongs to and all suborganizations of
252: * those organizations.
253: *
254: * @param userId the user id of the user
255: * @return a list of organizations
256: */
257: public List getManageableOrganizations(long userId)
258: throws PortalException, SystemException {
259:
260: List manageableOrganizations = new UniqueList();
261:
262: List userOrganizations = userPersistence
263: .getOrganizations(userId);
264:
265: manageableOrganizations.addAll(userOrganizations);
266: manageableOrganizations
267: .addAll(getSuborganizations(userOrganizations));
268:
269: return manageableOrganizations;
270: }
271:
272: public Organization getOrganization(long organizationId)
273: throws PortalException, SystemException {
274:
275: return organizationPersistence.findByPrimaryKey(organizationId);
276: }
277:
278: public Organization getOrganization(long companyId, String name)
279: throws PortalException, SystemException {
280:
281: return organizationPersistence.findByC_N(companyId, name);
282: }
283:
284: public long getOrganizationId(long companyId, String name)
285: throws PortalException, SystemException {
286:
287: try {
288: Organization organization = organizationPersistence
289: .findByC_N(companyId, name);
290:
291: return organization.getOrganizationId();
292: } catch (NoSuchOrganizationException nsoge) {
293: return 0;
294: }
295: }
296:
297: public List getOrganizations(long[] organizationIds)
298: throws PortalException, SystemException {
299:
300: List organizations = new ArrayList();
301:
302: for (int i = 0; i < organizationIds.length; i++) {
303: Organization organization = getOrganization(organizationIds[i]);
304:
305: organizations.add(organization);
306: }
307:
308: return organizations;
309: }
310:
311: public List getParentOrganizations(long organizationId)
312: throws PortalException, SystemException {
313:
314: if (organizationId == OrganizationImpl.DEFAULT_PARENT_ORGANIZATION_ID) {
315: return new ArrayList();
316: }
317:
318: Organization organization = organizationPersistence
319: .findByPrimaryKey(organizationId);
320:
321: return getParentOrganizations(organization, true);
322: }
323:
324: public List getSuborganizations(List organizations)
325: throws SystemException {
326:
327: List allSuborganizations = new ArrayList();
328:
329: for (int i = 0; i < organizations.size(); i++) {
330: Organization organization = (Organization) organizations
331: .get(i);
332:
333: List suborganizations = organizationPersistence.findByC_P(
334: organization.getCompanyId(), organization
335: .getOrganizationId());
336:
337: addSuborganizations(allSuborganizations, suborganizations);
338: }
339:
340: return allSuborganizations;
341: }
342:
343: public List getSubsetOrganizations(List allOrganizations,
344: List availableOrganizations) throws PortalException,
345: SystemException {
346:
347: List subsetOrganizations = new ArrayList();
348:
349: Iterator itr = allOrganizations.iterator();
350:
351: while (itr.hasNext()) {
352: Organization organization = (Organization) itr.next();
353:
354: if (availableOrganizations.contains(organization)) {
355: subsetOrganizations.add(organization);
356: }
357: }
358:
359: return subsetOrganizations;
360: }
361:
362: public List getUserOrganizations(long userId)
363: throws PortalException, SystemException {
364:
365: return userPersistence.getOrganizations(userId);
366: }
367:
368: public int getUserOrganizationsCount(long userId)
369: throws PortalException, SystemException {
370:
371: return userPersistence.getOrganizationsSize(userId);
372: }
373:
374: public boolean hasGroupOrganization(long groupId,
375: long organizationId) throws PortalException,
376: SystemException {
377:
378: return groupPersistence.containsOrganization(groupId,
379: organizationId);
380: }
381:
382: public boolean hasUserOrganization(long userId, long organizationId)
383: throws PortalException, SystemException {
384:
385: return userPersistence.containsOrganization(userId,
386: organizationId);
387: }
388:
389: public boolean hasPasswordPolicyOrganization(long passwordPolicyId,
390: long organizationId) throws PortalException,
391: SystemException {
392:
393: return passwordPolicyRelLocalService.hasPasswordPolicyRel(
394: passwordPolicyId, Organization.class.getName(),
395: organizationId);
396: }
397:
398: public List search(long companyId, long parentOrganizationId,
399: String keywords, int type, Long regionId, Long countryId,
400: LinkedHashMap params, int begin, int end)
401: throws PortalException, SystemException {
402:
403: return search(companyId, parentOrganizationId, keywords, type,
404: regionId, countryId, params, begin, end,
405: new OrganizationNameComparator(true));
406: }
407:
408: public List search(long companyId, long parentOrganizationId,
409: String keywords, int type, Long regionId, Long countryId,
410: LinkedHashMap params, int begin, int end,
411: OrderByComparator obc) throws PortalException,
412: SystemException {
413:
414: String parentOrganizationComparator = StringPool.EQUAL;
415:
416: if (parentOrganizationId == OrganizationImpl.ANY_PARENT_ORGANIZATION_ID) {
417:
418: parentOrganizationComparator = StringPool.NOT_EQUAL;
419: }
420:
421: return organizationFinder.findByKeywords(companyId,
422: parentOrganizationId, parentOrganizationComparator,
423: keywords, type, regionId, countryId, params, begin,
424: end, obc);
425: }
426:
427: public List search(long companyId, long parentOrganizationId,
428: String name, int type, String street, String city,
429: String zip, Long regionId, Long countryId,
430: LinkedHashMap params, boolean andOperator, int begin,
431: int end) throws PortalException, SystemException {
432:
433: return search(companyId, parentOrganizationId, name, type,
434: street, city, zip, regionId, countryId, params,
435: andOperator, begin, end,
436: new OrganizationNameComparator(true));
437: }
438:
439: public List search(long companyId, long parentOrganizationId,
440: String name, int type, String street, String city,
441: String zip, Long regionId, Long countryId,
442: LinkedHashMap params, boolean andOperator, int begin,
443: int end, OrderByComparator obc) throws PortalException,
444: SystemException {
445:
446: String parentOrganizationComparator = StringPool.EQUAL;
447:
448: if (parentOrganizationId == OrganizationImpl.ANY_PARENT_ORGANIZATION_ID) {
449:
450: parentOrganizationComparator = StringPool.NOT_EQUAL;
451: }
452:
453: return organizationFinder.findByC_PO_N_T_S_C_Z_R_C(companyId,
454: parentOrganizationId, parentOrganizationComparator,
455: name, type, street, city, zip, regionId, countryId,
456: params, andOperator, begin, end, obc);
457: }
458:
459: public int searchCount(long companyId, long parentOrganizationId,
460: String keywords, int type, Long regionId, Long countryId,
461: LinkedHashMap params) throws PortalException,
462: SystemException {
463:
464: String parentOrganizationComparator = StringPool.EQUAL;
465:
466: if (parentOrganizationId == OrganizationImpl.ANY_PARENT_ORGANIZATION_ID) {
467:
468: parentOrganizationComparator = StringPool.NOT_EQUAL;
469: }
470:
471: return organizationFinder.countByKeywords(companyId,
472: parentOrganizationId, parentOrganizationComparator,
473: keywords, type, regionId, countryId, params);
474: }
475:
476: public int searchCount(long companyId, long parentOrganizationId,
477: String name, int type, String street, String city,
478: String zip, Long regionId, Long countryId,
479: LinkedHashMap params, boolean andOperator)
480: throws PortalException, SystemException {
481:
482: String parentOrganizationComparator = StringPool.EQUAL;
483:
484: if (parentOrganizationId == OrganizationImpl.ANY_PARENT_ORGANIZATION_ID) {
485:
486: parentOrganizationComparator = StringPool.NOT_EQUAL;
487: }
488:
489: return organizationFinder.countByC_PO_N_T_S_C_Z_R_C(companyId,
490: parentOrganizationId, parentOrganizationComparator,
491: name, type, street, city, zip, regionId, countryId,
492: params, andOperator);
493: }
494:
495: public void setGroupOrganizations(long groupId,
496: long[] organizationIds) throws PortalException,
497: SystemException {
498:
499: groupPersistence.setOrganizations(groupId, organizationIds);
500:
501: PermissionCacheUtil.clearCache();
502: }
503:
504: public void unsetGroupOrganizations(long groupId,
505: long[] organizationIds) throws PortalException,
506: SystemException {
507:
508: groupPersistence.removeOrganizations(groupId, organizationIds);
509:
510: PermissionCacheUtil.clearCache();
511: }
512:
513: public void unsetPasswordPolicyOrganizations(long passwordPolicyId,
514: long[] organizationIds) throws PortalException,
515: SystemException {
516:
517: passwordPolicyRelLocalService.deletePasswordPolicyRels(
518: passwordPolicyId, Organization.class.getName(),
519: organizationIds);
520: }
521:
522: public Organization updateOrganization(long companyId,
523: long organizationId, long parentOrganizationId,
524: String name, int type, boolean recursable, long regionId,
525: long countryId, int statusId, String comments)
526: throws PortalException, SystemException {
527:
528: parentOrganizationId = getParentOrganizationId(companyId,
529: parentOrganizationId);
530: recursable = true;
531:
532: validate(companyId, organizationId, parentOrganizationId, name,
533: type, countryId, statusId);
534:
535: Organization organization = organizationPersistence
536: .findByPrimaryKey(organizationId);
537:
538: organization.setParentOrganizationId(parentOrganizationId);
539: organization.setName(name);
540:
541: if (type == OrganizationImpl.TYPE_LOCATION) {
542: organization.setLocation(true);
543: } else {
544: organization.setLocation(false);
545: }
546:
547: organization.setRecursable(recursable);
548: organization.setRegionId(regionId);
549: organization.setCountryId(countryId);
550: organization.setStatusId(statusId);
551: organization.setComments(comments);
552:
553: organizationPersistence.update(organization);
554:
555: return organization;
556: }
557:
558: protected void addSuborganizations(List allSuborganizations,
559: List organizations) throws SystemException {
560:
561: for (int i = 0; i < organizations.size(); i++) {
562: Organization organization = (Organization) organizations
563: .get(i);
564:
565: if (!allSuborganizations.contains(organization)) {
566: allSuborganizations.add(organization);
567:
568: List suborganizations = organizationPersistence
569: .findByC_P(organization.getCompanyId(),
570: organization.getOrganizationId());
571:
572: addSuborganizations(allSuborganizations,
573: suborganizations);
574: }
575: }
576: }
577:
578: protected long getParentOrganizationId(long companyId,
579: long parentOrganizationId) throws PortalException,
580: SystemException {
581:
582: if (parentOrganizationId != OrganizationImpl.DEFAULT_PARENT_ORGANIZATION_ID) {
583:
584: // Ensure parent organization exists and belongs to the proper
585: // company
586:
587: try {
588: Organization parentOrganization = organizationPersistence
589: .findByPrimaryKey(parentOrganizationId);
590:
591: if (companyId != parentOrganization.getCompanyId()) {
592: parentOrganizationId = OrganizationImpl.DEFAULT_PARENT_ORGANIZATION_ID;
593: }
594: } catch (NoSuchOrganizationException nsoe) {
595: parentOrganizationId = OrganizationImpl.DEFAULT_PARENT_ORGANIZATION_ID;
596: }
597: }
598:
599: return parentOrganizationId;
600: }
601:
602: protected List getParentOrganizations(Organization organization,
603: boolean lastOrganization) throws PortalException,
604: SystemException {
605:
606: List organizations = new ArrayList();
607:
608: if (!lastOrganization) {
609: organizations.add(organization);
610: }
611:
612: long parentOrganizationId = organization
613: .getParentOrganizationId();
614:
615: if (parentOrganizationId == OrganizationImpl.DEFAULT_PARENT_ORGANIZATION_ID) {
616:
617: return organizations;
618: }
619:
620: Organization parentOrganization = organizationPersistence
621: .findByPrimaryKey(parentOrganizationId);
622:
623: List parentOrganizatons = getParentOrganizations(
624: parentOrganization, false);
625:
626: organizations.addAll(parentOrganizatons);
627:
628: return organizations;
629: }
630:
631: protected boolean isParentOrganization(long parentOrganizationId,
632: long organizationId) throws PortalException,
633: SystemException {
634:
635: // Return true if parentOrganizationId is among the parent organizatons
636: // of organizationId
637:
638: Organization parentOrganization = organizationPersistence
639: .findByPrimaryKey(parentOrganizationId);
640:
641: List parentOrganizations = getParentOrganizations(organizationId);
642:
643: if (parentOrganizations.contains(parentOrganization)) {
644: return true;
645: } else {
646: return false;
647: }
648: }
649:
650: protected void validate(long companyId, long parentOrganizationId,
651: String name, int type, long countryId, int statusId)
652: throws PortalException, SystemException {
653:
654: validate(companyId, 0, parentOrganizationId, name, type,
655: countryId, statusId);
656: }
657:
658: protected void validate(long companyId, long organizationId,
659: long parentOrganizationId, String name, int type,
660: long countryId, int statusId) throws PortalException,
661: SystemException {
662:
663: if ((type == OrganizationImpl.TYPE_LOCATION)
664: || (parentOrganizationId != OrganizationImpl.DEFAULT_PARENT_ORGANIZATION_ID)) {
665:
666: try {
667: Organization parentOrganization = organizationPersistence
668: .findByPrimaryKey(parentOrganizationId);
669:
670: if ((companyId != parentOrganization.getCompanyId())
671: || (parentOrganizationId == organizationId)
672: || (parentOrganization.isLocation())) {
673:
674: throw new OrganizationParentException();
675: }
676: } catch (NoSuchOrganizationException nsoe) {
677: throw new OrganizationParentException();
678: }
679: }
680:
681: if ((organizationId > 0)
682: && (parentOrganizationId != OrganizationImpl.DEFAULT_PARENT_ORGANIZATION_ID)) {
683:
684: // Prevent circular organizational references
685:
686: if (isParentOrganization(organizationId,
687: parentOrganizationId)) {
688: throw new OrganizationParentException();
689: }
690: }
691:
692: if (Validator.isNull(name)) {
693: throw new OrganizationNameException();
694: } else {
695: try {
696: Organization organization = organizationPersistence
697: .findByC_N(companyId, name);
698:
699: if (organization.getName().equalsIgnoreCase(name)) {
700: if ((organizationId <= 0)
701: || (organization.getOrganizationId() != organizationId)) {
702:
703: throw new DuplicateOrganizationException();
704: }
705: }
706: } catch (NoSuchOrganizationException nsoe) {
707: }
708: }
709:
710: try {
711: if ((countryId > 0)
712: || PropsValues.ORGANIZATIONS_COUNTRY_REQUIRED) {
713: countryPersistence.findByPrimaryKey(countryId);
714: }
715:
716: listTypeService.validate(statusId,
717: ListTypeImpl.ORGANIZATION_STATUS);
718: } catch (RemoteException re) {
719: throw new SystemException(re);
720: }
721: }
722:
723: }
|