001: /*
002: * The contents of this file are subject to the
003: * Mozilla Public License Version 1.1 (the "License");
004: * you may not use this file except in compliance with the License.
005: * You may obtain a copy of the License at http://www.mozilla.org/MPL/
006: *
007: * Software distributed under the License is distributed on an "AS IS"
008: * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied.
009: * See the License for the specific language governing rights and
010: * limitations under the License.
011: *
012: * The Initial Developer of the Original Code is Simulacra Media Ltd.
013: * Portions created by Simulacra Media Ltd are Copyright (C) Simulacra Media Ltd, 2004.
014: *
015: * All Rights Reserved.
016: *
017: * Contributor(s):
018: */
019: package org.openharmonise.vfs.metadata;
020:
021: import java.util.ArrayList;
022: import java.util.Iterator;
023: import java.util.List;
024:
025: import org.openharmonise.vfs.*;
026: import org.openharmonise.vfs.event.*;
027: import org.openharmonise.vfs.metadata.range.*;
028: import org.openharmonise.vfs.metadata.value.*;
029: import org.openharmonise.vfs.servers.ServerList;
030:
031: /**
032: * A property instance is an item of metadata which is attached to a
033: * virtual file.
034: *
035: * @author Matthew Large
036: * @version $Revision: 1.2 $
037: *
038: */
039: public class PropertyInstance {
040:
041: /**
042: * Namespace of this property instance.
043: */
044: private String m_sNamespaceURI = null;
045:
046: /**
047: * Name of this property instance.
048: */
049: private String m_sName = null;
050:
051: /**
052: * Path to the virtual file for the property defining this property instance.
053: */
054: private String m_sPropDefnPath = null;
055:
056: /**
057: * Version identifier of the property defining this property instance.
058: */
059: private String m_sPropDefnVersion = null;
060:
061: /**
062: * List of {@link ValueInstance} objects which are the values for this property instance.
063: */
064: private ArrayList m_values = new ArrayList();
065:
066: /**
067: * Virtual file that owns this property instance
068: */
069: private VirtualFile m_vfFile = null;
070:
071: public PropertyInstance() {
072: super ();
073: }
074:
075: /**
076: * Constructs a new property instance.
077: *
078: * @param sNamespaceURI Namespace
079: * @param sName Name
080: */
081: public PropertyInstance(String sNamespaceURI, String sName) {
082: super ();
083: this .m_sNamespaceURI = sNamespaceURI;
084: this .m_sName = sName;
085: }
086:
087: /**
088: * Constructs a new property instance.
089: *
090: * @param sNamespaceURI Namespace
091: * @param sName Name
092: * @param aValues List of {@link ValueInstance} objects
093: */
094: public PropertyInstance(String sNamespaceURI, String sName,
095: List aValues) {
096: this (sNamespaceURI, sName);
097: this .m_values.addAll(aValues);
098: }
099:
100: /**
101: * Clears all the values for this property instance.
102: *
103: */
104: public void clearAllValues() {
105: this .m_values.clear();
106: if (this .m_vfFile != null) {
107: this .m_vfFile
108: .fireVirtualFileEvent(VirtualFileEvent.FILE_METADATA_CHANGED);
109: } else {
110: }
111: }
112:
113: /**
114: * Returns the namespace for this property instance.
115: *
116: * @return Namespace
117: */
118: public String getNamespaceURI() {
119: return this .m_sNamespaceURI;
120: }
121:
122: /**
123: * Sets the namespace for this property instance.
124: *
125: * @param sNamespaceURI Namespace
126: */
127: public void setNamespaceURI(String sNamespaceURI) {
128: this .m_sNamespaceURI = sNamespaceURI;
129: }
130:
131: /**
132: * Returns the name for this property instance.
133: *
134: * @return Name
135: */
136: public String getName() {
137: return this .m_sName;
138: }
139:
140: /**
141: * Sets the name for this property instance.
142: *
143: * @param sName Name
144: */
145: public void setName(String sName) {
146: this .m_sName = sName;
147: }
148:
149: /**
150: * Returns a list of the values for this property instance.
151: *
152: * @return List of {@link ValueInstance} objects
153: */
154: public List getValues() {
155: return (List) this .m_values.clone();
156: }
157:
158: /**
159: * Sets the value for this property instance.
160: *
161: * @param value Value
162: */
163: public void setValue(ValueInstance value) {
164: if (this .m_values.size() > 0) {
165: this .m_values.set(0, value);
166: } else {
167: this .m_values.add(value);
168: }
169: if (this .m_vfFile != null) {
170: this .m_vfFile
171: .fireVirtualFileEvent(VirtualFileEvent.FILE_METADATA_CHANGED);
172: } else {
173: }
174: }
175:
176: /**
177: * Sets the values for this property instance.
178: *
179: * @param aValues List of {@link ValueInstance} objects
180: */
181: public void setValues(List aValues) {
182: this .m_values = (ArrayList) aValues;
183: if (this .m_vfFile != null) {
184: this .m_vfFile
185: .fireVirtualFileEvent(VirtualFileEvent.FILE_METADATA_CHANGED);
186: } else {
187: }
188: }
189:
190: /**
191: * Adds a value to this property instance WITHOUT FIRING A VIRTUAL
192: * FILE EVENT. This is a bit of an evil cludge......use with caution,
193: * however it does do what it says on the tin!! ;D
194: *
195: * @param value Value
196: */
197: public void addValueWithoutFiringVirtualFileEvent(
198: ValueInstance value) {
199: boolean bAddValue = true;
200: if (this .getDefinition().getRange() instanceof ValueRange) {
201: if (!((ValueRange) this .getDefinition().getRange())
202: .validate(value).isValid()) {
203: bAddValue = false;
204: }
205: }
206:
207: if (bAddValue && !this .m_values.contains(value)) {
208: this .m_values.add(value);
209: }
210: }
211:
212: /**
213: * Adds a value to this property instance.
214: *
215: * @param value Value
216: */
217: public void addValue(ValueInstance value) {
218: boolean bAddValue = true;
219: if (this .getDefinition().getRange() instanceof ValueRange) {
220: if (!((ValueRange) this .getDefinition().getRange())
221: .validate(value).isValid()) {
222: bAddValue = false;
223: }
224: }
225:
226: if (bAddValue && !this .m_values.contains(value)) {
227: this .m_values.add(value);
228: }
229: if (bAddValue && this .m_vfFile != null) {
230: this .m_vfFile
231: .fireVirtualFileEvent(VirtualFileEvent.FILE_METADATA_CHANGED);
232: }
233: }
234:
235: /**
236: * Removes a value from this property instance.
237: *
238: * @param value Value
239: */
240: public void removeValue(ValueInstance value) {
241: this .m_values.remove(value);
242: if (this .m_vfFile != null) {
243: this .m_vfFile
244: .fireVirtualFileEvent(VirtualFileEvent.FILE_METADATA_CHANGED);
245: }
246: }
247:
248: /**
249: * Removes a value from this property instance.
250: *
251: * @param value Value
252: */
253: public void removeValueWithoutFiringVirtualFileEvent(
254: ValueInstance value) {
255: this .m_values.remove(value);
256: }
257:
258: /**
259: * Sets the version identifier and full path to the virtual file
260: * for the property which defines this property instance.
261: *
262: * @param sPath Full path
263: * @param sVersion Version identifier
264: */
265: public void setDefinitionPath(String sPath, String sVersion) {
266: this .m_sPropDefnPath = sPath;
267: this .m_sPropDefnVersion = sVersion;
268: }
269:
270: /**
271: * Returns the full path to the virtual file for the property
272: * which defines this property instance.
273: *
274: * @return Full path
275: */
276: public String getDefinitionPath() {
277: return this .m_sPropDefnPath;
278: }
279:
280: /**
281: * Returns the version identifier for the property which
282: * defines this property instance.
283: *
284: * @return Version identifier
285: */
286: public String getDefinitionVersion() {
287: return this .m_sPropDefnVersion;
288: }
289:
290: /**
291: * Returns the property which defines this property instance.
292: *
293: * @return Property
294: */
295: public Property getDefinition() {
296: if (this .m_sPropDefnPath != null) {
297: Property prop = PropertyCache.getInstance()
298: .getPropertyByPath(m_sPropDefnPath,
299: m_sPropDefnVersion);
300: if (prop != null) {
301: prop.setNamespace(m_sNamespaceURI);
302: return prop;
303: } else {
304: return PropertyCache.getInstance().getPropertyByName(
305: this .m_sNamespaceURI, this .m_sName);
306: }
307: } else {
308: return PropertyCache.getInstance().getPropertyByName(
309: this .m_sNamespaceURI, this .m_sName);
310: }
311: }
312:
313: public String toString() {
314: return this .m_sNamespaceURI + ":" + this .m_sName + ":"
315: + this .m_values;
316: }
317:
318: /**
319: * Sets the virtual file which owns this property instance.
320: *
321: * @param vfFile Virtual file
322: */
323: public void setVirtualFile(VirtualFile vfFile) {
324: this .m_vfFile = vfFile;
325: if (this .getDefinition() != null
326: && this .getDefinition().getRange() instanceof PropertyRange) {
327: Iterator itor = this .m_values.iterator();
328: while (itor.hasNext()) {
329: PropertyValue val = (PropertyValue) itor.next();
330: Iterator itor2 = val.getValue().iterator();
331: while (itor2.hasNext()) {
332: PropertyInstance propInst = (PropertyInstance) itor2
333: .next();
334: propInst.setVirtualFile(vfFile);
335: }
336: }
337: }
338: }
339:
340: /**
341: * Returns the virtual file which owns this property instance.
342: *
343: * @return Virtual file
344: */
345: public VirtualFile getVirtualFile() {
346: return this .m_vfFile;
347: }
348:
349: /**
350: * Returns a new value of the correct concrete type for this
351: * property instance.
352: *
353: * @return Value
354: */
355: public ValueInstance getNewValueInstance() {
356: if (this .m_vfFile.getVFS() != null) {
357: return this .m_vfFile.getVFS().getNewValueInstance(this );
358: } else {
359: return ServerList.getInstance().getHarmoniseServer()
360: .getVFS().getNewValueInstance(this );
361: }
362: }
363:
364: /* (non-Javadoc)
365: * @see java.lang.Object#equals(java.lang.Object)
366: */
367: public boolean equals(Object arg0) {
368: if (super .equals(arg0)) {
369: return true;
370: } else if (arg0 instanceof PropertyInstance) {
371: PropertyInstance propInst = (PropertyInstance) arg0;
372: if (propInst.m_sName.equals(this .m_sName)
373: && propInst.m_sNamespaceURI
374: .equals(this .m_sNamespaceURI)
375: && propInst.m_values.containsAll(this .m_values)) {
376: return true;
377: } else {
378: return false;
379: }
380: } else {
381: return false;
382: }
383: }
384:
385: }
|