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.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      /* (non-Javadoc)
65       * @see no.sintef.assetrepository.dao.AssetRepresentationDAO#load(no.sintef.assetrepository.interfaces.AssetRepresentationPK, no.sintef.assetrepository.entity.AssetRepresentationEJB)
66       */
67      public void load(AssetRepresentationPK pk, AssetRepresentationEJB ejb)
68              throws EJBException {
69          super.load(pk, ejb);
70      }
71  
72      /* (non-Javadoc)
73       * @see no.sintef.assetrepository.dao.AssetRepresentationDAO#store(no.sintef.assetrepository.entity.AssetRepresentationEJB)
74       */
75      public void store(AssetRepresentationEJB ejb) throws EJBException {
76          super.store(ejb);
77      }
78  
79      /* (non-Javadoc)
80       * @see no.sintef.assetrepository.dao.AssetRepresentationDAO#remove(no.sintef.assetrepository.interfaces.AssetRepresentationPK)
81       */
82      public void remove(AssetRepresentationPK pk) throws RemoveException,
83              EJBException {
84          super.remove(pk);
85      }
86  
87      /* (non-Javadoc)
88       * @see no.sintef.assetrepository.dao.AssetRepresentationDAO#create(no.sintef.assetrepository.entity.AssetRepresentationEJB)
89       */
90      public AssetRepresentationPK create(AssetRepresentationEJB ejb)
91              throws CreateException, EJBException {
92          return (AssetRepresentationPK) super.create(ejb);
93      }
94  
95      /* (non-Javadoc)
96       * @see no.sintef.assetrepository.dao.AssetRepresentationDAO#findByPrimaryKey(no.sintef.assetrepository.interfaces.AssetRepresentationPK)
97       */
98      public AssetRepresentationPK findByPrimaryKey(AssetRepresentationPK pk)
99              throws FinderException {
100         return (AssetRepresentationPK) super.findByPrimaryKey(pk);
101     }
102 
103     /* (non-Javadoc)
104      * @see no.sintef.assetrepository.dao.AssetRepresentationDAO#findAll()
105      */
106     public Collection findAll() throws FinderException {
107         return getPrimaryKeys(ASSET_REPRESENTATION_QUERY);
108     }
109 
110     /* (non-Javadoc)
111      * @see no.sintef.assetrepository.dao.AbstractDAO#getId(java.lang.Object)
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     /* (non-Javadoc)
131      * @see no.sintef.assetrepository.dao.AbstractDAO#getPrimaryKey(java.lang.String)
132      */
133     protected Object getPrimaryKey(String id) {
134         return new AssetRepresentationPK(id);
135     }
136 
137     /* (non-Javadoc)
138      * @see no.sintef.assetrepository.dao.AbstractDAO#createJaxbObject(java.lang.String)
139      */
140     private Object createJaxbObject(String id) {
141         IAssetRepresentation representationData = new AssetRepresentationImpl();
142         representationData.setOid(id);
143         return representationData;
144     }
145 
146     /* (non-Javadoc)
147      * @see no.sintef.assetrepository.dao.AbstractDAO#createJaxbObject(javax.ejb.EntityBean)
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; // FIXME
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     /* (non-Javadoc)
184      * @see no.sintef.assetrepository.dao.AbstractDAO#setJaxbObject(javax.ejb.EntityBean, java.lang.Object)
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                 // This should never happen!
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     /* (non-Javadoc)
233      * @see no.sintef.assetrepository.dao.AbstractDAO#getSingleResourceQuery(java.lang.String)
234      */
235     protected String getSingleResourceQuery(String id) {
236         return ASSET_REPRESENTATION_QUERY + "[@oid='" + id + "']";
237     }
238 
239 }