001: /*
002: * Copyright 2005 Pentaho Corporation. All rights reserved.
003: * This software was developed by Pentaho Corporation and is provided under the terms
004: * of the Mozilla Public License, Version 1.1, or any later version. You may not use
005: * this file except in compliance with the license. If you need a copy of the license,
006: * please go to http://www.mozilla.org/MPL/MPL-1.1.txt. The Original Code is the Pentaho
007: * BI Platform. The Initial Developer is Pentaho Corporation.
008: *
009: * Software distributed under the Mozilla Public License is distributed on an "AS IS"
010: * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. Please refer to
011: * the license for the specific language governing your rights and limitations.
012: *
013: * Created Feb 8, 2006
014: * @author wseyler
015: */
016:
017: package com.pentaho.repository.dbbased.solution;
018:
019: import java.util.ArrayList;
020: import java.util.Iterator;
021: import java.util.List;
022: import java.util.Set;
023: import java.util.TreeSet;
024:
025: import org.acegisecurity.acl.basic.AclObjectIdentity;
026: import org.pentaho.core.solution.ISolutionFile;
027: import org.pentaho.messages.Messages;
028: import org.pentaho.repository.ISearchable;
029: import org.pentaho.util.UUIDUtil;
030:
031: import com.pentaho.security.acls.IAclSolutionFile;
032:
033: public class RepositoryFile implements ISearchable, Comparable,
034: AclObjectIdentity, IAclSolutionFile {
035: public static final char EXTENSION_CHAR = '.';
036:
037: private static final long serialVersionUID = -4129429077568560627L;
038:
039: public static final int ClassVersionNumber = 2;
040:
041: private static final String EMPTY_STRING = ""; //$NON-NLS-1$
042:
043: private static final String[] SearchableColumns = {
044: Messages.getString("SolutionRepository.QUERY_COLUMN_NAME"), //$NON-NLS-1$
045: Messages.getString("SolutionRepository.QUERY_COLUMN_PATH"), //$NON-NLS-1$
046: Messages
047: .getString("SolutionRepository.QUERY_COLUMN_PARENT") //$NON-NLS-1$
048: };
049:
050: private static final String SearchableTable = Messages
051: .getString("SolutionRepository.QUERY_HIBERNATE_TABLE"); //$NON-NLS-1$
052:
053: private static final String SearchablePhraseNamedQuery = Messages
054: .getString("SolutionRepository.QUERY_FOLDER_SEARCHER"); //$NON-NLS-1$
055:
056: public static final char SEPARATOR = '/';
057:
058: protected int revision;
059:
060: protected String fileId;
061:
062: protected RepositoryFile parent;
063:
064: protected String fileName;
065:
066: protected String fullPath;
067:
068: protected long lastModified;
069:
070: protected boolean directory = true;
071:
072: private byte[] data = null;
073:
074: private Set childrenFiles = new TreeSet();
075:
076: private List accessControls = new ArrayList();
077:
078: public RepositoryFile() {
079: super ();
080: }
081:
082: public RepositoryFile(String fileName, RepositoryFile parent,
083: byte[] data) {
084: this (fileName, parent, data, System.currentTimeMillis());
085: }
086:
087: public RepositoryFile(String fileName, RepositoryFile parent,
088: byte[] data, long lastModified) {
089: this ();
090: this .fileId = UUIDUtil.getUUIDAsString();
091:
092: this .fileName = fileName;
093: if (parent != null) {
094: parent.addChildFile(this );
095: }
096: setParent(parent);
097: setData(data);
098: setLastModified(lastModified);
099: directory = data == null;
100: }
101:
102: public int hashCode() {
103: return fileId.hashCode();
104: }
105:
106: public boolean equals(Object other) {
107: if (this == other) {
108: return true;
109: }
110: if (!(other instanceof RepositoryFile)) {
111: return false;
112: }
113: final RepositoryFile that = (RepositoryFile) other;
114: return getFileId().equals(that.getFileId());
115: }
116:
117: protected void resolvePath() {
118: StringBuffer buffer = new StringBuffer(EMPTY_STRING);
119:
120: if (parent != null) {
121: buffer.append(parent.getFullPath());
122: }
123: buffer.append(SEPARATOR);
124: buffer.append(fileName);
125:
126: setFullPath(buffer.toString());
127: }
128:
129: public List getAccessControls() {
130: return this .accessControls;
131: }
132:
133: public void setAccessControls(List acls) {
134: this .accessControls = acls;
135: }
136:
137: public void resetAccessControls(List acls) {
138: if (this .accessControls != null) {
139: this .accessControls.clear();
140: this .accessControls.addAll(acls);
141: }
142: }
143:
144: public int getRevision() {
145: return revision;
146: }
147:
148: protected void setRevision(int revision) {
149: this .revision = revision;
150: }
151:
152: public String getFileId() {
153: return fileId;
154: }
155:
156: protected void setFileId(String fileId) {
157: this .fileId = fileId;
158: }
159:
160: public String getSolution() {
161: return getTopFolder().getFileName();
162: }
163:
164: public String getSolutionPath() {
165: ArrayList pathList = new ArrayList();
166: ISolutionFile folder = parent;
167: while (!folder.isRoot()
168: && folder.retrieveParent().retrieveParent() != null) {
169: folder = folder.retrieveParent();
170: pathList.add(folder.getFileName());
171: }
172: StringBuffer buffer = new StringBuffer(EMPTY_STRING);
173: for (int i = pathList.size() - 1; i >= 0; i--) {
174: buffer.append(SEPARATOR);
175: buffer.append(pathList.get(i).toString());
176: }
177: return buffer.toString();
178: }
179:
180: public String getFileName() {
181: return fileName;
182: }
183:
184: protected void setFileName(String fileName) {
185: this .fileName = fileName;
186: resolvePath();
187: }
188:
189: public String getFullPath() {
190: return fullPath;
191: }
192:
193: protected void setFullPath(String fullPath) {
194: this .fullPath = fullPath;
195: }
196:
197: public void setParent(RepositoryFile parent) {
198: this .parent = parent;
199: resolvePath();
200: }
201:
202: public RepositoryFile getParent() {
203: return parent;
204: }
205:
206: public ISolutionFile retrieveParent() {
207: return parent;
208: }
209:
210: protected RepositoryFile getTopFolder() {
211: RepositoryFile topFolder = parent;
212: if (topFolder == null) {
213: return this ;
214: }
215: while (!topFolder.isRoot()) {
216: topFolder = (RepositoryFile) topFolder.retrieveParent();
217: }
218: return topFolder;
219: }
220:
221: /*
222: * (non-Javadoc)
223: *
224: * @see org.pentaho.repository.ISearchable#getSearchableColumns()
225: */
226: public String[] getSearchableColumns() {
227: return SearchableColumns;
228: }
229:
230: /*
231: * (non-Javadoc)
232: *
233: * @see org.pentaho.repository.ISearchable#getSearchableTable()
234: */
235: public String getSearchableTable() {
236: return SearchableTable;
237: }
238:
239: /*
240: * (non-Javadoc)
241: *
242: * @see org.pentaho.repository.ISearchable#getPhraseSearchQueryName()
243: */
244: public String getPhraseSearchQueryName() {
245: return SearchablePhraseNamedQuery;
246: }
247:
248: protected void setDirectory(boolean directory) {
249: this .directory = directory;
250: }
251:
252: protected boolean getDirectory() {
253: return directory;
254: }
255:
256: public boolean isDirectory() {
257: return getDirectory();
258: }
259:
260: /**
261: * @return Returns the childrenResources.
262: */
263: public Set getChildrenFiles() {
264: return childrenFiles;
265: }
266:
267: /**
268: * @param childrenResources
269: * The childrenResources to set.
270: */
271: public void setChildrenFiles(Set childrenFiles) {
272: this .childrenFiles = childrenFiles;
273: }
274:
275: public void addChildFile(RepositoryFile file) {
276: getChildrenFiles().add(file);
277: }
278:
279: public void removeChildFile(RepositoryFile file) {
280: getChildrenFiles().remove(file);
281: file.setParent(null); // as of now this file has no parent.
282: }
283:
284: /**
285: * @return Returns the data.
286: */
287: public byte[] getData() {
288: return data;
289: }
290:
291: /**
292: * @param data
293: * The data to set.
294: */
295: public void setData(byte[] data) {
296: this .data = data;
297: }
298:
299: public ISolutionFile[] listFiles() {
300: Object[] objArray = getChildrenFiles().toArray();
301: ISolutionFile[] childrenArray = new ISolutionFile[objArray.length];
302: for (int i = 0; i < objArray.length; i++) {
303: childrenArray[i] = (ISolutionFile) objArray[i];
304: }
305: return childrenArray;
306: }
307:
308: public RepositoryFile[] listRepositoryFiles() {
309: RepositoryFile[] files = new RepositoryFile[getChildrenFiles()
310: .size()];
311: Iterator iter = getChildrenFiles().iterator();
312: int i = 0;
313: while (iter.hasNext()) {
314: files[i] = (RepositoryFile) iter.next();
315: i++;
316: }
317: return files;
318: }
319:
320: public int compareTo(Object o) {
321: if (o == null) {
322: return 1;
323: } else if (o instanceof RepositoryFile) {
324: RepositoryFile that = (RepositoryFile) o;
325: if (this .getFullPath() == null
326: && that.getFullPath() == null) {
327: return 0;
328: } else if (this .getFullPath() == null
329: && that.getFullPath() != null) {
330: return -1;
331: } else if (this .getFullPath() != null
332: && that.getFullPath() == null) {
333: return 1;
334: } else {
335: return this .getFullPath().compareTo(
336: ((RepositoryFile) o).getFullPath());
337: }
338: } else {
339: return this .getFullPath().compareTo(o.toString());
340: }
341: }
342:
343: /**
344: * Chains up to find the access controls that are in force on this object.
345: * Could end up chaining all the way to the root.
346: */
347: public List getEffectiveAccessControls() {
348: List acls = this .getAccessControls();
349: if (acls.size() == 0) {
350: RepositoryFile chain = this ;
351: while (!chain.isRoot() && (acls.size() == 0)) {
352: chain = (RepositoryFile) chain.retrieveParent();
353: acls = chain.getAccessControls();
354: }
355: }
356: return acls;
357: }
358:
359: /**
360: * @return Returns the modDate.
361: */
362: public long getLastModified() {
363: return lastModified;
364: }
365:
366: /**
367: * @param modDate The modDate to set.
368: */
369: public void setLastModified(long modDate) {
370: this .lastModified = modDate;
371: }
372:
373: public boolean containsActions() {
374: boolean hasActions = false;
375: if (this .isDirectory()) {
376: Set children = getChildrenFiles();
377: Iterator iter = children.iterator();
378: while (iter.hasNext() && !hasActions) {
379: RepositoryFile file = (RepositoryFile) iter.next();
380: hasActions = file.getFileName().toLowerCase().endsWith(
381: ".xaction"); //$NON-NLS-1$
382: }
383: }
384: return hasActions;
385: }
386:
387: public boolean isRoot() {
388: return retrieveParent() == null;
389: }
390:
391: /**
392: * @return a boolean indicating if this file has an extension
393: */
394: public boolean hasExtension() {
395: return fileName.lastIndexOf(EXTENSION_CHAR) != -1;
396: }
397:
398: /**
399: * @return the extension (including the . seperator) of this file
400: */
401: public String getExtension() {
402: return hasExtension() ? fileName.substring(fileName
403: .lastIndexOf(EXTENSION_CHAR)) : ""; //$NON-NLS-1$
404: }
405:
406: public boolean exists() {
407: return true;
408: }
409:
410: }
|