View Javadoc

1   /*
2    *  Copyright (C) 2003-2005 SINTEF
3    *  Author:  fredrik dot vraalsen at sintef dot no
4    *  Webpage: http://coras.sourceforge.net/
5    *
6    *  This program is free software; you can redistribute it and/or
7    *  modify it under the terms of the GNU Lesser General Public License
8    *  as published by the Free Software Foundation; either version 2.1
9    *  of the License, or (at your option) any later version.
10   *
11   *  This program is distributed in the hope that it will be useful,
12   *  but WITHOUT ANY WARRANTY; without even the implied warranty of
13   *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14   *  Lesser General Public License for more details.
15   *
16   *  You should have received a copy of the GNU Lesser General Public
17   *  License along with this program; if not, write to the Free
18   *  Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
19   *  02111-1307 USA
20   */
21  package no.sintef.assetrepository.dao;
22  
23  import java.util.ArrayList;
24  import java.util.Arrays;
25  import java.util.Collection;
26  import java.util.HashMap;
27  import java.util.Iterator;
28  import java.util.List;
29  import java.util.Map;
30  
31  import javax.ejb.CreateException;
32  import javax.ejb.EJBException;
33  import javax.ejb.EntityBean;
34  import javax.ejb.FinderException;
35  import javax.ejb.RemoveException;
36  
37  import no.sintef.assetrepository.entity.AssetVersionEJB;
38  import no.sintef.assetrepository.interfaces.AssetRepresentationPK;
39  import no.sintef.assetrepository.interfaces.AssetTypePK;
40  import no.sintef.assetrepository.interfaces.AssetVersionPK;
41  import no.sintef.assetrepository.interfaces.VersionedAssetPK;
42  import no.sintef.assetrepository.jaxb.IAssetVersion;
43  import no.sintef.assetrepository.jaxb.IOid;
44  import no.sintef.assetrepository.jaxb.IProperties;
45  import no.sintef.assetrepository.jaxb.impl.AssetVersionImpl;
46  import no.sintef.assetrepository.jaxb.impl.IAssetVersionImpl;
47  import no.sintef.assetrepository.jaxb.impl.IOidListImpl;
48  import no.sintef.assetrepository.jaxb.impl.IPropertiesImpl;
49  
50  import org.apache.log4j.Logger;
51  
52  /***
53   * @author fvr
54   *
55   * TODO To change the template for this generated type comment go to
56   * Window - Preferences - Java - Code Style - Code Templates
57   */
58  public class AssetVersionDAOImpl extends AbstractDAO implements AssetVersionDAO {
59  
60      private static final Logger LOGGER = Logger.getLogger(AssetVersionDAOImpl.class);
61      private static final String COLLECTION_NAME = "assetVersions";
62      private static final String ASSET_VERSION_QUERY = "/ar:assetVersion";
63      
64      public AssetVersionDAOImpl() {
65          super(COLLECTION_NAME);
66      }
67      
68      /* (non-Javadoc)
69       * @see no.sintef.assetrepository.dao.AssetVersionDAO#load(no.sintef.assetrepository.interfaces.AssetVersionPK, no.sintef.assetrepository.entity.AssetVersionEJB)
70       */
71      public void load(AssetVersionPK pk, AssetVersionEJB ejb)
72              throws EJBException {
73          super.load(pk, ejb);
74      }
75  
76      /* (non-Javadoc)
77       * @see no.sintef.assetrepository.dao.AssetVersionDAO#store(no.sintef.assetrepository.entity.AssetVersionEJB)
78       */
79      public void store(AssetVersionEJB ejb) throws EJBException {
80          super.store(ejb);
81      }
82  
83      /* (non-Javadoc)
84       * @see no.sintef.assetrepository.dao.AssetVersionDAO#remove(no.sintef.assetrepository.interfaces.AssetVersionPK)
85       */
86      public void remove(AssetVersionPK pk) throws RemoveException, EJBException {
87          super.remove(pk);
88      }
89  
90      /* (non-Javadoc)
91       * @see no.sintef.assetrepository.dao.AssetVersionDAO#create(no.sintef.assetrepository.entity.AssetVersionEJB)
92       */
93      public AssetVersionPK create(AssetVersionEJB ejb) throws CreateException,
94              EJBException {
95          return (AssetVersionPK) super.create(ejb);
96      }
97  
98      /* (non-Javadoc)
99       * @see no.sintef.assetrepository.dao.AssetVersionDAO#findByPrimaryKey(no.sintef.assetrepository.interfaces.AssetVersionPK)
100      */
101     public AssetVersionPK findByPrimaryKey(AssetVersionPK pk)
102             throws FinderException {
103         return (AssetVersionPK) super.findByPrimaryKey(pk);
104     }
105 
106     /* (non-Javadoc)
107      * @see no.sintef.assetrepository.dao.AssetVersionDAO#findAll()
108      */
109     public Collection findAll() throws FinderException {
110         return getPrimaryKeys(ASSET_VERSION_QUERY);
111     }
112 
113     /* (non-Javadoc)
114      * @see no.sintef.assetrepository.dao.AssetVersionDAO#findByAssetAndLabel(no.sintef.assetrepository.interfaces.AssetPK, java.lang.String)
115      */
116     public AssetVersionPK findByAssetAndLabel(VersionedAssetPK assetPk, String label) throws FinderException {
117         // TODO Auto-generated method stub
118         return null;
119     }
120 
121     /* (non-Javadoc)
122      * @see no.sintef.assetrepository.dao.AbstractDAO#getId(java.lang.Object)
123      */
124     protected String getId(Object o) {
125         if (o == null) {
126             return null;
127         } else if (o instanceof String) {
128             return (String) o;
129         } else if (o instanceof AssetVersionPK) {
130             return ((AssetVersionPK)o).getId();
131         } else if (o instanceof AssetVersionEJB) {
132             return ((AssetVersionEJB)o).getId();
133         } else if (o instanceof IAssetVersion) {
134             return ((IAssetVersion)o).getOid();
135         } else {
136 			LOGGER.error("Unexpected object type: " + o.getClass().getName());
137 			return null;
138         }
139     }
140 
141     /* (non-Javadoc)
142      * @see no.sintef.assetrepository.dao.AbstractDAO#getPrimaryKey(java.lang.String)
143      */
144     protected Object getPrimaryKey(String id) {
145         return new AssetVersionPK(id);
146     }
147 
148     /* (non-Javadoc)
149      * @see no.sintef.assetrepository.dao.AbstractDAO#createJaxbObject(java.lang.String)
150      */
151     private Object createJaxbObject(String id) {
152         IAssetVersion versionData = new AssetVersionImpl();
153         versionData.setOid(id);
154         
155         versionData.setLabels(new IAssetVersionImpl.LabelsTypeImpl());
156         versionData.setProperties(new IPropertiesImpl());
157         versionData.setRepresentations(new IOidListImpl());
158         versionData.setChildren(new IOidListImpl());
159         versionData.setPreviousVersions(new IOidListImpl());
160         versionData.setNextVersions(new IOidListImpl());
161         return versionData;
162     }
163 
164     /* (non-Javadoc)
165      * @see no.sintef.assetrepository.dao.AbstractDAO#createJaxbObject(javax.ejb.EntityBean)
166      */
167     protected Object createJaxbObject(EntityBean ejb) {
168 		IAssetVersion versionData = (IAssetVersion) createJaxbObject((String) null);
169 		if (! (ejb instanceof AssetVersionEJB)) {
170 			LOGGER.error("Unexpected entity bean type: " + (ejb != null ? ejb.getClass().getName() : null));
171 			return versionData; // FIXME
172 		}
173         
174 		AssetVersionEJB versionEjb = (AssetVersionEJB) ejb;
175 		
176 		/* Attributes */
177 		versionData.setOid(versionEjb.getId());
178 		String[] labels = versionEjb.getLabel();
179 		if (labels != null) {
180 		    List labelList = versionData.getLabels().getLabel();
181 		    for (Iterator i = Arrays.asList(labels).iterator(); i.hasNext(); ) {
182 		        String label = (String) i.next();
183 		        if (label != null) {
184 		            labelList.add(label);
185 		        }
186 		    }
187 		}
188 		versionData.setAnnotation(versionEjb.getAnnotation());
189 		versionData.setFrozen(false); // FIXME add frozen property to EJB
190 		versionData.setCreationDate(versionEjb.getCreationDate());
191 		versionData.setName(versionEjb.getName());
192 		versionData.setDescription(versionEjb.getDescription());
193 		versionData.setOwner(versionEjb.getOwner());
194 		Map properties = versionEjb.getProperties();
195 		if (properties != null) {
196 		    List propertiesList = versionData.getProperties().getProperty();
197 		    for (Iterator i = properties.keySet().iterator(); i.hasNext(); ) {
198 		        Object key = i.next();
199 		        Object value = properties.get(key);
200 		        if (key instanceof String && value instanceof String) {
201 		            IProperties.PropertyType property = new IPropertiesImpl.PropertyTypeImpl();
202 		            property.setKey((String) key);
203 		            property.setValue((String) value);
204 		            propertiesList.add(property);
205 		        }
206 		    }
207 		}
208 		
209 		/* Relations */
210 		VersionedAssetPK assetPk = versionEjb.getAsset();
211 		if (assetPk == null) {
212 		    // FIXME This should never happen
213 		}
214 		versionData.setAsset(assetPk != null ? createIOid(assetPk.getId()) : null);
215 		
216 		AssetTypePK typePk = versionEjb.getAssetType();
217 		versionData.setAssetType(typePk != null ? createIOid(typePk.getId()) : null);
218 		
219 		VersionedAssetPK parentPk = versionEjb.getParent();
220 		versionData.setParent(parentPk != null ? createIOid(parentPk.getId()) : null);
221 		
222 		Collection childrenPks = versionEjb.getChilds();
223 		List childrenList = versionData.getChildren().getOid();
224 		pksToOids(childrenPks, childrenList, VersionedAssetPK.class);
225 
226 		Collection representationPks = versionEjb.getRepresentations();
227 		List representationOids = versionData.getRepresentations().getOid();
228 		representationOids.clear();
229 		if (representationPks != null) {
230 		    for (Iterator i = representationPks.iterator(); i.hasNext();) {
231                 AssetRepresentationPK representationPk = (AssetRepresentationPK) i.next();
232                 IOid representationOid = createIOid(representationPk.getId());
233                 representationOids.add(representationOid);
234             }
235 		}
236 		
237 		Collection prevVersionPks = versionEjb.getPreviousVersions();
238 		List prevVersionOids = versionData.getPreviousVersions().getOid();
239 		pksToOids(prevVersionPks, prevVersionOids, AssetVersionPK.class);
240 		
241 		Collection nextVersionPks = versionEjb.getNextVersions();
242 		List nextVersionOids = versionData.getNextVersions().getOid();
243 		pksToOids(nextVersionPks, nextVersionOids, AssetVersionPK.class);
244 		
245         return versionData;
246     }
247 
248     /* (non-Javadoc)
249      * @see no.sintef.assetrepository.dao.AbstractDAO#setJaxbObject(javax.ejb.EntityBean, java.lang.Object)
250      */
251     protected void setJaxbObject(EntityBean ejb, Object jaxbObject) {
252 		if (!(ejb instanceof AssetVersionEJB) 
253 				|| !(jaxbObject instanceof IAssetVersion)) {
254 			LOGGER.error("Entity bean = " + ejb + ", JAXB object = " + jaxbObject);
255 			return;
256 		}
257         
258 		AssetVersionEJB versionEjb = (AssetVersionEJB) ejb;
259 		IAssetVersion versionData = (IAssetVersion) jaxbObject;
260 		
261 		/* Attributes */
262 		versionEjb.setId(versionData.getOid());
263 		versionEjb.setLabel((String[]) versionData.getLabels().getLabel().toArray(new String[0]));
264 		versionEjb.setAnnotation(versionData.getAnnotation());
265 		versionEjb.setCreationDate(versionData.getCreationDate());
266 		versionEjb.setName(versionData.getName());
267 		versionEjb.setDescription(versionData.getDescription());
268 		versionEjb.setOwner(versionData.getOwner());
269 		Map propertiesMap = new HashMap();
270 		List propertiesList = versionData.getProperties().getProperty();
271 		for (Iterator i = propertiesList.iterator(); i.hasNext(); ) {
272 		    IProperties.PropertyType property = (IProperties.PropertyType) i.next();
273 		    propertiesMap.put(property.getKey(), property.getValue());
274 		}
275 		versionEjb.setProperties(propertiesMap);
276 		
277 		/* Relations */
278 		IOid assetOid = versionData.getAsset();
279 		versionEjb.setAsset(assetOid != null ? new VersionedAssetPK(assetOid.getOid()) : null);
280 		
281 		IOid typeOid = versionData.getAssetType();
282 		versionEjb.setAssetType(typeOid != null ? new AssetTypePK(typeOid.getOid()) : null);
283 		
284 		IOid parentOid = versionData.getParent();
285 		versionEjb.setParent(parentOid != null ? new VersionedAssetPK(parentOid.getOid()) : null);
286 		
287 		List childrenOids = versionData.getChildren().getOid();
288 		versionEjb.setChilds(oidsToPks(childrenOids, VersionedAssetPK.class));
289 		
290 		Collection representationPks = new ArrayList();
291 		List representationOids = versionData.getRepresentations().getOid();
292 		for (Iterator i = representationOids.iterator(); i.hasNext();) {
293 		    IOid representationOid = (IOid) i.next();
294 		    AssetRepresentationPK representationPk = new AssetRepresentationPK(representationOid.getOid());
295 		    representationPks.add(representationPk);
296 		}
297 		versionEjb.setRepresentations(representationPks);
298 
299 		List prevVersionOids = versionData.getPreviousVersions().getOid();
300 		versionEjb.setPreviousVersions(oidsToPks(prevVersionOids, AssetVersionPK.class));
301 
302 		List nextVersionOids = versionData.getNextVersions().getOid();
303 		versionEjb.setNextVersions(oidsToPks(nextVersionOids, AssetVersionPK.class));
304     }
305 
306     /* (non-Javadoc)
307      * @see no.sintef.assetrepository.dao.AbstractDAO#getSingleResourceQuery(java.lang.String)
308      */
309     protected String getSingleResourceQuery(String id) {
310         return ASSET_VERSION_QUERY + "[@oid='" + id + "']";
311     }
312 
313 }