001: /*
002: * Created on Jan 12, 2005
003: */
004: package org.openedit.repository.filesystem;
005:
006: import java.io.File;
007: import java.io.InputStream;
008: import java.util.ArrayList;
009: import java.util.List;
010:
011: import org.apache.commons.logging.Log;
012: import org.apache.commons.logging.LogFactory;
013: import org.openedit.repository.ContentItem;
014: import org.openedit.repository.Repository;
015: import org.openedit.repository.RepositoryException;
016:
017: import com.openedit.util.FileUtils;
018: import com.openedit.util.PathUtilities;
019:
020: /**
021: * @author cburkey
022: *
023: */
024: public class FileRepository implements Repository {
025: private static final Log log = LogFactory
026: .getLog(FileRepository.class);
027: protected File fieldRootDirectory;
028: protected FileUtils fieldFileUtils;
029:
030: /**
031: *
032: */
033: public FileRepository() {
034: }
035:
036: public FileRepository(File inRoot) {
037: setRootDirectory(inRoot);
038: }
039:
040: public void setRootDirectory(File inRoot) {
041: fieldRootDirectory = inRoot;
042: }
043:
044: /* (non-javadoc)
045: * @see com.einnovation.repository.Repository#get(java.lang.String)
046: */
047: public ContentItem get(String inPath) throws RepositoryException {
048: if (log.isDebugEnabled()) {
049: log.debug("reading:" + inPath);
050: }
051:
052: ContentItem revision = createContentItem(inPath);
053: if (inPath.endsWith(".html") && !revision.exists()) {
054: //try looking for an XML file of the same name
055: String xmlPath = PathUtilities.extractPagePath(inPath)
056: + ".xml";
057: ContentItem alternateRevision = createContentItem(xmlPath);
058: if (alternateRevision.exists()) {
059: alternateRevision.setActualPath(xmlPath);
060: revision = alternateRevision;
061: }
062: }
063: return revision;
064: }
065:
066: public ContentItem getStub(String inPath)
067: throws RepositoryException {
068: if (log.isDebugEnabled()) {
069: log.debug("reading:" + inPath);
070: }
071: return createContentItem(inPath);
072: }
073:
074: /* (non-javadoc)
075: * @see com.einnovation.repository.Repository#put(com.einnovation.repository.ContentItem)
076: */
077: public void put(ContentItem inContent) throws RepositoryException {
078: if (log.isDebugEnabled()) {
079: log.debug("saving:" + inContent.getPath());
080: }
081:
082: String path = inContent.getPath();
083: File file = getFile(path);
084: if (file.isDirectory()) {
085: throw new RepositoryException(
086: "Error attempting to write content to a "
087: + "folder instead of a file for path "
088: + path);
089: }
090: if (path.endsWith("/") && !file.exists()) {
091: file.mkdirs();
092: } else {
093: writeContent(inContent);
094: }
095: }
096:
097: protected File getFile(ContentItem inPath) {
098: if (inPath instanceof FileItem) {
099: FileItem f = (FileItem) inPath;
100: return f.getFile();
101: }
102: return new File(getRootDirectory(), inPath.getPath());
103: }
104:
105: /* (non-javadoc)
106: * @see com.einnovation.repository.Repository#copy(com.einnovation.repository.ContentItem, com.einnovation.repository.ContentItem)
107: */
108: public void copy(ContentItem inSource, ContentItem inDestination)
109: throws RepositoryException {
110: File sourceFile = getFile(inSource.getPath());
111: File destination = null;
112:
113: if (sourceFile.isDirectory()) {
114: destination = getFile(inDestination.getPath());
115: destination.mkdirs();
116: } else {
117: //why was I doing this? delete it
118: //String destContentPath = PathUtilities.extractPagePath( inDestination.getPath() ) + "."
119: // + PathUtilities.extractPageType( inSource.getPath() );
120:
121: destination = getFile(inDestination.getPath());
122: destination.getParentFile().mkdirs();
123: }
124:
125: copyFiles(sourceFile, destination);
126:
127: }
128:
129: public void move(ContentItem inSource, ContentItem inDestination)
130: throws RepositoryException {
131: File todelete = getFile(inSource.getPath());
132: File toMove = getFile(inDestination.getPath());
133: if (inDestination.getPath().endsWith("/")) {
134: toMove.mkdirs();
135: }
136: moveFiles(todelete, toMove);
137: }
138:
139: /* (non-javadoc)
140: * @see com.einnovation.repository.Repository#remove(com.einnovation.repository.ContentItem)
141: */
142: public void remove(ContentItem inContentItem)
143: throws RepositoryException {
144: File todelete = getFile(inContentItem.getPath());
145:
146: deleteAll(todelete);
147: }
148:
149: /* (non-javadoc)
150: * @see com.einnovation.repository.Repository#getContentItems(java.lang.String)
151: */
152: public List getVersions(String inPath) throws RepositoryException {
153: List list = new ArrayList(1);
154: list.add(createContentItem(inPath));
155: return list;
156: }
157:
158: protected ContentItem createContentItem(String inPath) {
159: FileItem contentItem = new FileItem();
160: contentItem.setPath(inPath);
161: contentItem.setFile(getFile(inPath));
162: return contentItem;
163: }
164:
165: protected void deleteAll(File todelete) {
166: getFileUtils().deleteAll(todelete);
167: }
168:
169: public FileUtils getFileUtils() {
170: if (fieldFileUtils == null) {
171: fieldFileUtils = new FileUtils();
172: }
173: return fieldFileUtils;
174: }
175:
176: protected File getFile(String inPath) {
177: return new File(getRootDirectory(), inPath);
178: }
179:
180: public File getRootDirectory() {
181: return fieldRootDirectory;
182: }
183:
184: protected void copyFiles(File source, File destination)
185: throws RepositoryException {
186: try {
187: getFileUtils().copyFiles(source, destination);
188: } catch (Exception e) {
189: throw new RepositoryException(e);
190: }
191: }
192:
193: protected void moveFiles(File source, File destination)
194: throws RepositoryException {
195: try {
196: if (!source.renameTo(destination)) {
197: getFileUtils().copyFiles(source, destination);
198: getFileUtils().deleteAll(source);
199: }
200: } catch (Exception e) {
201: throw new RepositoryException(e);
202: }
203: }
204:
205: protected void writeContent(ContentItem inContentItem)
206: throws RepositoryException {
207: if (inContentItem instanceof FileItem) {
208: return; //else we would end up saving it onto itself
209: }
210: File file = getFile(inContentItem.getPath());
211: InputStream in = inContentItem.getInputStream();
212: if (in == null) {
213: throw new RepositoryException(
214: "Content item did not have an input stream "
215: + inContentItem.getPath());
216: }
217: try {
218: getFileUtils().writeFile(in, file);
219: } catch (Exception e) {
220: throw new RepositoryException(
221: "Error writing content for path "
222: + inContentItem.getPath(), e);
223: }
224: }
225:
226: public boolean doesExist(String inPath) throws RepositoryException {
227: File file = getFile(inPath);
228:
229: boolean exists = file.exists();
230:
231: if (!exists && inPath.endsWith(".html")) {
232: String xmlPath = PathUtilities.extractPagePath(inPath)
233: + ".xml";
234: file = getFile(inPath);
235: exists = file.exists();
236: }
237: return exists;
238: }
239:
240: public ContentItem getLastVersion(String inPath)
241: throws RepositoryException {
242: return createContentItem(inPath);
243: }
244:
245: public List getChildrenNames(String inParent)
246: throws RepositoryException {
247: if (inParent.endsWith("/")) {
248: inParent = inParent.substring(0, inParent.length() - 1);
249: }
250: List children = new ArrayList();
251: File file = getFile(inParent);
252: File[] all = file.listFiles();
253: if (all != null) {
254: for (int i = 0; i < all.length; i++) {
255: String name = inParent + "/" + all[i].getName();
256: children.add(name);
257: }
258: }
259: return children;
260: }
261:
262: public void deleteOldVersions(String inPath)
263: throws RepositoryException {
264:
265: }
266:
267: }
|