0001: /*
0002: * Copyright (c) 2001 - 2005 ivata limited.
0003: * All rights reserved.
0004: * -----------------------------------------------------------------------------
0005: * ivata groupware may be redistributed under the GNU General Public
0006: * License as published by the Free Software Foundation;
0007: * version 2 of the License.
0008: *
0009: * These programs are free software; you can redistribute them and/or
0010: * modify them under the terms of the GNU General Public License
0011: * as published by the Free Software Foundation; version 2 of the License.
0012: *
0013: * These programs are distributed in the hope that they will be useful,
0014: * but WITHOUT ANY WARRANTY; without even the implied warranty of
0015: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
0016: *
0017: * See the GNU General Public License in the file LICENSE.txt for more
0018: * details.
0019: *
0020: * If you would like a copy of the GNU General Public License write to
0021: *
0022: * Free Software Foundation, Inc.
0023: * 59 Temple Place - Suite 330
0024: * Boston, MA 02111-1307, USA.
0025: *
0026: *
0027: * To arrange commercial support and licensing, contact ivata at
0028: * http://www.ivata.com/contact.jsp
0029: * -----------------------------------------------------------------------------
0030: * $Log: AddressBookImpl.java,v $
0031: * Revision 1.11 2005/10/12 18:36:10 colinmacleod
0032: * Standardized format of Logger declaration - to make it easier to find instances
0033: * which are not both static and final.
0034: *
0035: * Revision 1.10 2005/10/11 18:53:07 colinmacleod
0036: * Fixed some checkstyle and javadoc issues.
0037: *
0038: * Revision 1.9 2005/10/03 10:21:13 colinmacleod
0039: * Fixed some style and javadoc issues.
0040: *
0041: * Revision 1.8 2005/10/02 14:08:55 colinmacleod
0042: * Added/improved log4j logging.
0043: *
0044: * Revision 1.7 2005/09/29 14:17:01 colinmacleod
0045: * Split UserGroupDO off from GroupDO.
0046: * Moved UserGroupDO, Right classes to security subproject (from
0047: * addressbook).
0048: * Centralized user right handling into Rights and RightsImpl.
0049: *
0050: * Revision 1.6 2005/09/14 14:50:14 colinmacleod
0051: * Removed unused local and class variables.
0052: * Added serialVersionUID.
0053: *
0054: * Revision 1.5 2005/04/29 02:48:14 colinmacleod
0055: * Data bugfixes.
0056: * Changed primary key back to Integer.
0057: *
0058: * Revision 1.4 2005/04/28 18:47:10 colinmacleod
0059: * Fixed XHMTL, styles and resin compatibility.
0060: * Added support for URL rewriting.
0061: *
0062: * Revision 1.3 2005/04/10 20:09:34 colinmacleod
0063: * Added new themes.
0064: * Changed id type to String.
0065: * Changed i tag to em and b tag to strong.
0066: * Improved PicoContainerFactory with NanoContainer scripts.
0067: *
0068: * Revision 1.2 2005/04/09 17:19:06 colinmacleod
0069: * Changed copyright text to GPL v2 explicitly.
0070: *
0071: * Revision 1.1.1.1 2005/03/10 17:50:18 colinmacleod
0072: * Restructured ivata op around Hibernate/PicoContainer.
0073: * Renamed ivata groupware.
0074: *
0075: * Revision 1.7 2004/11/12 18:17:10 colinmacleod
0076: * Ordered imports.
0077: *
0078: * Revision 1.6 2004/11/12 15:56:47 colinmacleod
0079: * Removed dependencies on SSLEXT.
0080: * Moved Persistence classes to ivata masks.
0081: *
0082: * Revision 1.5 2004/11/03 15:36:48 colinmacleod
0083: * Changed relationship between person and address:
0084: * each person for now has exactly one address.
0085: *
0086: * Revision 1.4 2004/07/18 22:30:25 colinmacleod
0087: * Synchronized lists and collections.
0088: *
0089: * Revision 1.3 2004/07/18 21:59:13 colinmacleod
0090: * Removed Person from User - now you need to use addressbook/persistence
0091: * manager to find the person (makes the app run faster.)
0092: *
0093: * Revision 1.2 2004/07/13 19:41:13 colinmacleod
0094: * Moved project to POJOs from EJBs.
0095: * Applied PicoContainer to services layer (replacing session EJBs).
0096: * Applied Hibernate to persistence layer (replacing entity EJBs).
0097: *
0098: * Revision 1.1 2004/03/26 21:34:35 colinmacleod
0099: * Split off functionality into POJO.
0100: * -----------------------------------------------------------------------------
0101: */
0102: package com.ivata.groupware.business.addressbook;
0103:
0104: import java.util.ArrayList;
0105: import java.util.Arrays;
0106: import java.util.Collections;
0107: import java.util.Iterator;
0108: import java.util.List;
0109: import java.util.Stack;
0110: import java.util.Vector;
0111:
0112: import org.apache.log4j.Logger;
0113:
0114: import com.ivata.groupware.admin.security.Security;
0115: import com.ivata.groupware.admin.security.server.SecuritySession;
0116: import com.ivata.groupware.admin.security.user.UserConstants;
0117: import com.ivata.groupware.admin.security.user.UserDO;
0118: import com.ivata.groupware.admin.setting.Settings;
0119: import com.ivata.groupware.business.BusinessLogic;
0120: import com.ivata.groupware.business.addressbook.address.AddressDO;
0121: import com.ivata.groupware.business.addressbook.address.country.CountryDO;
0122: import com.ivata.groupware.business.addressbook.person.PersonDO;
0123: import com.ivata.groupware.business.addressbook.person.group.GroupConstants;
0124: import com.ivata.groupware.business.addressbook.person.group.GroupDO;
0125: import com.ivata.groupware.business.addressbook.telecomaddress.TelecomAddressConstants;
0126: import com.ivata.groupware.business.addressbook.telecomaddress.TelecomAddressDO;
0127: import com.ivata.groupware.container.persistence.TimestampDOHandling;
0128: import com.ivata.mask.Mask;
0129: import com.ivata.mask.MaskFactory;
0130: import com.ivata.mask.persistence.PersistenceException;
0131: import com.ivata.mask.persistence.PersistenceSession;
0132: import com.ivata.mask.persistence.QueryPersistenceManager;
0133: import com.ivata.mask.persistence.listener.RemovePersistenceListener;
0134: import com.ivata.mask.util.StringHandling;
0135: import com.ivata.mask.util.SystemException;
0136: import com.ivata.mask.validation.ValidationError;
0137: import com.ivata.mask.validation.ValidationErrors;
0138: import com.ivata.mask.validation.ValidationException;
0139: import com.ivata.mask.valueobject.ValueObject;
0140:
0141: /**
0142: * <p>
0143: * This is a <a href='http://en.wikipedia.org/wiki/POJO'>POJO</a> implmenetation
0144: * of the ivata groupware address book. It can be wrapped to provide an <a
0145: * href='http://en.wikipedia.org/wiki/Enterprise_JavaBeans'>EJB</a>.
0146: * </p>
0147: *
0148: * <copyDoc>Refer to {@link AddressBook}</copyDoc>
0149: *
0150: * @author Colin MacLeod
0151: * <a href='mailto:colin.macleod@ivata.com'>colin.macleod@ivata.com</a>
0152: * @since ivata groupware 0.9 (Mar 22, 2004)
0153: * @version $Revision: 1.11 $
0154: */
0155: public class AddressBookImpl extends BusinessLogic implements
0156: AddressBook, RemovePersistenceListener {
0157: /**
0158: * Logger for this class.
0159: */
0160: private static final Logger logger = Logger
0161: .getLogger(AddressBookImpl.class);
0162:
0163: /**
0164: * Serialization version (for <code>Serializable</code> interface).
0165: */
0166: private static final long serialVersionUID = 1L;
0167:
0168: /**
0169: * <copyDoc>Refer to {@link AddressBookImpl()</copyDoc>}.
0170: */
0171: private MaskFactory maskFactory;
0172:
0173: /**
0174: * <copyDoc>Refer to {@link AddressBookImpl()</copyDoc>}.
0175: */
0176: private QueryPersistenceManager persistenceManager;
0177:
0178: /**
0179: * <copyDoc>Refer to {@link AddressBookImpl()</copyDoc>}.
0180: */
0181: private Settings settings;
0182:
0183: /**
0184: * Construct a new address book.
0185: *
0186: * @param persistenceManagerParam used to store objects in db.
0187: * @param maskFactoryParam Used to retrieve the current mask in validation.
0188: * @param settingsParam System settings interface. Used to detect whether
0189: * or not the site is operating in demo version mode.
0190: */
0191: public AddressBookImpl(
0192: final QueryPersistenceManager persistenceManagerParam,
0193: final MaskFactory maskFactoryParam,
0194: final Settings settingsParam) {
0195: this .persistenceManager = persistenceManagerParam;
0196: this .maskFactory = maskFactoryParam;
0197: persistenceManagerParam.addRemoveListener(GroupDO.class, this );
0198: this .settings = settingsParam;
0199: }
0200:
0201: /**
0202: * {@inheritDoc}
0203: *
0204: * @param securitySession {@inheritDoc}
0205: * @param groupDO {@inheritDoc}
0206: * @return
0207: * @throws SystemException
0208: */
0209: public GroupDO addAddressBook(
0210: final SecuritySession securitySession, final GroupDO groupDO)
0211: throws SystemException {
0212: if (logger.isDebugEnabled()) {
0213: logger
0214: .debug("addAddressBook(SecuritySession securitySession = "
0215: + securitySession
0216: + ", GroupDO groupDO = "
0217: + groupDO + ") - start");
0218: }
0219:
0220: PersistenceSession persistenceSession = persistenceManager
0221: .openSession(securitySession);
0222:
0223: try {
0224: // before creating the group, check we have reasonable data
0225: ValidationErrors errors = validate(securitySession, groupDO);
0226:
0227: if (!errors.isEmpty()) {
0228: throw new ValidationException(errors);
0229: }
0230:
0231: GroupDO parent = (GroupDO) persistenceManager
0232: .findByPrimaryKey(persistenceSession,
0233: GroupDO.class,
0234: GroupConstants.ADDRESS_BOOK_PUBLIC);
0235: groupDO.setParent(parent);
0236:
0237: TimestampDOHandling.add(securitySession, groupDO);
0238: persistenceManager.add(persistenceSession, groupDO);
0239:
0240: if (logger.isDebugEnabled()) {
0241: logger.debug("addAddressBook - end - return value = "
0242: + groupDO);
0243: }
0244:
0245: return groupDO;
0246: } catch (Exception e) {
0247: logger.error("addAddressBook(SecuritySession, GroupDO)", e);
0248:
0249: persistenceSession.cancel();
0250: throw new SystemException(e);
0251: } finally {
0252: persistenceSession.close();
0253: }
0254: }
0255:
0256: /**
0257: * {@inheritDoc}
0258: *
0259: * @param securitySession {@inheritDoc}
0260: * @param groupDO {@inheritDoc}
0261: * @return
0262: * @throws SystemException
0263: */
0264: public GroupDO addGroup(final SecuritySession securitySession,
0265: final GroupDO groupDO) throws SystemException {
0266: if (logger.isDebugEnabled()) {
0267: logger.debug("addGroup(SecuritySession securitySession = "
0268: + securitySession + ", GroupDO groupDO = "
0269: + groupDO + ") - start");
0270: }
0271:
0272: PersistenceSession persistenceSession = persistenceManager
0273: .openSession(securitySession);
0274:
0275: try {
0276: // before creating the group, check we have reasonable data
0277: ValidationErrors errors = validate(securitySession, groupDO);
0278:
0279: if (!errors.isEmpty()) {
0280: throw new ValidationException(errors);
0281: }
0282:
0283: TimestampDOHandling.add(securitySession, groupDO);
0284:
0285: GroupDO returnGroupDO = (GroupDO) persistenceManager.add(
0286: persistenceSession, groupDO);
0287:
0288: if (logger.isDebugEnabled()) {
0289: logger
0290: .debug("addGroup(SecuritySession, GroupDO) - end - return value = "
0291: + returnGroupDO);
0292: }
0293:
0294: return returnGroupDO;
0295: } catch (Exception e) {
0296: logger.error("addGroup(SecuritySession, GroupDO)", e);
0297:
0298: persistenceSession.cancel();
0299: throw new SystemException(e);
0300: } finally {
0301: persistenceSession.close();
0302: }
0303: }
0304:
0305: /**
0306: * {@inheritDoc}
0307: *
0308: * @param securitySession {@inheritDoc}
0309: * @param person {@inheritDoc}
0310: * @return
0311: * @throws SystemException
0312: */
0313: public PersonDO addPerson(final SecuritySession securitySession,
0314: final PersonDO person) throws SystemException {
0315: if (logger.isDebugEnabled()) {
0316: logger.debug("addPerson(SecuritySession securitySession = "
0317: + securitySession + ", PersonDO person = " + person
0318: + ") - start");
0319: }
0320:
0321: PersistenceSession persistenceSession = persistenceManager
0322: .openSession(securitySession);
0323:
0324: try {
0325: // before creating the person, check we have reasonable data
0326: ValidationErrors errors = validate(securitySession, person);
0327:
0328: if (!errors.isEmpty()) {
0329: throw new ValidationException(errors);
0330: }
0331:
0332: TimestampDOHandling.add(securitySession, person);
0333:
0334: if (person.getTelecomAddresses() != null) {
0335: Iterator telecomAddressIterator = person
0336: .getTelecomAddresses().iterator();
0337:
0338: while (telecomAddressIterator.hasNext()) {
0339: TelecomAddressDO telecomAddress = (TelecomAddressDO) telecomAddressIterator
0340: .next();
0341:
0342: // only new addressses allowed!
0343: telecomAddress.setId(null);
0344: TimestampDOHandling.add(securitySession,
0345: telecomAddress);
0346: }
0347: }
0348:
0349: // not sure why I need to do this? should be automatic?
0350: if (person.getAddress() != null) {
0351: person.getAddress().setPerson(person);
0352: }
0353:
0354: PersonDO returnPersonDO = (PersonDO) persistenceManager
0355: .add(persistenceSession, person);
0356:
0357: if (logger.isDebugEnabled()) {
0358: logger.debug("addPerson - end - return value = "
0359: + returnPersonDO);
0360: }
0361:
0362: return returnPersonDO;
0363: } catch (Exception e) {
0364: logger.error("addPerson(SecuritySession, PersonDO)", e);
0365:
0366: persistenceSession.cancel();
0367: throw new SystemException(e);
0368: } finally {
0369: persistenceSession.close();
0370: }
0371: }
0372:
0373: /**
0374: * {@inheritDoc}
0375: *
0376: * @param securitySession {@inheritDoc}
0377: * @param groupDO {@inheritDoc}
0378: * @return
0379: * @throws SystemException
0380: */
0381: public GroupDO amendGroup(final SecuritySession securitySession,
0382: final GroupDO groupDO) throws SystemException {
0383: if (logger.isDebugEnabled()) {
0384: logger
0385: .debug("amendGroup(SecuritySession securitySession = "
0386: + securitySession
0387: + ", GroupDO groupDO = "
0388: + groupDO + ") - start");
0389: }
0390:
0391: PersistenceSession persistenceSession = persistenceManager
0392: .openSession(securitySession);
0393:
0394: try {
0395: // before changing the group, check we have reasonable data
0396: ValidationErrors errors = validate(securitySession, groupDO);
0397:
0398: if (!errors.isEmpty()) {
0399: throw new ValidationException(errors);
0400: }
0401:
0402: TimestampDOHandling.amend(securitySession, groupDO);
0403: persistenceManager.amend(persistenceSession, groupDO);
0404:
0405: if (logger.isDebugEnabled()) {
0406: logger.debug("amendGroup - end - return value = "
0407: + groupDO);
0408: }
0409:
0410: return groupDO;
0411: } catch (Exception e) {
0412: logger.error("amendGroup(SecuritySession, GroupDO)", e);
0413:
0414: persistenceSession.cancel();
0415: throw new SystemException(e);
0416: } finally {
0417: persistenceSession.close();
0418: }
0419: }
0420:
0421: /**
0422: * {@inheritDoc}
0423: *
0424: * @param securitySession {@inheritDoc}
0425: * @param person {@inheritDoc}
0426: * @return
0427: * @throws SystemException
0428: */
0429: public PersonDO amendPerson(final SecuritySession securitySession,
0430: final PersonDO person) throws SystemException {
0431: if (logger.isDebugEnabled()) {
0432: logger
0433: .debug("amendPerson(SecuritySession securitySession = "
0434: + securitySession
0435: + ", PersonDO person = "
0436: + person + ") - start");
0437: }
0438:
0439: PersistenceSession persistenceSession = persistenceManager
0440: .openSession(securitySession);
0441:
0442: try {
0443: // before creating the person, check we have reasonable data
0444: ValidationErrors errors = validate(securitySession, person);
0445:
0446: if (!errors.isEmpty()) {
0447: throw new ValidationException(errors);
0448: }
0449:
0450: TimestampDOHandling.amend(securitySession, person);
0451:
0452: if (person.getTelecomAddresses() != null) {
0453: Iterator telecomAddressIterator = person
0454: .getTelecomAddresses().iterator();
0455:
0456: while (telecomAddressIterator.hasNext()) {
0457: TelecomAddressDO telecomAddress = (TelecomAddressDO) telecomAddressIterator
0458: .next();
0459:
0460: if (telecomAddress.getId() == null) {
0461: TimestampDOHandling.add(securitySession,
0462: telecomAddress);
0463: } else {
0464: TimestampDOHandling.amend(securitySession,
0465: telecomAddress);
0466: }
0467: }
0468: }
0469:
0470: // not sure why I need to do this? should be automatic, shurely?
0471: if (person.getAddress() != null) {
0472: person.getAddress().setPerson(person);
0473: }
0474:
0475: // if the person is an employee but the country hasn't been set,
0476: // remove the country object
0477: if ((person.getEmployee() != null)
0478: && (person.getEmployee().getCountry() != null)
0479: && (person.getEmployee().getCountry().getId() == null)) {
0480: person.getEmployee().setCountry(null);
0481: }
0482:
0483: // if this person is a user, (s)he must be in the public address
0484: // book
0485: if ((person.getUser() != null)
0486: && !StringHandling.isNullOrEmpty(person.getUser()
0487: .getName())) {
0488: GroupDO addressBook = person.getGroup()
0489: .getAddressBook();
0490:
0491: if (!GroupConstants.equals(addressBook.getId(),
0492: GroupConstants.ADDRESS_BOOK_DEFAULT)) {
0493: GroupDO defaultAddressBook = findGroupByPrimaryKey(
0494: securitySession,
0495: GroupConstants.ADDRESS_BOOK_DEFAULT);
0496: person.setGroup(defaultAddressBook);
0497: }
0498: }
0499:
0500: persistenceManager.amend(persistenceSession, person);
0501:
0502: if (logger.isDebugEnabled()) {
0503: logger.debug("amendPerson - end - return value = "
0504: + person);
0505: }
0506:
0507: return person;
0508: } catch (Exception e) {
0509: logger.error("amendPerson(SecuritySession, PersonDO)", e);
0510:
0511: persistenceSession.cancel();
0512: throw new SystemException(e);
0513: } finally {
0514: persistenceSession.close();
0515: }
0516: }
0517:
0518: /**
0519: * {@inheritDoc}
0520: *
0521: * @param securitySession {@inheritDoc}
0522: * @param includePersonal {@inheritDoc}
0523: * @return
0524: * @throws SystemException
0525: */
0526: public List findAddressBooks(final SecuritySession securitySession,
0527: final boolean includePersonal) throws SystemException {
0528: if (logger.isDebugEnabled()) {
0529: logger
0530: .debug("findAddressBooks(SecuritySession securitySession = "
0531: + securitySession
0532: + ", boolean includePersonal = "
0533: + includePersonal + ") - start");
0534: }
0535:
0536: List addressBooks = new ArrayList();
0537: PersistenceSession persistenceSession = persistenceManager
0538: .openSession(securitySession);
0539:
0540: try {
0541: GroupDO addressBookParent = (GroupDO) persistenceManager
0542: .findByPrimaryKey(persistenceSession,
0543: GroupDO.class,
0544: GroupConstants.ADDRESS_BOOK_PUBLIC);
0545: List children = persistenceManager.find(persistenceSession,
0546: "addressBookGroupsInGroup",
0547: new Object[] { addressBookParent.getId() });
0548: addressBooks.addAll(children);
0549:
0550: // find personal addressBook for that user
0551: if (includePersonal) {
0552: GroupDO personalAddressBook = findPersonalAddressBook(securitySession);
0553: addressBooks.add(personalAddressBook);
0554: }
0555:
0556: List returnList = Collections
0557: .synchronizedList(addressBooks);
0558:
0559: if (logger.isDebugEnabled()) {
0560: logger.debug("findAddressBooks - end - return value = "
0561: + returnList);
0562: }
0563:
0564: return returnList;
0565: } catch (Exception e) {
0566: logger.error("findAddressBooks(SecuritySession, boolean)",
0567: e);
0568:
0569: persistenceSession.cancel();
0570: throw new SystemException(e);
0571: } finally {
0572: persistenceSession.close();
0573: }
0574: }
0575:
0576: /**
0577: * {@inheritDoc}
0578: *
0579: * @param securitySession {@inheritDoc}
0580: * @return
0581: * @throws SystemException
0582: */
0583: public List findAllCountries(final SecuritySession securitySession)
0584: throws SystemException {
0585: if (logger.isDebugEnabled()) {
0586: logger
0587: .debug("findAllCountries(SecuritySession securitySession = "
0588: + securitySession + ") - start");
0589: }
0590:
0591: PersistenceSession persistenceSession = persistenceManager
0592: .openSession(securitySession);
0593:
0594: try {
0595: List returnList = Collections
0596: .synchronizedList(persistenceManager.findAll(
0597: persistenceSession, CountryDO.class));
0598:
0599: if (logger.isDebugEnabled()) {
0600: logger
0601: .debug("findAllCountries(SecuritySession) - end - return value = "
0602: + returnList);
0603: }
0604:
0605: return returnList;
0606: } catch (Exception e) {
0607: logger.error("findAllCountries(SecuritySession)", e);
0608:
0609: persistenceSession.cancel();
0610: throw new SystemException(e);
0611: } finally {
0612: persistenceSession.close();
0613: }
0614: }
0615:
0616: /**
0617: * <p>
0618: * Recursive implementation to find all people in child groups.
0619: * </p>
0620: *
0621: * @param persistenceSession Current, open persistence session. Will be
0622: * re-used recursively to find all people in this and sub-groups.
0623: * @param group Parent group for which to find all people.
0624: * @param lowerCaseInitialLetter if not <code>null</code>, specifies a
0625: * filter used to limit the people returned.
0626: * @param totalResults Used to store all the results.
0627: * @param parentGroups Stores the parent groups, stacked in order.
0628: * @throws SystemException If there is a technical problem trying to
0629: * retrieve child groups.
0630: */
0631: private void findAllPeopleInGroup(
0632: final PersistenceSession persistenceSession,
0633: final GroupDO group, final String lowerCaseInitialLetter,
0634: final List totalResults, final Stack parentGroups)
0635: throws SystemException {
0636: if (logger.isDebugEnabled()) {
0637: logger
0638: .debug("findAllPeopleInGroup(PersistenceSession persistenceSession = "
0639: + persistenceSession
0640: + ", GroupDO group = "
0641: + group
0642: + ", String lowerCaseInitialLetter = "
0643: + lowerCaseInitialLetter
0644: + ", List totalResults = "
0645: + totalResults
0646: + ", Stack parentGroups = "
0647: + parentGroups
0648: + ") - start");
0649: }
0650:
0651: // go through the children first
0652: parentGroups.push(group);
0653:
0654: try {
0655: List children = persistenceManager.find(persistenceSession,
0656: "addressBookGroupsInGroup", new Object[] { group
0657: .getId() });
0658: Iterator childIterator = children.iterator();
0659:
0660: while (childIterator.hasNext()) {
0661: GroupDO child = (GroupDO) childIterator.next();
0662:
0663: if (parentGroups.contains(child)) {
0664: throw new ValidationException(
0665: new ValidationError(
0666: "errors.addressbook.group.parent",
0667: Arrays.asList(new Object[] { group,
0668: child })));
0669: }
0670:
0671: findAllPeopleInGroup(persistenceSession, child,
0672: lowerCaseInitialLetter, totalResults,
0673: parentGroups);
0674: }
0675: } catch (Exception e) {
0676: logger.error("Exception (" + e.getClass()
0677: + ") getting all poeple in group " + group, e);
0678:
0679: throw new SystemException(e);
0680: } finally {
0681: parentGroups.pop();
0682: }
0683:
0684: // iterate through all the people
0685: // in this group and only add those which match the first letter
0686: Iterator personIterator = group.getPeople().iterator();
0687:
0688: while (personIterator.hasNext()) {
0689: PersonDO person = (PersonDO) personIterator.next();
0690: String fileAs = person.getFileAs();
0691:
0692: if (StringHandling.isNullOrEmpty(fileAs)) {
0693: continue;
0694: }
0695:
0696: if ((lowerCaseInitialLetter != null)
0697: && !lowerCaseInitialLetter.equals(fileAs.substring(
0698: 0, 1).toLowerCase())) {
0699: continue;
0700: }
0701:
0702: totalResults.add(person);
0703: }
0704:
0705: if (logger.isDebugEnabled()) {
0706: logger.debug("findAllPeopleInGroup - end");
0707: }
0708: }
0709:
0710: /**
0711: * <p>Find All People in a specific address book group.</p>
0712: *
0713: * {@inheritDoc}
0714: *
0715: * @param securitySession {@inheritDoc}
0716: * @param group {@inheritDoc}
0717: * @param initialLetterParam {@inheritDoc}
0718: * @return
0719: * @throws SystemException
0720: */
0721: public List findAllPeopleInGroup(
0722: final SecuritySession securitySession, final GroupDO group,
0723: final String initialLetterParam) throws SystemException {
0724: if (logger.isDebugEnabled()) {
0725: logger
0726: .debug("findAllPeopleInGroup(SecuritySession securitySession = "
0727: + securitySession
0728: + ", GroupDO group = "
0729: + group
0730: + ", String initialLetterParam = "
0731: + initialLetterParam + ") - start");
0732: }
0733:
0734: boolean demoVersion = settings.getBooleanSetting(
0735: securitySession, "demoVersion", null).booleanValue();
0736: List results = new Vector();
0737: String initialLetterLowerCase = initialLetterParam;
0738:
0739: if (initialLetterLowerCase != null) {
0740: initialLetterLowerCase = initialLetterLowerCase
0741: .toLowerCase();
0742: }
0743:
0744: PersistenceSession persistenceSession = persistenceManager
0745: .openSession(securitySession);
0746:
0747: try {
0748: findAllPeopleInGroup(persistenceSession, group,
0749: initialLetterLowerCase, results, new Stack());
0750: } catch (Exception e) {
0751: logger
0752: .error(
0753: "findAllPeopleInGroup(SecuritySession, GroupDO, String)",
0754: e);
0755:
0756: persistenceSession.cancel();
0757: throw new SystemException(e);
0758: } finally {
0759: persistenceSession.close();
0760: }
0761:
0762: // this filters out all the people which are in private address
0763: // books - this should be removed when real user rights are used again
0764: // this filtering stage also removes logically deleted people - that
0765: // can probably stay in (depending on how the rights are implemented)
0766: List filteredResults = new Vector();
0767: Iterator peopleIterator = results.iterator();
0768:
0769: while (peopleIterator.hasNext()) {
0770: PersonDO person = (PersonDO) peopleIterator.next();
0771:
0772: // don't show the administator in the demo
0773: // TODO: this should really be handled via user rights
0774: if (demoVersion
0775: && UserConstants.ADMINISTRATOR.equals(person
0776: .getId())) {
0777: continue;
0778: }
0779:
0780: if (person.isDeleted()) {
0781: continue;
0782: }
0783:
0784: /*TODO
0785: if (!rights.canUser(securitySession, person.getGroup(),
0786: RightConstants.ACCESS_VIEW)) {
0787: continue;
0788: }*/
0789: filteredResults.add(person);
0790: }
0791:
0792: List returnList = Collections.synchronizedList(filteredResults);
0793:
0794: if (logger.isDebugEnabled()) {
0795: logger.debug("findAllPeopleInGroup - end - return value = "
0796: + returnList);
0797: }
0798:
0799: return returnList;
0800: }
0801:
0802: /**
0803: * {@inheritDoc}
0804: *
0805: * @param securitySession {@inheritDoc}
0806: * @param countryCode {@inheritDoc}
0807: * @return The country which matches this code.
0808: * @throws SystemException If the country cannot be found, or there is a
0809: * technical problem accessing the persistence store.
0810: */
0811: public CountryDO findCountryByCode(
0812: final SecuritySession securitySession,
0813: final String countryCode) throws SystemException {
0814: if (logger.isDebugEnabled()) {
0815: logger
0816: .debug("findCountryByCode(SecuritySession securitySession = "
0817: + securitySession
0818: + ", String countryCode = "
0819: + countryCode
0820: + ") - start");
0821: }
0822:
0823: PersistenceSession persistenceSession = persistenceManager
0824: .openSession(securitySession);
0825:
0826: try {
0827: CountryDO returnCountryDO = (CountryDO) persistenceManager
0828: .findInstance(persistenceSession,
0829: "addressBookCountryByCode",
0830: new Object[] { countryCode });
0831:
0832: if (logger.isDebugEnabled()) {
0833: logger
0834: .debug("findCountryByCode - end - return value = "
0835: + returnCountryDO);
0836: }
0837:
0838: return returnCountryDO;
0839: } catch (Exception e) {
0840: logger.error("findCountryByCode(SecuritySession, String)",
0841: e);
0842:
0843: persistenceSession.cancel();
0844: throw new SystemException(e);
0845: } finally {
0846: persistenceSession.close();
0847: }
0848: }
0849:
0850: /**
0851: * {@inheritDoc}
0852: *
0853: * @param securitySession {@inheritDoc}
0854: * @param id {@inheritDoc}
0855: * @return The group matching this unique identifier.
0856: * @throws SystemException If the group cannot be found, or there is a
0857: * technical problem accessing the persistence store.
0858: */
0859: public GroupDO findGroupByPrimaryKey(
0860: final SecuritySession securitySession, final Integer id)
0861: throws SystemException {
0862: if (logger.isDebugEnabled()) {
0863: logger
0864: .debug("findGroupByPrimaryKey(SecuritySession securitySession = "
0865: + securitySession
0866: + ", Integer id = "
0867: + id
0868: + ") - start");
0869: }
0870:
0871: PersistenceSession persistenceSession = persistenceManager
0872: .openSession(securitySession);
0873:
0874: try {
0875: GroupDO returnGroupDO = (GroupDO) persistenceManager
0876: .findByPrimaryKey(persistenceSession,
0877: GroupDO.class, id);
0878:
0879: if (logger.isDebugEnabled()) {
0880: logger
0881: .debug("findGroupByPrimaryKey - end - return value = "
0882: + returnGroupDO);
0883: }
0884:
0885: return returnGroupDO;
0886: } catch (Exception e) {
0887: logger.error(
0888: "findGroupByPrimaryKey(SecuritySession, Integer)",
0889: e);
0890:
0891: persistenceSession.cancel();
0892: throw new SystemException(e);
0893: } finally {
0894: persistenceSession.close();
0895: }
0896: }
0897:
0898: /**
0899: * {@inheritDoc}
0900: *
0901: * @param securitySession {@inheritDoc}
0902: * @param parentId {@inheritDoc}
0903: * @return a <code>List</code> containing all child groups.
0904: * @throws SystemException If there is a
0905: * technical problem accessing the persistence store.
0906: */
0907: public List findGroupsByParent(
0908: final SecuritySession securitySession,
0909: final Integer parentId) throws SystemException {
0910: if (logger.isDebugEnabled()) {
0911: logger
0912: .debug("findGroupsByParent(SecuritySession securitySession = "
0913: + securitySession
0914: + ", Integer parentId = "
0915: + parentId + ") - start");
0916: }
0917:
0918: PersistenceSession persistenceSession = persistenceManager
0919: .openSession(securitySession);
0920:
0921: try {
0922: List returnList = Collections
0923: .synchronizedList(persistenceManager.find(
0924: persistenceSession,
0925: "addressBookGroupsInGroup",
0926: new Object[] { parentId }));
0927:
0928: if (logger.isDebugEnabled()) {
0929: logger
0930: .debug("findGroupsByParent - end - return value = "
0931: + returnList);
0932: }
0933:
0934: return returnList;
0935: } catch (Exception e) {
0936: logger.error(
0937: "findGroupsByParent(SecuritySession, Integer)", e);
0938:
0939: persistenceSession.cancel();
0940: throw new SystemException(e);
0941: } finally {
0942: persistenceSession.close();
0943: }
0944: }
0945:
0946: /**
0947: * {@inheritDoc}
0948: *
0949: * @param securitySession {@inheritDoc}
0950: * @return {@inheritDoc}
0951: * @throws SystemException If there is a
0952: * technical problem accessing the persistence store.
0953: */
0954: public GroupDO findPersonalAddressBook(
0955: final SecuritySession securitySession)
0956: throws SystemException {
0957: if (logger.isDebugEnabled()) {
0958: logger
0959: .debug("findPersonalAddressBook(SecuritySession securitySession = "
0960: + securitySession + ") - start");
0961: }
0962:
0963: PersistenceSession persistenceSession = persistenceManager
0964: .openSession(securitySession);
0965:
0966: try {
0967: GroupDO returnGroupDO = (GroupDO) persistenceManager
0968: .findInstance(
0969: persistenceSession,
0970: "addressBookGroupsInGroupByName",
0971: new Object[] {
0972: GroupConstants.ADDRESS_BOOK_PRIVATE,
0973: securitySession.getUser().getName() });
0974:
0975: if (logger.isDebugEnabled()) {
0976: logger
0977: .debug("findPersonalAddressBook - end - return value = "
0978: + returnGroupDO);
0979: }
0980:
0981: return returnGroupDO;
0982: } catch (Exception e) {
0983: logger.error("findPersonalAddressBook(SecuritySession)", e);
0984:
0985: persistenceSession.cancel();
0986: throw new SystemException(e);
0987: } finally {
0988: persistenceSession.close();
0989: }
0990: }
0991:
0992: /**
0993: * {@inheritDoc}
0994: *
0995: * @param securitySession {@inheritDoc}
0996: * @param id {@inheritDoc}
0997: * @return {@inheritDoc}
0998: * @throws SystemException If there is a
0999: * technical problem accessing the persistence store.
1000: */
1001: public PersonDO findPersonByPrimaryKey(
1002: final SecuritySession securitySession, final String id)
1003: throws SystemException {
1004: if (logger.isDebugEnabled()) {
1005: logger
1006: .debug("findPersonByPrimaryKey(SecuritySession securitySession = "
1007: + securitySession
1008: + ", String id = "
1009: + id
1010: + ") - start");
1011: }
1012:
1013: PersistenceSession persistenceSession = persistenceManager
1014: .openSession(securitySession);
1015:
1016: try {
1017: PersonDO returnPersonDO = (PersonDO) persistenceManager
1018: .findByPrimaryKey(persistenceSession,
1019: PersonDO.class, id);
1020:
1021: if (logger.isDebugEnabled()) {
1022: logger
1023: .debug("findPersonByPrimaryKey - end - return value = "
1024: + returnPersonDO);
1025: }
1026:
1027: return returnPersonDO;
1028: } catch (Exception e) {
1029: logger.error(
1030: "findPersonByPrimaryKey(SecuritySession, String)",
1031: e);
1032:
1033: persistenceSession.cancel();
1034: throw new SystemException(e);
1035: } finally {
1036: persistenceSession.close();
1037: }
1038: }
1039:
1040: /**
1041: * {@inheritDoc}
1042: *
1043: * @param securitySession {@inheritDoc}
1044: * @param userName {@inheritDoc}
1045: * @return {@inheritDoc}
1046: * @throws SystemException {@inheritDoc}
1047: */
1048: public PersonDO findPersonByUserName(
1049: final SecuritySession securitySession, final String userName)
1050: throws SystemException {
1051: if (logger.isDebugEnabled()) {
1052: logger
1053: .debug("findPersonByUserName(SecuritySession securitySession = "
1054: + securitySession
1055: + ", String userName = "
1056: + userName + ") - start");
1057: }
1058:
1059: PersistenceSession persistenceSession = persistenceManager
1060: .openSession(securitySession);
1061:
1062: try {
1063: PersonDO returnPersonDO = (PersonDO) persistenceManager
1064: .findInstance(persistenceSession,
1065: "addressBookPersonByUserName",
1066: new Object[] { userName });
1067:
1068: if (logger.isDebugEnabled()) {
1069: logger
1070: .debug("findPersonByUserName - end - return value = "
1071: + returnPersonDO);
1072: }
1073:
1074: return returnPersonDO;
1075: } catch (Exception e) {
1076: logger.error(
1077: "findPersonByUserName(SecuritySession, String)", e);
1078:
1079: persistenceSession.cancel();
1080: throw new SystemException(e);
1081: } finally {
1082: persistenceSession.close();
1083: }
1084: }
1085:
1086: /**
1087: * {@inheritDoc}
1088: *
1089: * @param persistenceSession {@inheritDoc}
1090: * @param valueObject {@inheritDoc}
1091: */
1092: public void onRemove(final PersistenceSession persistenceSession,
1093: final ValueObject valueObject) throws PersistenceException {
1094: if (logger.isDebugEnabled()) {
1095: logger
1096: .debug("onRemove(PersistenceSession persistenceSession = "
1097: + persistenceSession
1098: + ", ValueObject valueObject = "
1099: + valueObject + ") - start");
1100: }
1101:
1102: // only interested in deleting groups - at least for now!
1103: if (!(valueObject instanceof GroupDO)) {
1104: if (logger.isDebugEnabled()) {
1105: logger
1106: .debug("onRemove(PersistenceSession, ValueObject) - end");
1107: }
1108:
1109: return;
1110: }
1111:
1112: GroupDO groupDO = (GroupDO) valueObject;
1113:
1114: // relocate everything in this group to the parent
1115: GroupDO parent = groupDO.getParent();
1116:
1117: List children = persistenceManager.find(persistenceSession,
1118: "addressBookGroupsInGroup", new Object[] { groupDO
1119: .getId() });
1120:
1121: // if this is an addressbook group, see if the group has any members
1122: if (GroupConstants.equals(groupDO.getParent().getId(),
1123: GroupConstants.ADDRESS_BOOK_PRIVATE)
1124: || GroupConstants.equals(groupDO.getParent().getId(),
1125: GroupConstants.ADDRESS_BOOK_PRIVATE)) {
1126: assert (children.size() > 0);
1127: assert (groupDO.getPeople().size() == 0);
1128: }
1129:
1130: // move all people and subgroups up a level
1131: Iterator childIterator = children.iterator();
1132:
1133: while (childIterator.hasNext()) {
1134: GroupDO child = (GroupDO) childIterator.next();
1135: child.setParent(parent);
1136: persistenceManager.amend(persistenceSession, child);
1137: }
1138:
1139: Iterator personIterator = groupDO.getPeople().iterator();
1140:
1141: while (personIterator.hasNext()) {
1142: PersonDO person = (PersonDO) personIterator.next();
1143: person.setGroup(parent);
1144: persistenceManager.amend(persistenceSession, person);
1145: }
1146:
1147: if (logger.isDebugEnabled()) {
1148: logger
1149: .debug("onRemove(PersistenceSession, ValueObject) - end");
1150: }
1151: }
1152:
1153: /**
1154: * {@inheritDoc}
1155: *
1156: * @param securitySession {@inheritDoc}
1157: * @param id {@inheritDoc}
1158: * @throws SystemException {@inheritDoc}
1159: */
1160: public void removeGroup(final SecuritySession securitySession,
1161: final Integer id) throws SystemException {
1162: if (logger.isDebugEnabled()) {
1163: logger
1164: .debug("removeGroup(SecuritySession securitySession = "
1165: + securitySession
1166: + ", Integer id = "
1167: + id
1168: + ") - start");
1169: }
1170:
1171: // check we have an addressBookID and Id of group
1172: assert (id != null);
1173:
1174: PersistenceSession persistenceSession = persistenceManager
1175: .openSession(securitySession);
1176:
1177: try {
1178: persistenceManager.remove(persistenceSession,
1179: GroupDO.class, id);
1180: } catch (Exception e) {
1181: logger.error("removeGroup(SecuritySession, Integer)", e);
1182:
1183: persistenceSession.cancel();
1184: throw new SystemException(e);
1185: } finally {
1186: persistenceSession.close();
1187: }
1188:
1189: if (logger.isDebugEnabled()) {
1190: logger.debug("removeGroup(SecuritySession, Integer) - end");
1191: }
1192: }
1193:
1194: /**
1195: * {@inheritDoc}
1196: *
1197: * @param securitySession {@inheritDoc}
1198: * @param id {@inheritDoc}
1199: * @throws SystemException {@inheritDoc}
1200: */
1201: public void removePerson(final SecuritySession securitySession,
1202: final Integer id) throws SystemException {
1203: if (logger.isDebugEnabled()) {
1204: logger
1205: .debug("removePerson(SecuritySession securitySession = "
1206: + securitySession
1207: + ", Integer id = "
1208: + id
1209: + ") - start");
1210: }
1211:
1212: // check we have an id
1213: if (id == null) {
1214: throw new SystemException(
1215: "ERROR in AddressBook: cannot remove a person with a null id");
1216: }
1217:
1218: PersistenceSession persistenceSession = persistenceManager
1219: .openSession(securitySession);
1220: PersonDO person;
1221:
1222: try {
1223: person = (PersonDO) persistenceManager.findByPrimaryKey(
1224: persistenceSession, PersonDO.class, id);
1225: } finally {
1226: persistenceSession.close();
1227: }
1228:
1229: persistenceSession = persistenceManager
1230: .openSession(securitySession);
1231:
1232: try {
1233: // if this person has a user, only logically remove
1234: if (person.getUser() == null) {
1235: persistenceManager.remove(persistenceSession, person);
1236: } else {
1237: person.setDeleted(true);
1238: person.getUser().setDeleted(true);
1239: persistenceManager.amend(persistenceSession, person);
1240: }
1241: } catch (Exception e) {
1242: logger.error("removePerson(SecuritySession, Integer)", e);
1243:
1244: persistenceSession.cancel();
1245: throw new SystemException(e);
1246: } finally {
1247: persistenceSession.close();
1248: }
1249:
1250: if (logger.isDebugEnabled()) {
1251: logger
1252: .debug("removePerson(SecuritySession, Integer) - end");
1253: }
1254: }
1255:
1256: /**
1257: * {@inheritDoc}
1258: *
1259: * @param securitySession {@inheritDoc}
1260: * @param addressDO {@inheritDoc}
1261: * @return {@inheritDoc}
1262: */
1263: public ValidationErrors validate(
1264: final SecuritySession securitySession,
1265: final AddressDO addressDO) {
1266: if (logger.isDebugEnabled()) {
1267: logger.debug("validate(SecuritySession securitySession = "
1268: + securitySession + ", AddressDO addressDO = "
1269: + addressDO + ") - start");
1270: }
1271:
1272: ValidationErrors errors = new ValidationErrors();
1273: Mask mask = maskFactory.getMask(AddressDO.class);
1274:
1275: if ((addressDO.getCountry() == null)
1276: || StringHandling.isNullOrEmpty(addressDO.getCountry()
1277: .getCode())) {
1278: errors.add(new ValidationError("person.address",
1279: AddressBook.BUNDLE_PATH, mask.getField("country"),
1280: "errors.required"));
1281: }
1282:
1283: if (StringHandling.isNullOrEmpty(addressDO.getStreetAddress())) {
1284: errors.add(new ValidationError("person.address",
1285: AddressBook.BUNDLE_PATH, mask
1286: .getField("streetAddress"),
1287: "errors.required"));
1288: }
1289:
1290: if (StringHandling.isNullOrEmpty(addressDO.getTown())) {
1291: errors.add(new ValidationError("person.address",
1292: AddressBook.BUNDLE_PATH, mask.getField("town"),
1293: "errors.required"));
1294: }
1295:
1296: if (logger.isDebugEnabled()) {
1297: logger
1298: .debug("validate(SecuritySession, AddressDO) - end - return value = "
1299: + errors);
1300: }
1301:
1302: return errors;
1303: }
1304:
1305: /**
1306: * {@inheritDoc}
1307: *
1308: * @param securitySession {@inheritDoc}
1309: * @param groupDO {@inheritDoc}
1310: * @return {@inheritDoc}
1311: */
1312: public ValidationErrors validate(
1313: final SecuritySession securitySession, final GroupDO groupDO) {
1314: if (logger.isDebugEnabled()) {
1315: logger.debug("validate(SecuritySession securitySession = "
1316: + securitySession + ", GroupDO groupDO = "
1317: + groupDO + ") - start");
1318: }
1319:
1320: ValidationErrors errors = new ValidationErrors();
1321: Mask mask = maskFactory.getMask(GroupDO.class);
1322:
1323: // name is mandatory
1324: if (StringHandling.isNullOrEmpty(groupDO.getName())) {
1325: errors.add(new ValidationError("group",
1326: AddressBook.BUNDLE_PATH, mask.getField("name"),
1327: "errors.required"));
1328: }
1329:
1330: if (logger.isDebugEnabled()) {
1331: logger
1332: .debug("validate(SecuritySession, GroupDO) - end - return value = "
1333: + errors);
1334: }
1335:
1336: return errors;
1337: }
1338:
1339: /**
1340: * {@inheritDoc}
1341: *
1342: * @param securitySession {@inheritDoc}
1343: * @param personDO {@inheritDoc}
1344: * @return {@inheritDoc}
1345: */
1346: public ValidationErrors validate(
1347: final SecuritySession securitySession,
1348: final PersonDO personDO) {
1349: if (logger.isDebugEnabled()) {
1350: logger.debug("validate(SecuritySession securitySession = "
1351: + securitySession + ", PersonDO personDO = "
1352: + personDO + ") - start");
1353: }
1354:
1355: ValidationErrors errors = new ValidationErrors();
1356: Mask mask = maskFactory.getMask(PersonDO.class);
1357: Mask userMask = maskFactory.getMask(UserDO.class);
1358:
1359: if (StringHandling.isNullOrEmpty(personDO.getFileAs())) {
1360: errors.add(new ValidationError("personDetails",
1361: AddressBook.BUNDLE_PATH, mask.getField("fileAs"),
1362: "errors.required"));
1363: }
1364:
1365: if (StringHandling.isNullOrEmpty(personDO.getFirstNames())) {
1366: errors
1367: .add(new ValidationError("personDetails",
1368: AddressBook.BUNDLE_PATH, mask
1369: .getField("firstNames"),
1370: "errors.required"));
1371: }
1372:
1373: if (StringHandling.isNullOrEmpty(personDO.getLastName())) {
1374: errors.add(new ValidationError("personDetails",
1375: AddressBook.BUNDLE_PATH, mask.getField("lastName"),
1376: "errors.required"));
1377:
1378: // contact has to belong to some group not root node of
1379: // group tree !!!!
1380: }
1381:
1382: if (personDO.getGroup() == null) {
1383: errors.add(new ValidationError("personDetails",
1384: AddressBook.BUNDLE_PATH, mask
1385: .getField("addressBookName"),
1386: "errors.required"));
1387: }
1388:
1389: // if this is the default administrator, you can't stop that person
1390: // being a user
1391: if (UserConstants.ADMINISTRATOR.equals(personDO.getId())) {
1392: if ((personDO.getUser() == null)
1393: || !personDO.getUser().isEnabled()
1394: || personDO.getUser().isDeleted()) {
1395: errors.add(new ValidationError("user",
1396: Security.BUNDLE_PATH,
1397: userMask.getField("name"),
1398: "errors.addressBook.user.administrator"));
1399: }
1400: }
1401:
1402: if (personDO.getUser() != null) {
1403: if (personDO.getUser().getName() == null) {
1404: errors.add(new ValidationError("user",
1405: Security.BUNDLE_PATH,
1406: userMask.getField("name"), "errors.required"));
1407: }
1408:
1409: // every user _must_ have at least one email address
1410: // go thro' all the telecom addresses and look for an email address
1411: boolean foundEmail = false;
1412:
1413: for (Iterator i = personDO.getTelecomAddresses().iterator(); i
1414: .hasNext();) {
1415: int type = ((TelecomAddressDO) i.next()).getType();
1416:
1417: if (type == TelecomAddressConstants.TYPE_EMAIL) {
1418: foundEmail = true;
1419:
1420: break;
1421: }
1422: }
1423:
1424: if (!foundEmail) {
1425: errors.add(new ValidationError("personDetails",
1426: AddressBook.BUNDLE_PATH, mask
1427: .getField("emailAddress"),
1428: "errors.required"));
1429: }
1430: }
1431:
1432: // if any the mandatory address fields are there, see if they all are
1433: if (personDO.getAddress() != null) {
1434: ValidationErrors addressErrors = validate(securitySession,
1435: personDO.getAddress());
1436:
1437: if (addressErrors != null) {
1438: errors.addAll(addressErrors);
1439: }
1440: }
1441:
1442: if (logger.isDebugEnabled()) {
1443: logger
1444: .debug("validate(SecuritySession, PersonDO) - end - return value = "
1445: + errors);
1446: }
1447:
1448: return errors;
1449: }
1450: }
|