1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
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
73
74
75 public void load(VersionedAssetPK pk, VersionedAssetEJB ejb) throws EJBException {
76 super.load(pk, ejb);
77 }
78
79
80
81
82 public void store(VersionedAssetEJB ejb) throws EJBException {
83 super.store(ejb);
84 }
85
86
87
88
89 public void remove(VersionedAssetPK pk) throws RemoveException, EJBException {
90 super.remove(pk);
91 }
92
93
94
95
96 public VersionedAssetPK create(VersionedAssetEJB ejb) throws CreateException, EJBException {
97 return (VersionedAssetPK) super.create(ejb);
98 }
99
100
101
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
111
112
113 public VersionedAssetPK findByPrimaryKey(VersionedAssetPK pk) throws FinderException {
114 return (VersionedAssetPK) super.findByPrimaryKey(pk);
115 }
116
117
118
119
120 public Collection findAll() throws FinderException {
121 return getPrimaryKeys(ASSET_QUERY);
122 }
123
124
125
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
145
146
147 protected Object getPrimaryKey(String id) {
148 return new VersionedAssetPK(id);
149 }
150
151
152
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
169
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;
176 }
177 VersionedAssetEJB assetEjb = (VersionedAssetEJB) ejb;
178
179
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
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
256
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
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
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
326
327
328 protected String getSingleResourceQuery(String id) {
329 return ASSET_QUERY + "[@oid='" + id + "']";
330 }
331
332 }