001: /*--
002:
003: Copyright (C) 2002-2005 Adrian Price.
004: All rights reserved.
005:
006: Redistribution and use in source and binary forms, with or without
007: modification, are permitted provided that the following conditions
008: are met:
009:
010: 1. Redistributions of source code must retain the above copyright
011: notice, this list of conditions, and the following disclaimer.
012:
013: 2. Redistributions in binary form must reproduce the above copyright
014: notice, this list of conditions, and the disclaimer that follows
015: these conditions in the documentation and/or other materials
016: provided with the distribution.
017:
018: 3. The names "OBE" and "Open Business Engine" must not be used to
019: endorse or promote products derived from this software without prior
020: written permission. For written permission, please contact
021: adrianprice@sourceforge.net.
022:
023: 4. Products derived from this software may not be called "OBE" or
024: "Open Business Engine", nor may "OBE" or "Open Business Engine"
025: appear in their name, without prior written permission from
026: Adrian Price (adrianprice@users.sourceforge.net).
027:
028: THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
029: WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
030: OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
031: DISCLAIMED. IN NO EVENT SHALL THE AUTHOR(S) BE LIABLE FOR ANY DIRECT,
032: INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
033: (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
034: SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
035: HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
036: STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
037: IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
038: POSSIBILITY OF SUCH DAMAGE.
039:
040: For more information on OBE, please see
041: <http://obe.sourceforge.net/>.
042:
043: */
044:
045: package org.obe.server.j2ee.repository;
046:
047: import org.obe.client.api.repository.ObjectAlreadyExistsException;
048: import org.obe.client.api.repository.ObjectNotFoundException;
049: import org.obe.client.api.repository.RepositoryException;
050: import org.obe.server.j2ee.ejb.AbstractEntityEJB;
051: import org.obe.spi.model.AbstractSystemAttribute;
052: import org.obe.spi.model.AttributeInstance;
053: import org.obe.spi.model.AttributedEntity;
054:
055: import javax.ejb.*;
056: import java.beans.PropertyDescriptor;
057: import java.util.*;
058:
059: /**
060: * An abstract entity bean that owns a collection of attributes. Subclasses
061: * must call ejbPassivate() or ejbRemove() from overrides of those methods.
062: *
063: * @author Adrian Price
064: * @ejb:bean generate="false"
065: */
066: public abstract class AbstractAttributedEntityEJB extends
067: AbstractEntityEJB implements AttributedEntity {
068:
069: private static final long serialVersionUID = 5530261826284806149L;
070: private final Map _sysAttrs = new HashMap();
071: private transient Map _attrs;
072: private transient Map _umAttrs;
073:
074: private class SystemAttribute extends AbstractSystemAttribute {
075: private static final long serialVersionUID = -3131223826382337073L;
076:
077: protected SystemAttribute(PropertyDescriptor propDesc) {
078: super (propDesc);
079: }
080:
081: public AttributedEntity getOwner() {
082: return (AttributedEntity) _ejbLocalObject;
083: }
084: }
085:
086: protected AbstractAttributedEntityEJB(PropertyDescriptor[] propDescs) {
087: for (int i = 0; i < propDescs.length; i++) {
088: PropertyDescriptor propDesc = propDescs[i];
089: _sysAttrs.put(propDesc.getName(), new SystemAttribute(
090: propDesc));
091: }
092: }
093:
094: public void ejbRemove() throws RemoveException {
095: try {
096: for (Iterator iter = getAttributeInstances().values()
097: .iterator(); iter.hasNext();) {
098:
099: Object attr = iter.next();
100: if (attr instanceof AttributeInstanceLocal)
101: ((EJBLocalObject) attr).remove();
102: }
103: super .ejbRemove();
104: } catch (RepositoryException e) {
105: getLogger().error(e);
106: throw new RemoveException(e.getMessage());
107: }
108: }
109:
110: protected void clear() {
111: super .clear();
112: if (_attrs != null) {
113: _attrs.clear();
114: _attrs = _umAttrs = null;
115: }
116: }
117:
118: /**
119: * @ejb:interface-method
120: * @ejb:persistence column-name="PROCESSDEFINITIONID"
121: */
122: public abstract String getProcessDefinitionId();
123:
124: /**
125: * @ejb:persistence column-name="PROCESSDEFINITIONID"
126: */
127: public abstract void setProcessDefinitionId(
128: String processDefinitionId);
129:
130: /**
131: * Subclasses must implement this method and return their owner type.
132: *
133: * @return Owner type, one of:
134: * {@link AttributedEntity#PROCESS_INSTANCE_TYPE},
135: * {@link AttributedEntity#ACTIVITY_INSTANCE_TYPE},
136: * {@link AttributedEntity#WORKITEM_TYPE}.
137: */
138: protected abstract int getOwnerType();
139:
140: /**
141: * @ejb:interface-method
142: */
143: public AttributeInstance getAttributeInstance(String attributeName)
144: throws RepositoryException {
145:
146: AttributeInstance attr = (AttributeInstance) getAttributeInstances()
147: .get(attributeName);
148: if (attr == null)
149: throw new ObjectNotFoundException(attributeName);
150: return attr;
151: }
152:
153: /**
154: * @ejb:interface-method
155: */
156: public Map getAttributeInstances() throws RepositoryException {
157: // EJB 2.0 CMR does not support relationships with complex keys, so we
158: // have to implement this method ourselves.
159: if (_attrs == null) {
160: Map attrs = new HashMap();
161: String ownerId = getEntityId();
162: try {
163: Collection attributes = EJBLocalHelper
164: .getAttributeInstanceHome().findByOwnerId(
165: ownerId, getOwnerType());
166: for (Iterator iter = attributes.iterator(); iter
167: .hasNext();) {
168: AttributeInstance attr = (AttributeInstance) iter
169: .next();
170: attrs.put(attr.getName(), attr);
171: }
172: // System attributes override custom attributes.
173: attrs.putAll(_sysAttrs);
174: } catch (FinderException e) {
175: throw new RepositoryException(e);
176: }
177: _attrs = attrs;
178: _umAttrs = Collections.unmodifiableMap(_attrs);
179: }
180: return _umAttrs;
181: }
182:
183: /**
184: * @ejb:interface-method
185: */
186: public AttributeInstance addAttributeInstance(String attrName,
187: int attrType, Object attrValue) throws RepositoryException {
188:
189: try {
190: // Can't override system attributes.
191: if (_sysAttrs.containsKey(attrName))
192: throw new ObjectAlreadyExistsException(attrName);
193:
194: AttributeInstanceLocal attr = EJBLocalHelper
195: .getAttributeInstanceHome().create(
196: getProcessDefinitionId(),
197: getProcessInstanceId(), getEntityId(),
198: getOwnerType(), attrName, attrType,
199: attrValue);
200: // Must add the new attribute to the collection so we can find it
201: // in memory before the commit.
202: getAttributeInstances();
203: _attrs.put(attrName, attr);
204: return attr;
205: } catch (DuplicateKeyException e) {
206: throw new ObjectAlreadyExistsException(attrName);
207: } catch (CreateException e) {
208: throw new RepositoryException(e);
209: }
210: }
211: }
|