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/home/XmlElementHome.java $
003: * $Id: XmlElementHome.java 20850 2007-02-01 00:05:58Z 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.home;
021:
022: import java.io.File;
023: import java.io.FileOutputStream;
024: import java.io.IOException;
025: import java.io.InputStream;
026: import java.util.ArrayList;
027: import java.util.Collection;
028: import java.util.Date;
029: import java.util.List;
030:
031: import org.apache.commons.logging.Log;
032: import org.apache.commons.logging.LogFactory;
033: import org.jdom.Document;
034: import org.jdom.Element;
035: import org.jdom.input.SAXBuilder;
036: import org.jdom.output.Format;
037: import org.jdom.output.XMLOutputter;
038: import org.sakaiproject.metaobj.shared.mgt.IdManager;
039: import org.sakaiproject.metaobj.shared.model.Agent;
040: import org.sakaiproject.metaobj.shared.model.Artifact;
041: import org.sakaiproject.metaobj.shared.model.FinderException;
042: import org.sakaiproject.metaobj.shared.model.Id;
043: import org.sakaiproject.metaobj.shared.model.OspException;
044: import org.sakaiproject.metaobj.shared.model.PersistenceException;
045: import org.sakaiproject.metaobj.shared.model.StructuredArtifact;
046: import org.sakaiproject.metaobj.shared.model.Type;
047: import org.sakaiproject.metaobj.utils.xml.SchemaFactory;
048: import org.sakaiproject.metaobj.utils.xml.SchemaInvalidException;
049: import org.sakaiproject.metaobj.utils.xml.SchemaNode;
050: import org.sakaiproject.tool.cover.ToolManager;
051: import org.sakaiproject.content.api.ContentResource;
052: import org.springframework.beans.factory.InitializingBean;
053: import org.springframework.context.ResourceLoaderAware;
054: import org.springframework.core.io.ResourceLoader;
055:
056: /**
057: * Created by IntelliJ IDEA.
058: * User: John Ellis
059: * Date: Apr 9, 2004
060: * Time: 1:22:35 PM
061: * To change this template use File | Settings | File Templates.
062: */
063: public class XmlElementHome implements StructuredArtifactHomeInterface,
064: InitializingBean, ResourceLoaderAware {
065:
066: private SchemaNode schema = null;
067: private String rootNode = null;
068: private Date schemaDate = null;
069: protected final Log logger = LogFactory.getLog(getClass());
070: private File homeDirectory = null;
071: private String schemaFileName;
072: private Type type = null;
073: private String typeId = null;
074: private IdManager idManager = null;
075: public static final String XSD_DIR = "xsd";
076: public static final String XML_HOME_PATH = "xmlHome";
077: private ResourceLoader resourceLoader;
078: /**
079: * help information supplied to the user when creating an instance of this xmlelement
080: */
081: private String instruction;
082:
083: public XmlElementHome() {
084: }
085:
086: public XmlElementHome(String rootNode) {
087: this .rootNode = rootNode;
088: }
089:
090: public SchemaNode getSchema() {
091: if (schema == null) {
092: File schemaFile = getSchemaFile(schemaFileName);
093: schema = SchemaFactory.getInstance().getSchema(schemaFile);
094: schemaDate = new Date(schemaFile.lastModified());
095: }
096: return schema;
097: }
098:
099: public String getDocumentRoot() {
100: return null;
101: }
102:
103: protected File getSchemaFile(String schemaFileName) {
104: return new File(this .pathToWebInf() + File.separator + XSD_DIR
105: + File.separator + schemaFileName);
106: }
107:
108: public void setSchema(SchemaNode schema) {
109: this .schema = schema;
110: }
111:
112: public Artifact store(Artifact object) throws PersistenceException {
113: String id = (String) object.getId().getValue();
114:
115: File objectFile = null;
116:
117: if (id == null) {
118: try {
119: objectFile = File.createTempFile(rootNode, ".xml",
120: homeDirectory);
121: } catch (IOException e) {
122: logger.error("", e);
123: throw new OspException(e);
124: }
125: } else {
126: objectFile = new File(homeDirectory, id);
127: if (objectFile.exists()) {
128: objectFile.delete();
129: }
130: }
131:
132: XMLOutputter outputter = new XMLOutputter();
133: StructuredArtifact xmlObject = (StructuredArtifact) object;
134:
135: xmlObject.setId(objectFile.getName());
136:
137: try {
138: Format format = Format.getPrettyFormat();
139: outputter.setFormat(format);
140: outputter.output(xmlObject.getBaseElement(),
141: new FileOutputStream(objectFile));
142: } catch (IOException e) {
143: logger.error("", e);
144: throw new OspException(e);
145: }
146:
147: return object;
148: }
149:
150: public void remove(Artifact object) throws PersistenceException {
151: File objectFile = null;
152:
153: objectFile = new File(homeDirectory, object.getId().getValue());
154:
155: objectFile.delete();
156: }
157:
158: public Artifact store(String displayName, String contentType,
159: Type type, InputStream in) throws PersistenceException {
160: // todo complete
161: return null;
162: }
163:
164: public Artifact update(Artifact object, InputStream in)
165: throws PersistenceException {
166: return null;//todo
167: }
168:
169: public Type getType() {
170: return type;
171: }
172:
173: public String getExternalType() {
174: if (getSchema() == null) {
175: return "";
176: }
177: return getSchema().getTargetNamespace().getURI() + "?"
178: + getRootNode();
179: }
180:
181: public void setType(Type type) {
182: this .type = type;
183: }
184:
185: public Artifact load(Id id) throws PersistenceException {
186: return load(id.getValue());
187: }
188:
189: public StructuredArtifact load(ContentResource resource) {
190: return null;
191: }
192:
193: protected Artifact load(String id) throws PersistenceException {
194: File objectFile = new File(homeDirectory, id);
195:
196: SAXBuilder builder = new SAXBuilder();
197:
198: try {
199: Document doc = builder.build(objectFile);
200:
201: StructuredArtifact xmlObject = new StructuredArtifact(doc
202: .getRootElement(), getSchema().getChild(rootNode));
203:
204: xmlObject.setId(id);
205:
206: xmlObject.setHome(this );
207:
208: return xmlObject;
209: } catch (Exception e) {
210: throw new SchemaInvalidException(e);
211: }
212: }
213:
214: public Artifact createInstance() {
215: StructuredArtifact instance = new StructuredArtifact(rootNode,
216: getSchema().getChild(rootNode));
217: prepareInstance(instance);
218: return instance;
219: }
220:
221: public void prepareInstance(Artifact object) {
222: object.setHome(this );
223: StructuredArtifact xmlObject = (StructuredArtifact) object;
224: xmlObject.getBaseElement().setName(rootNode);
225: }
226:
227: public Artifact createSample() {
228: return createInstance();
229: }
230:
231: public Collection findByOwner(Agent owner) throws FinderException {
232: // really just list all here for now...
233: String[] files = homeDirectory.list();
234:
235: List returnedList = new ArrayList();
236:
237: for (int i = 0; i < files.length; i++) {
238: try {
239: returnedList.add(load(files[i]));
240: } catch (PersistenceException e) {
241: throw new FinderException();
242: }
243: }
244:
245: return returnedList;
246: }
247:
248: public boolean isInstance(Artifact testObject) {
249: return (testObject instanceof StructuredArtifact);
250: }
251:
252: public void refresh() {
253: schema = null;
254: getSchema();
255: }
256:
257: public String getExternalUri(Id artifactId, String name) {
258: //http://johnellis.rsmart.com:8080/osp/member/viewNode.osp?pid=1107451588272-643&nodeId=48D2AFE5A98453AD673579E14405607C
259: return "viewNode.osp?pid="
260: + ToolManager.getCurrentPlacement().getId()
261: + "&nodeId=" + artifactId.getValue();
262: }
263:
264: public InputStream getStream(Id artifactId) {
265: // todo ... implement this
266: return null;
267: }
268:
269: public boolean isSystemOnly() {
270: return false;
271: }
272:
273: public Class getInterface() {
274: return StructuredArtifactHomeInterface.class;
275: }
276:
277: public String getRootNode() {
278: return rootNode;
279: }
280:
281: public void setRootNode(String rootNode) {
282: this .rootNode = rootNode;
283: }
284:
285: public Date getModified() {
286: return schemaDate;
287: }
288:
289: public void setModified(Date schemaDate) {
290: this .schemaDate = schemaDate;
291: }
292:
293: public String getSchemaFileName() {
294: return schemaFileName;
295: }
296:
297: public void setSchemaFileName(String schemaFileName) {
298: this .schemaFileName = schemaFileName;
299: }
300:
301: /**
302: * Invoked by a BeanFactory after it has set all bean properties supplied
303: * (and satisfied BeanFactoryAware and ApplicationContextAware).
304: * <p>This method allows the bean instance to perform initialization only
305: * possible when all bean properties have been set and to throw an
306: * exception in the event of misconfiguration.
307: *
308: * @throws Exception in the event of misconfiguration (such
309: * as failure to set an essential property) or if initialization fails.
310: */
311: public void afterPropertiesSet() throws Exception {
312: homeDirectory = new File(pathToWebInf(), XML_HOME_PATH
313: + File.separator + rootNode);
314:
315: if (!homeDirectory.exists()) {
316: homeDirectory.mkdirs();
317: }
318:
319: getSchema();
320: getType().setId(getIdManager().getId(getTypeId()));
321: }
322:
323: protected String pathToWebInf() {
324: try {
325: return resourceLoader.getResource("WEB-INF").getFile()
326: .getCanonicalPath();
327: } catch (IOException e) {
328: throw new RuntimeException(e);
329: }
330: }
331:
332: public IdManager getIdManager() {
333: return idManager;
334: }
335:
336: public void setIdManager(IdManager idManager) {
337: this .idManager = idManager;
338: }
339:
340: public String getTypeId() {
341: return typeId;
342: }
343:
344: public byte[] getBytes(StructuredArtifact artifact) {
345: return new byte[0];
346: }
347:
348: public void setTypeId(String typeId) {
349: this .typeId = typeId;
350: }
351:
352: public void setResourceLoader(ResourceLoader resourceLoader) {
353: this .resourceLoader = resourceLoader;
354: }
355:
356: public String getInstruction() {
357: return instruction;
358: }
359:
360: public void setInstruction(String instruction) {
361: this .instruction = instruction;
362: }
363:
364: public SchemaNode getRootSchema() {
365: return getSchema().getChild(getRootNode());
366: }
367:
368: public String getSiteId() {
369: return null;
370: }
371:
372: public Artifact cloneArtifact(Artifact copy, String newName)
373: throws PersistenceException {
374: return null;
375: }
376:
377: public Element getArtifactAsXml(Artifact art) {
378: return null;
379: }
380: }
|