001: /**********************************************************************************
002: * $URL: https://source.sakaiproject.org/svn/metaobj/tags/sakai_2-4-1/metaobj-impl/api-impl/src/java/org/sakaiproject/metaobj/shared/mgt/impl/ContentResourceHome.java $
003: * $Id: ContentResourceHome.java 21318 2007-02-10 23:53:53Z john.ellis@rsmart.com $
004: ***********************************************************************************
005: *
006: * Copyright (c) 2004, 2005, 2006 The Sakai Foundation.
007: *
008: * Licensed under the Educational Community License, Version 1.0 (the "License");
009: * you may not use this file except in compliance with the License.
010: * You may obtain a copy of the License at
011: *
012: * http://www.opensource.org/licenses/ecl1.php
013: *
014: * Unless required by applicable law or agreed to in writing, software
015: * distributed under the License is distributed on an "AS IS" BASIS,
016: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
017: * See the License for the specific language governing permissions and
018: * limitations under the License.
019: *
020: **********************************************************************************/package org.sakaiproject.metaobj.shared.mgt.impl;
021:
022: import java.io.ByteArrayInputStream;
023: import java.io.IOException;
024: import java.io.InputStream;
025: import java.util.Collection;
026: import java.util.Date;
027: import java.util.Iterator;
028: import java.util.List;
029:
030: import org.jdom.CDATA;
031: import org.jdom.Document;
032: import org.jdom.Element;
033: import org.jdom.JDOMException;
034: import org.jdom.input.SAXBuilder;
035: import org.sakaiproject.content.api.ContentResource;
036: import org.sakaiproject.entity.api.EntityPropertyNotDefinedException;
037: import org.sakaiproject.entity.api.EntityPropertyTypeException;
038: import org.sakaiproject.entity.api.ResourceProperties;
039: import org.sakaiproject.exception.ServerOverloadException;
040: import org.sakaiproject.metaobj.shared.mgt.*;
041: import org.sakaiproject.metaobj.shared.mgt.home.StructuredArtifactHomeInterface;
042: import org.sakaiproject.metaobj.shared.model.Agent;
043: import org.sakaiproject.metaobj.shared.model.Artifact;
044: import org.sakaiproject.metaobj.shared.model.ContentResourceArtifact;
045: import org.sakaiproject.metaobj.shared.model.FinderException;
046: import org.sakaiproject.metaobj.shared.model.Id;
047: import org.sakaiproject.metaobj.shared.model.MimeType;
048: import org.sakaiproject.metaobj.shared.model.PersistenceException;
049: import org.sakaiproject.metaobj.shared.model.Type;
050: import org.sakaiproject.metaobj.utils.xml.SchemaNode;
051: import org.sakaiproject.time.api.Time;
052:
053: /**
054: * Created by IntelliJ IDEA.
055: * User: John Ellis
056: * Date: Sep 14, 2005
057: * Time: 10:08:18 AM
058: * To change this template use File | Settings | File Templates.
059: */
060: public class ContentResourceHome implements ReadableObjectHome,
061: PresentableObjectHome {
062:
063: private HomeFactory homeFactory;
064: private IdManager idManager;
065:
066: public Type getType() {
067: return new Type(getIdManager().getId("fileArtifact"),
068: "Uploaded File");
069: }
070:
071: public String getExternalType() {
072: return "fileArtifact";
073: }
074:
075: public Artifact load(Id id) throws PersistenceException {
076: throw new UnsupportedOperationException("not implemented");
077: }
078:
079: public Artifact createInstance() {
080: throw new UnsupportedOperationException("not implemented");
081: }
082:
083: public void prepareInstance(Artifact object) {
084: throw new UnsupportedOperationException("not implemented");
085: }
086:
087: public Artifact createSample() {
088: throw new UnsupportedOperationException("not implemented");
089: }
090:
091: public Collection findByOwner(Agent owner) throws FinderException {
092: throw new UnsupportedOperationException("not implemented");
093: }
094:
095: public boolean isInstance(Artifact testObject) {
096: throw new UnsupportedOperationException("not implemented");
097: }
098:
099: public void refresh() {
100: throw new UnsupportedOperationException("not implemented");
101: }
102:
103: public String getExternalUri(Id artifactId, String name) {
104: throw new UnsupportedOperationException("not implemented");
105: }
106:
107: public InputStream getStream(Id artifactId) {
108: throw new UnsupportedOperationException("not implemented");
109: }
110:
111: public boolean isSystemOnly() {
112: throw new UnsupportedOperationException("not implemented");
113: }
114:
115: public Class getInterface() {
116: throw new UnsupportedOperationException("not implemented");
117: }
118:
119: public Element getArtifactAsXml(Artifact art) {
120: ContentResourceArtifact artifact = (ContentResourceArtifact) art;
121: Element root = new Element("artifact");
122:
123: root.addContent(getMetadata(artifact));
124:
125: String type = artifact.getBase().getProperties().getProperty(
126: ResourceProperties.PROP_STRUCTOBJ_TYPE);
127:
128: if (type == null) {
129: addFileContent(artifact, root);
130: } else {
131: addStructuredObjectContent(type, artifact, root);
132: }
133:
134: return root;
135: }
136:
137: protected void addStructuredObjectContent(String type,
138: ContentResourceArtifact artifact, Element root) {
139: Element data = new Element("structuredData");
140: Element baseElement = null;
141:
142: byte[] bytes = null;
143: try {
144: bytes = artifact.getBase().getContent();
145: } catch (ServerOverloadException e) {
146: throw new RuntimeException(e);
147: }
148: SAXBuilder builder = new SAXBuilder();
149: Document doc = null;
150:
151: try {
152: doc = builder.build(new ByteArrayInputStream(bytes));
153: } catch (JDOMException e) {
154: throw new RuntimeException(e);
155: } catch (IOException e) {
156: throw new RuntimeException(e);
157: }
158:
159: baseElement = (Element) doc.getRootElement().detach();
160:
161: data.addContent(baseElement);
162: root.addContent(data);
163:
164: StructuredArtifactHomeInterface home = (StructuredArtifactHomeInterface) getHomeFactory()
165: .getHome(type);
166:
167: Element schemaData = new Element("schema");
168: schemaData.addContent(createInstructions(home));
169: schemaData.addContent(addSchemaInfo(home.getRootSchema()));
170: root.addContent(schemaData);
171: }
172:
173: protected Element createInstructions(
174: StructuredArtifactHomeInterface home) {
175: Element instructions = new Element("instructions");
176: instructions.setContent(new CDATA(home.getInstruction()));
177: return instructions;
178: }
179:
180: protected Element addSchemaInfo(SchemaNode schema) {
181: Element schemaElement = new Element("element");
182: schemaElement.setAttribute("name", schema.getName());
183: if (schema.getType() != null
184: && schema.getType().getBaseType() != null) {
185: schemaElement.setAttribute("type", schema.getType()
186: .getBaseType());
187: }
188: schemaElement.setAttribute("minOccurs", schema.getMinOccurs()
189: + "");
190: schemaElement.setAttribute("maxOccurs", schema.getMaxOccurs()
191: + "");
192: Element annotation = schema.getSchemaElement().getChild(
193: "annotation", schema.getSchemaElement().getNamespace());
194:
195: if (annotation != null) {
196: schemaElement.addContent(annotation.detach());
197: }
198:
199: Element simpleType = schema.getSchemaElement().getChild(
200: "simpleType", schema.getSchemaElement().getNamespace());
201:
202: if (simpleType != null) {
203: schemaElement.addContent(simpleType.detach());
204: }
205:
206: List children = schema.getChildren();
207: Element childElement = new Element("children");
208: boolean found = false;
209: for (Iterator i = children.iterator(); i.hasNext();) {
210: childElement
211: .addContent(addSchemaInfo((SchemaNode) i.next()));
212: found = true;
213: }
214:
215: if (found) {
216: schemaElement.addContent(childElement);
217: }
218:
219: return schemaElement;
220: }
221:
222: protected void addFileContent(ContentResourceArtifact artifact,
223: Element root) {
224: Element fileData = new Element("fileArtifact");
225: Element uri = new Element("uri");
226: uri.addContent(artifact.getBase().getUrl());
227: fileData.addContent(uri);
228:
229: root.addContent(fileData);
230: }
231:
232: protected Element getMetadata(ContentResourceArtifact art) {
233: Element root = new Element("metaData");
234: root.addContent(ContentHostingUtil.createNode("id", art.getId()
235: .getValue()));
236: root.addContent(ContentHostingUtil.createNode("displayName",
237: art.getDisplayName()));
238:
239: Element type = new Element("type");
240: root.addContent(type);
241:
242: type.addContent(ContentHostingUtil.createNode("id", "file"));
243: type.addContent(ContentHostingUtil.createNode("description",
244: "file"));
245:
246: ContentResource contentResource = art.getBase();
247: Element repositoryNode = ContentHostingUtil
248: .createRepoNode(contentResource);
249: root.addContent(repositoryNode);
250:
251: repositoryNode.addContent(ContentHostingUtil.createNode("size",
252: "" + contentResource.getContentLength()));
253:
254: Element mimeType = new Element("mimeType");
255: repositoryNode.addContent(mimeType);
256: String mimeTypeString = contentResource.getContentType();
257: MimeType mime = new MimeType(mimeTypeString);
258: mimeType.addContent(ContentHostingUtil.createNode("primary",
259: mime.getPrimaryType()));
260: mimeType.addContent(ContentHostingUtil.createNode("sub", mime
261: .getSubType()));
262:
263: return root;
264: }
265:
266: public HomeFactory getHomeFactory() {
267: return homeFactory;
268: }
269:
270: public void setHomeFactory(HomeFactory homeFactory) {
271: this .homeFactory = homeFactory;
272: }
273:
274: public IdManager getIdManager() {
275: return idManager;
276: }
277:
278: public void setIdManager(IdManager idManager) {
279: this.idManager = idManager;
280: }
281:
282: }
|