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.Calendar;
25  import java.util.Collection;
26  import java.util.Collections;
27  import java.util.HashMap;
28  import java.util.Iterator;
29  import java.util.List;
30  import java.util.Map;
31  
32  import javax.ejb.CreateException;
33  import javax.ejb.EJBException;
34  import javax.ejb.EntityBean;
35  import javax.ejb.FinderException;
36  import javax.ejb.ObjectNotFoundException;
37  import javax.ejb.RemoveException;
38  
39  import no.sintef.assetrepository.VersionHistory;
40  import no.sintef.assetrepository.entity.VersionedAssetEJB;
41  import no.sintef.assetrepository.interfaces.AssetRepresentationPK;
42  import no.sintef.assetrepository.interfaces.AssetTypePK;
43  import no.sintef.assetrepository.interfaces.AssetVersionPK;
44  import no.sintef.assetrepository.interfaces.VersionedAssetPK;
45  import no.sintef.assetrepository.jaxb.IOid;
46  import no.sintef.assetrepository.jaxb.IOidList;
47  import no.sintef.assetrepository.jaxb.IProperties;
48  import no.sintef.assetrepository.jaxb.IVersionedAsset;
49  import no.sintef.assetrepository.jaxb.impl.IOidListImpl;
50  import no.sintef.assetrepository.jaxb.impl.IPropertiesImpl;
51  import no.sintef.assetrepository.jaxb.impl.IVersionedAssetImpl;
52  import no.sintef.assetrepository.jaxb.impl.VersionedAssetImpl;
53  
54  import org.apache.log4j.Logger;
55  
56  /***
57   * @author fvr
58   *
59   * TODO To change the template for this generated type comment go to
60   * Window - Preferences - Java - Code Style - Code Templates
61   */
62  public class VersionedAssetDAOImpl extends AbstractDAO implements VersionedAssetDAO {
63  
64      private static final Logger LOGGER = Logger.getLogger(VersionedAssetDAOImpl.class);
65      private static final String COLLECTION_NAME = "versionedAssets";
66      private static final String ASSET_QUERY = "/ar:versionedAsset";
67      
68      public VersionedAssetDAOImpl() {
69          super(COLLECTION_NAME);
70      }
71      
72      /* (non-Javadoc)
73       * @see no.sintef.assetrepository.dao.AssetDAO#load(no.sintef.assetrepository.interfaces.AssetPK, no.sintef.assetrepository.entity.AssetEJB)
74       */
75      public void load(VersionedAssetPK pk, VersionedAssetEJB ejb) throws EJBException {
76          super.load(pk, ejb);
77      }
78  
79      /* (non-Javadoc)
80       * @see no.sintef.assetrepository.dao.AssetDAO#store(no.sintef.assetrepository.entity.AssetEJB)
81       */
82      public void store(VersionedAssetEJB ejb) throws EJBException {
83          super.store(ejb);
84      }
85  
86      /* (non-Javadoc)
87       * @see no.sintef.assetrepository.dao.AssetDAO#remove(no.sintef.assetrepository.interfaces.AssetPK)
88       */
89      public void remove(VersionedAssetPK pk) throws RemoveException, EJBException {
90          super.remove(pk);
91      }
92  
93      /* (non-Javadoc)
94       * @see no.sintef.assetrepository.dao.AssetDAO#create(no.sintef.assetrepository.entity.AssetEJB)
95       */
96      public VersionedAssetPK create(VersionedAssetEJB ejb) throws CreateException, EJBException {
97          return (VersionedAssetPK) super.create(ejb);
98      }
99  
100     /* (non-Javadoc)
101      * @see no.sintef.assetrepository.dao.AssetDAO#findByType(java.lang.String)
102      */
103     public Collection findByType(AssetTypePK typePk) throws FinderException {
104 		if (typePk == null || typePk.getId() == null) {
105 			throw new ObjectNotFoundException("Asset type is null");
106 		}
107 		return getPrimaryKeys(ASSET_QUERY + "[ar:assetType/@oid='" + typePk.getId() + "']");
108     }
109 
110     /* (non-Javadoc)
111      * @see no.sintef.assetrepository.dao.AssetDAO#findByPrimaryKey(no.sintef.assetrepository.interfaces.AssetPK)
112      */
113     public VersionedAssetPK findByPrimaryKey(VersionedAssetPK pk) throws FinderException {
114         return (VersionedAssetPK) super.findByPrimaryKey(pk);
115     }
116 
117     /* (non-Javadoc)
118      * @see no.sintef.assetrepository.dao.AssetDAO#findAll()
119      */
120     public Collection findAll() throws FinderException {
121         return getPrimaryKeys(ASSET_QUERY);
122     }
123 
124     /* (non-Javadoc)
125      * @see no.sintef.assetrepository.dao.AbstractDAO#getId(java.lang.Object)
126      */
127     protected String getId(Object o) {
128         if (o == null) {
129             return null;
130         } else if (o instanceof String) {
131             return (String) o;
132         } else if (o instanceof VersionedAssetPK) {
133             return ((VersionedAssetPK)o).getId();
134         } else if (o instanceof VersionedAssetEJB) {
135             return ((VersionedAssetEJB)o).getId();
136         } else if (o instanceof IVersionedAsset) {
137             return ((IVersionedAsset)o).getOid();
138         } else {
139 			LOGGER.error("Unexpected object type: " + o.getClass().getName());
140 			return null;
141         }
142     }
143 
144     /* (non-Javadoc)
145      * @see no.sintef.assetrepository.dao.AbstractDAO#getPrimaryKey(java.lang.String)
146      */
147     protected Object getPrimaryKey(String id) {
148         return new VersionedAssetPK(id);
149     }
150 
151     /* (non-Javadoc)
152      * @see no.sintef.assetrepository.dao.AbstractDAO#createJaxbObject(java.lang.String)
153      */
154     private Object createJaxbObject(String id) {
155         IVersionedAsset assetData = new VersionedAssetImpl();
156         assetData.setOid(id);
157 
158         assetData.setProperties(new IPropertiesImpl());
159         assetData.setRepresentations(new IOidListImpl());
160         assetData.setChildren(new IOidListImpl());
161         assetData.setPreviousVersions(new IOidListImpl());
162         IVersionedAsset.VersionHistoryType history = new IVersionedAssetImpl.VersionHistoryTypeImpl();
163         history.setVersions(new IVersionedAssetImpl.VersionHistoryTypeImpl.VersionsTypeImpl());
164         assetData.setVersionHistory(history);
165         return assetData;
166     }
167 
168     /* (non-Javadoc)
169      * @see no.sintef.assetrepository.dao.AbstractDAO#createJaxbObject(javax.ejb.EntityBean)
170      */
171     protected Object createJaxbObject(EntityBean ejb) {
172         IVersionedAsset assetData = (IVersionedAsset) createJaxbObject((String) null);
173 		if (! (ejb instanceof VersionedAssetEJB)) {
174 			LOGGER.error("Unexpected entity bean type: " + (ejb != null ? ejb.getClass().getName() : null));
175 			return assetData; // FIXME
176 		}
177 		VersionedAssetEJB assetEjb = (VersionedAssetEJB) ejb;
178 
179         /* Attributes */
180         assetData.setOid(assetEjb.getId());
181         assetData.setFrozen(assetEjb.getFrozen());
182         assetData.setName(assetEjb.getName());
183         assetData.setDescription(assetEjb.getDescription());
184         assetData.setOwner(assetEjb.getOwner());
185 		Map properties = assetEjb.getProperties();
186 		if (properties != null) {
187 		    List propertiesList = assetData.getProperties().getProperty();
188 		    for (Iterator i = properties.keySet().iterator(); i.hasNext(); ) {
189 		        Object key = i.next();
190 		        Object value = properties.get(key);
191 		        if (key instanceof String && value instanceof String) {
192 		            IProperties.PropertyType property = new IPropertiesImpl.PropertyTypeImpl();
193 		            property.setKey((String) key);
194 		            property.setValue((String) value);
195 		            propertiesList.add(property);
196 		        }
197 		    }
198 		}
199         
200         String checkedOutBy = assetEjb.getCheckedOutBy();
201         Calendar checkedOutDate = assetEjb.getCheckedOutDate();
202         IVersionedAsset.CheckedOutType checkedOut = null;
203         if (checkedOutDate != null) {
204             checkedOut = new IVersionedAssetImpl.CheckedOutTypeImpl();
205             checkedOut.setBy(checkedOutBy);
206             checkedOut.setDate(checkedOutDate);
207         }
208         assetData.setCheckedOut(checkedOut);
209 
210         assetData.setCreationDate(assetEjb.getCreationDate());
211         assetData.setDeletionDate(assetEjb.getDeletionDate());
212         
213         VersionHistory history = assetEjb.getVersionHistory();
214         AssetVersionPK rootVersionPk = history.getRootVersion();
215         IVersionedAsset.VersionHistoryType historyData = new IVersionedAssetImpl.VersionHistoryTypeImpl();
216         historyData.setVersions(new IVersionedAssetImpl.VersionHistoryTypeImpl.VersionsTypeImpl());
217         historyData.setRootVersion(rootVersionPk != null ? createIOid(rootVersionPk.getId()) : null);
218         Collection versionPks = history.getVersions();
219         List versionOids = historyData.getVersions().getVersion();
220         pksToOids(versionPks, versionOids, AssetVersionPK.class);
221         assetData.setVersionHistory(historyData);
222         
223         /* Relations */
224         AssetTypePK assetTypePk = assetEjb.getAssetType();
225         assetData.setAssetType(assetTypePk != null ? createIOid(assetTypePk.getId()) : null);
226 		
227 		VersionedAssetPK parentPk = assetEjb.getParent();
228 		assetData.setParent(parentPk != null ? createIOid(parentPk.getId()) : null);
229 		
230 		Collection childrenPks = assetEjb.getChilds();
231 		List childrenList = assetData.getChildren().getOid();
232 		pksToOids(childrenPks, childrenList, VersionedAssetPK.class);
233 
234 		Collection representationPks = assetEjb.getRepresentations();
235 		List representationOids = assetData.getRepresentations().getOid();
236 		representationOids.clear();
237 		if (representationPks != null) {
238 		    for (Iterator i = representationPks.iterator(); i.hasNext();) {
239                 AssetRepresentationPK representationLocal = (AssetRepresentationPK) i.next();
240                 IOid representationOid = createIOid(representationLocal.getId());
241                 representationOids.add(representationOid);
242             }
243 		}
244 
245         AssetVersionPK baseVersionPk = assetEjb.getBaseVersion();
246         assetData.setBaseVersion(baseVersionPk != null ? createIOid(baseVersionPk.getId()) : null);
247         
248         Collection previousVersionPks = assetEjb.getPreviousVersions();
249         List previousVersionOids = assetData.getPreviousVersions().getOid();
250         pksToOids(previousVersionPks, previousVersionOids, AssetVersionPK.class);
251         
252         return assetData;
253     }
254 
255     /* (non-Javadoc)
256      * @see no.sintef.assetrepository.dao.AbstractDAO#setJaxbObject(javax.ejb.EntityBean, java.lang.Object)
257      */
258     protected void setJaxbObject(EntityBean ejb, Object jaxbObject) {
259 		if (!(ejb instanceof VersionedAssetEJB) 
260 				|| !(jaxbObject instanceof IVersionedAsset)) {
261 			LOGGER.error("Entity bean = " + ejb + ", JAXB object = " + jaxbObject);
262 			return;
263 		}
264 		
265 		VersionedAssetEJB assetEjb = (VersionedAssetEJB) ejb;
266 		IVersionedAsset assetData = (IVersionedAsset) jaxbObject;
267 		
268 		/* Attributes */
269 		assetEjb.setId(assetData.getOid());
270 		assetEjb.setFrozen(assetData.isFrozen());
271 		IVersionedAsset.CheckedOutType checkedOut = assetData.getCheckedOut();
272 		assetEjb.setCheckedOutBy(checkedOut != null ? checkedOut.getBy() : null);
273 		assetEjb.setCheckedOutDate(checkedOut != null ? checkedOut.getDate() : null);
274 		assetEjb.setCreationDate(assetData.getCreationDate());
275 		assetEjb.setDeletionDate(assetData.getDeletionDate());
276 		assetEjb.setName(assetData.getName());
277 		assetEjb.setDescription(assetData.getDescription());
278 		assetEjb.setOwner(assetData.getOwner());
279 		Map propertiesMap = new HashMap();
280 		IProperties properties = assetData.getProperties();
281 		List propertiesList = properties != null ? properties.getProperty() : Collections.EMPTY_LIST;
282 		for (Iterator i = propertiesList.iterator(); i.hasNext(); ) {
283 		    IProperties.PropertyType property = (IProperties.PropertyType) i.next();
284 		    propertiesMap.put(property.getKey(), property.getValue());
285 		}
286 		assetEjb.setProperties(propertiesMap);
287 
288 		IVersionedAsset.VersionHistoryType historyData = assetData.getVersionHistory();
289 		IOid rootVersionOid = historyData.getRootVersion();
290 		AssetVersionPK rootVersionPk = new AssetVersionPK(rootVersionOid != null ? rootVersionOid.getOid() : null);
291 		List versionOids = historyData.getVersions().getVersion();
292 		Collection versionPks = oidsToPks(versionOids, AssetVersionPK.class);
293 		VersionHistory history = new VersionHistory(rootVersionPk, versionPks); 
294 		assetEjb.setVersionHistory(history);
295 		
296 		/* Relations */
297 		IOid assetTypeOid = assetData.getAssetType();
298 		AssetTypePK assetTypePk = assetTypeOid != null ? new AssetTypePK(assetTypeOid.getOid()) : null;
299 		assetEjb.setAssetType(assetTypePk);
300 		
301 		IOid parentOid = assetData.getParent();
302 		assetEjb.setParent(parentOid != null ? new VersionedAssetPK(parentOid.getOid()) : null);
303 		
304 		List childrenOids = assetData.getChildren().getOid();
305 		assetEjb.setChilds(oidsToPks(childrenOids, VersionedAssetPK.class));
306 		
307 		Collection representationPks = new ArrayList();
308 		List representationOids = assetData.getRepresentations().getOid();
309 		for (Iterator i = representationOids.iterator(); i.hasNext();) {
310 		    IOid representationOid = (IOid) i.next();
311 		    AssetRepresentationPK representationPk = new AssetRepresentationPK(representationOid.getOid());
312 		    representationPks.add(representationPk);
313 		}
314 		assetEjb.setRepresentations(representationPks);
315 		
316 		IOid baseVersionOid = assetData.getBaseVersion();
317 		AssetVersionPK baseVersionPk = baseVersionOid != null ? new AssetVersionPK(baseVersionOid.getOid()) : null;
318 		assetEjb.setBaseVersion(baseVersionPk);
319 
320 		IOidList previousVersions = assetData.getPreviousVersions();
321 		List previousVersionOids = previousVersions != null ? previousVersions.getOid() : Collections.EMPTY_LIST;
322 		assetEjb.setPreviousVersions(oidsToPks(previousVersionOids, AssetVersionPK.class));
323     }
324 
325     /* (non-Javadoc)
326      * @see no.sintef.assetrepository.dao.AbstractDAO#getSingleResourceQuery(java.lang.String)
327      */
328     protected String getSingleResourceQuery(String id) {
329         return ASSET_QUERY + "[@oid='" + id + "']";
330     }
331 
332 }