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.io.ByteArrayInputStream;
24 import java.io.IOException;
25 import java.io.InputStream;
26 import java.util.Collection;
27
28 import javax.ejb.CreateException;
29 import javax.ejb.EJBException;
30 import javax.ejb.EntityBean;
31 import javax.ejb.FinderException;
32 import javax.ejb.RemoveException;
33
34 import no.sintef.assetrepository.entity.AssetRepresentationEJB;
35 import no.sintef.assetrepository.interfaces.AssetRepresentationPK;
36 import no.sintef.assetrepository.jaxb.IAssetRepresentation;
37 import no.sintef.assetrepository.jaxb.IContent;
38 import no.sintef.assetrepository.jaxb.impl.AssetRepresentationImpl;
39 import no.sintef.assetrepository.jaxb.impl.IContentImpl;
40 import no.sintef.file.MimeTypeEnum;
41 import no.sintef.xml.XmlException;
42 import no.sintef.xml.XmlHelper;
43
44 import org.apache.log4j.Logger;
45 import org.w3c.dom.Node;
46 import org.xml.sax.InputSource;
47
48 /***
49 * @author fvr
50 *
51 * TODO To change the template for this generated type comment go to
52 * Window - Preferences - Java - Code Style - Code Templates
53 */
54 public class AssetRepresentationDAOImpl extends AbstractDAO implements AssetRepresentationDAO {
55
56 private static final Logger LOGGER = Logger.getLogger(AssetRepresentationDAOImpl.class);
57 private static final String COLLECTION_NAME = "assetRepresentations";
58 private static final String ASSET_REPRESENTATION_QUERY = "/ar:assetRepresentation";
59
60 public AssetRepresentationDAOImpl() {
61 super(COLLECTION_NAME);
62 }
63
64
65
66
67 public void load(AssetRepresentationPK pk, AssetRepresentationEJB ejb)
68 throws EJBException {
69 super.load(pk, ejb);
70 }
71
72
73
74
75 public void store(AssetRepresentationEJB ejb) throws EJBException {
76 super.store(ejb);
77 }
78
79
80
81
82 public void remove(AssetRepresentationPK pk) throws RemoveException,
83 EJBException {
84 super.remove(pk);
85 }
86
87
88
89
90 public AssetRepresentationPK create(AssetRepresentationEJB ejb)
91 throws CreateException, EJBException {
92 return (AssetRepresentationPK) super.create(ejb);
93 }
94
95
96
97
98 public AssetRepresentationPK findByPrimaryKey(AssetRepresentationPK pk)
99 throws FinderException {
100 return (AssetRepresentationPK) super.findByPrimaryKey(pk);
101 }
102
103
104
105
106 public Collection findAll() throws FinderException {
107 return getPrimaryKeys(ASSET_REPRESENTATION_QUERY);
108 }
109
110
111
112
113 protected String getId(Object o) {
114 if (o == null) {
115 return null;
116 } else if (o instanceof String) {
117 return (String) o;
118 } else if (o instanceof AssetRepresentationPK) {
119 return ((AssetRepresentationPK)o).getId();
120 } else if (o instanceof AssetRepresentationEJB) {
121 return ((AssetRepresentationEJB)o).getId();
122 } else if (o instanceof IAssetRepresentation) {
123 return ((IAssetRepresentation)o).getOid();
124 } else {
125 LOGGER.error("Unexpected object type: " + o.getClass().getName());
126 return null;
127 }
128 }
129
130
131
132
133 protected Object getPrimaryKey(String id) {
134 return new AssetRepresentationPK(id);
135 }
136
137
138
139
140 private Object createJaxbObject(String id) {
141 IAssetRepresentation representationData = new AssetRepresentationImpl();
142 representationData.setOid(id);
143 return representationData;
144 }
145
146
147
148
149 protected Object createJaxbObject(EntityBean ejb) {
150 IAssetRepresentation representationData = (IAssetRepresentation) createJaxbObject((String) null);
151 if (! (ejb instanceof AssetRepresentationEJB)) {
152 LOGGER.error("Unexpected entity bean type: " + (ejb != null ? ejb.getClass().getName() : null));
153 return representationData;
154 }
155 AssetRepresentationEJB representationEjb = (AssetRepresentationEJB) ejb;
156
157 IContent content = new IContentImpl();
158 Object o = representationEjb.getContent();
159 if (o instanceof Node) {
160 IContent.XmlType xml = new IContentImpl.XmlTypeImpl();
161 xml.setAny(XmlHelper.getRootElement((Node) o));
162 content.setXml(xml);
163 } else if (o instanceof String) {
164 content.setText((String) o);
165 } else if (o instanceof byte[]) {
166 content.setBinary((byte[]) o);
167 } else {
168 LOGGER.error("Unexpected content type: " + o);
169 content.setBinary(new byte[0]);
170 }
171
172 representationData.setOid(representationEjb.getId());
173 representationData.setType(representationEjb.getType());
174 representationData.setName(representationEjb.getName());
175 MimeTypeEnum mimeType = representationEjb.getMimeType();
176 representationData.setMimeType(mimeType != null ? mimeType.toString() : null);
177 representationData.setContent(content);
178 representationData.setCreationDate(representationEjb.getCreationDate());
179
180 return representationData;
181 }
182
183
184
185
186 protected void setJaxbObject(EntityBean ejb, Object jaxbObject) {
187 if (!(ejb instanceof AssetRepresentationEJB)
188 || !(jaxbObject instanceof IAssetRepresentation)) {
189 LOGGER.error("Entity bean = " + ejb + ", JAXB object = " + jaxbObject);
190 return;
191 }
192
193 AssetRepresentationEJB representationEjb = (AssetRepresentationEJB) ejb;
194 IAssetRepresentation representationData = (IAssetRepresentation) jaxbObject;
195 IContent content = representationData.getContent();
196 if (content == null) {
197 System.err.println("This should never ever happen!!!");
198 }
199
200 representationEjb.setId(representationData.getOid());
201 representationEjb.setType(representationData.getType());
202 representationEjb.setName(representationData.getName());
203 MimeTypeEnum mimeType = MimeTypeEnum.forName(representationData.getMimeType());
204 representationEjb.setMimeType(mimeType);
205 representationEjb.setCreationDate(representationData.getCreationDate());
206
207 IContent.XmlType xmlContent = content.getXml();
208 byte[] xmlBytesContent = content.getXmlBytes();
209 String textContent = content.getText();
210 byte[] binaryContent = content.getBinary();
211 if (xmlContent != null) {
212 representationEjb.setContent(xmlContent.getAny());
213 } else if (xmlBytesContent != null) {
214 try {
215 InputStream is = new ByteArrayInputStream(xmlBytesContent);
216 representationEjb.setContent(XmlHelper.parse(new InputSource(is)));
217 } catch (XmlException e) {
218 LOGGER.error("Unable to parse XML representation: " + new String(xmlBytesContent), e);
219 } catch (IOException e) {
220
221 LOGGER.error("Unable to read XML bytes", e);
222 }
223 } else if (textContent != null) {
224 representationEjb.setContent(textContent);
225 } else if (binaryContent != null) {
226 representationEjb.setContent(binaryContent);
227 } else {
228 LOGGER.error("All representation content types are null");
229 }
230 }
231
232
233
234
235 protected String getSingleResourceQuery(String id) {
236 return ASSET_REPRESENTATION_QUERY + "[@oid='" + id + "']";
237 }
238
239 }