001: /*
002: * Copyright 2001-2007 Geert Bevin <gbevin[remove] at uwyn dot com>
003: * Distributed under the terms of either:
004: * - the common development and distribution license (CDDL), v1.0; or
005: * - the GNU Lesser General Public License, v2.1 or later
006: * $Id: ContentInfo.java 3634 2007-01-08 21:42:24Z gbevin $
007: */
008: package com.uwyn.rife.cmf;
009:
010: import com.uwyn.rife.site.ConstrainedProperty;
011: import com.uwyn.rife.site.Validation;
012: import com.uwyn.rife.tools.Localization;
013: import java.sql.Timestamp;
014: import java.text.NumberFormat;
015: import java.util.Map;
016:
017: /**
018: * This class represents all the information that is stored in the backend
019: * about a certain {@link com.uwyn.rife.cmf.Content Content} instance.
020: * <p>The setters of this class are only present to make it possible for the
021: * back-ends to automatically populate the information.
022: *
023: * @author Geert Bevin (gbevin[remove] at uwyn dot com)
024: * @version $Revision: 3634 $
025: * @since 1.0
026: */
027: public class ContentInfo extends Validation {
028: private String mPath = null;
029: private int mVersion = -1;
030: private Timestamp mCreated = null;
031: private String mMimeType = null;
032: private boolean mFragment = false;
033: private String mName = null;
034: private Map<String, String> mAttributes = null;
035: private int mSize = -1;
036: private Map<String, String> mProperties = null;
037:
038: /**
039: * Instantiates a new <code>ContentInfo</code> instance.
040: */
041: public ContentInfo() {
042: }
043:
044: public void activateValidation() {
045: addConstraint(new ConstrainedProperty("path").notNull(true)
046: .notEmpty(true).maxLength(255));
047: addConstraint(new ConstrainedProperty("mimeType").notNull(true)
048: .notEmpty(true).maxLength(80));
049: addConstraint(new ConstrainedProperty("version").notNull(true)
050: .rangeBegin(0).saved(false));
051: addConstraint(new ConstrainedProperty("name").maxLength(100));
052: addConstraint(new ConstrainedProperty("created").notNull(true)
053: .saved(false));
054: addConstraint(new ConstrainedProperty("attributes")
055: .persistent(false));
056: addConstraint(new ConstrainedProperty("size").persistent(false));
057: addConstraint(new ConstrainedProperty("properties")
058: .persistent(false));
059: }
060:
061: /**
062: * Sets the path of the stored <code>Content</code> instance.
063: * <p>The path has to be unique and will be used to retrieve this
064: * particular <code>Content</code>.
065: *
066: * @param path the absolute and unique path
067: * @see #getPath()
068: * @since 1.0
069: */
070: public void setPath(String path) {
071: mPath = path;
072: }
073:
074: /**
075: * Retrieves the path of the stored <code>Content</code> instance.
076: *
077: * @return the <code>Content</code>'s path
078: * @see #setPath(String)
079: * @see #getOptimalPath()
080: * @since 1.0
081: */
082: public String getPath() {
083: return mPath;
084: }
085:
086: /**
087: * Retrieves the path of the stored <code>Content</code> instance in the
088: * most optimal form for usage in the cmf.
089: *
090: * @return the <code>Content</code>'s most optimal path
091: * @see #getPath()
092: * @since 1.0
093: */
094: public String getOptimalPath() {
095: if (null == mPath) {
096: return null;
097: }
098:
099: if (null == mName) {
100: return mPath;
101: }
102:
103: StringBuilder result = new StringBuilder(mPath);
104: result.append("/");
105: result.append(mName);
106: return result.toString();
107: }
108:
109: /**
110: * Sets the version of the stored <code>Content</code> instance.
111: * <p>Version numbers are unique and should be increased successively when
112: * the data on a certain <code>Content</code> is updated.
113: * <p>The path and the version together identify exactly one particular
114: * <code>Content</code> with one particular data.
115: *
116: * @param version the version as a unique integer
117: * @see #getVersion()
118: * @since 1.0
119: */
120: public void setVersion(int version) {
121: mVersion = version;
122: }
123:
124: /**
125: * Retrieves the version of the stored <code>Content</code> instance.
126: *
127: * @return the <code>Content</code>'s version
128: * @see #setVersion(int)
129: * @since 1.0
130: */
131: public int getVersion() {
132: return mVersion;
133: }
134:
135: /**
136: * Sets the mime type of the stored <code>Content</code> instance.
137: *
138: * @param mimeType the <code>String</code> that identifies the mime type
139: * @see #getMimeType()
140: * @since 1.0
141: */
142: public void setMimeType(String mimeType) {
143: mMimeType = mimeType;
144: }
145:
146: /**
147: * Retrieves the mime type of the stored <code>Content</code> instance.
148: *
149: * @return the <code>Content</code>'s mime type textual identifier
150: * @see #setMimeType(String)
151: * @since 1.0
152: */
153: public String getMimeType() {
154: return mMimeType;
155: }
156:
157: /**
158: * Sets wether the stored <code>Content</code> instance is a fragment or
159: * not.
160: *
161: * @param fragment <code>true</code> if it's a fragment; or
162: * <p><code>false</code> otherwise
163: * @see #isFragment()
164: * @since 1.0
165: */
166: public void setFragment(boolean fragment) {
167: mFragment = fragment;
168: }
169:
170: /**
171: * Retrieves wether the stored <code>Content</code> instance is a fragment
172: * or not.
173: *
174: * @return <code>true</code> if it's a fragment; or
175: * <p><code>false</code> otherwise
176: * @see #setFragment(boolean)
177: * @since 1.0
178: */
179: public boolean isFragment() {
180: return mFragment;
181: }
182:
183: /**
184: * Sets the name of the stored <code>Content</code> instance.
185: *
186: * @param name the name
187: * @see #getName()
188: * @see #hasName()
189: * @since 1.0
190: */
191: public void setName(String name) {
192: mName = name;
193: }
194:
195: /**
196: * Retrieves the name of the stored <code>Content</code> instance.
197: *
198: * @return <code>null</code> if the stored <code>Content</code> instance
199: * has no name; or
200: * <p>the name of the content
201: * @see #setName(String)
202: * @see #hasName()
203: * @since 1.0
204: */
205: public String getName() {
206: return mName;
207: }
208:
209: /**
210: * Indicates whether the stored <code>Content</code> instance has a name.
211: *
212: * @return <code>true</code> if it has a name; or
213: * <p><code>false</code> otherwise
214: * @see #setName(String)
215: * @see #getName()
216: * @since 1.0
217: */
218: public boolean hasName() {
219: return mName != null;
220: }
221:
222: /**
223: * Sets the moment when the <code>Content</code> instance was stored.
224: *
225: * @param created the moment of creation
226: * @see #getCreated()
227: * @since 1.0
228: */
229: public void setCreated(Timestamp created) {
230: mCreated = created;
231: }
232:
233: /**
234: * Retrieves the moment when the <code>Content</code> instance was stored.
235: *
236: * @return the moment of creation
237: * @see #setCreated(Timestamp)
238: * @since 1.0
239: */
240: public Timestamp getCreated() {
241: return mCreated;
242: }
243:
244: /**
245: * Sets the attributes map of the stored <code>Content</code> instance.
246: *
247: * @param attributes the attributes map with <code>String</code> keys and
248: * value.
249: * @see #getAttributes()
250: * @since 1.0
251: */
252: public void setAttributes(Map<String, String> attributes) {
253: mAttributes = attributes;
254: }
255:
256: /**
257: * Retrieves the attributes map of the stored <code>Content</code>
258: * instance.
259: *
260: * @return the attributes map
261: * @see #setAttributes(Map)
262: * @since 1.0
263: */
264: public Map<String, String> getAttributes() {
265: return mAttributes;
266: }
267:
268: /**
269: * Indicates whether named content attributes are present.
270: *
271: * @return <code>true</code> if named content attributes are present; or
272: * <p><code>false</code> otherwise
273: * @see #setAttributes(Map)
274: * @see #getAttributes()
275: * @since 1.0
276: */
277: public boolean hasAttributes() {
278: return mAttributes != null && mAttributes.size() > 0;
279: }
280:
281: /**
282: * Indicates whether a specific named content attribute is present.
283: *
284: * @param name the name of the attribute
285: * @return <code>true</code> if the name content attribute is present; or
286: * <p><code>false</code> otherwise
287: * @see #getAttribute(String)
288: * @since 1.0
289: */
290: public boolean hasAttribute(String name) {
291: if (null == mAttributes) {
292: return false;
293: }
294:
295: return mAttributes.containsKey(name);
296: }
297:
298: /**
299: * Retrieves the value of a named content attribute.
300: *
301: * @param name the name of the attribute
302: * @return the value of the named content attribute; or
303: * <p><code>null</code> if no such attribute could be found
304: * @see #hasAttribute(String)
305: * @since 1.0
306: */
307: public String getAttribute(String name) {
308: if (null == mAttributes) {
309: return null;
310: }
311:
312: return mAttributes.get(name);
313: }
314:
315: /**
316: * Sets the size of the stored <code>Content</code> instance.
317: *
318: * @param size the size of the cotent data
319: * @see #getSize()
320: * @since 1.0
321: */
322: public void setSize(int size) {
323: mSize = size;
324: }
325:
326: /**
327: * Retrieves the size of the stored <code>Content</code> instance.
328: *
329: * @return the <code>Content</code>'s size
330: * @see #setSize(int)
331: * @see #getFormattedSize()
332: * @since 1.0
333: */
334: public int getSize() {
335: return mSize;
336: }
337:
338: /**
339: * Retrieves the size of the stored <code>Content</code> instance as a
340: * formatted string.
341: *
342: * @return the <code>Content</code>'s formatted size
343: * @see #getSize()
344: * @since 1.0
345: */
346: public String getFormattedSize() {
347: NumberFormat format = NumberFormat
348: .getNumberInstance(Localization.getLocale());
349: double size = getSize();
350: double size_kb = size / 1024;
351: if (size_kb >= 1024) {
352: double size_mb = size_kb / 1024;
353: if (size_mb >= 1024) {
354: double size_gb = size_mb / 1024;
355: format.setMaximumFractionDigits(2);
356: return format.format(size_gb) + "GB";
357: } else {
358: format.setMaximumFractionDigits(2);
359: return format.format(size_mb) + "MB";
360: }
361: } else {
362: if (size_kb >= 100) {
363: format.setMaximumFractionDigits(0);
364: } else {
365: format.setMaximumFractionDigits(2);
366: }
367: return format.format(size_kb) + "KB";
368: }
369: }
370:
371: /**
372: * Sets the content data properties of the stored <code>Content</code>
373: * instance.
374: *
375: * @param properties the content data properties
376: * @see #hasProperties()
377: * @see #getProperties()
378: * @since 1.0
379: */
380: public void setProperties(Map<String, String> properties) {
381: mProperties = properties;
382: }
383:
384: /**
385: * Indicates whether content data properties are present for the stored
386: * <code>Content</code> instance.
387: *
388: * @return <code>true</code> if properties are present; or
389: * <p><code>false</code> otherwise
390: * @see #setProperties(Map)
391: * @see #getProperties()
392: * @since 1.0
393: */
394: public boolean hasProperties() {
395: return mProperties != null && mProperties.size() > 0;
396: }
397:
398: /**
399: * Indicates whether a specific named content property is present.
400: *
401: * @param name the name of the property
402: * @return <code>true</code> if the name content property is present; or
403: * <p><code>false</code> otherwise
404: * @see #getProperty(String)
405: * @since 1.0
406: */
407: public boolean hasProperty(String name) {
408: if (null == mProperties) {
409: return false;
410: }
411:
412: return mProperties.containsKey(name);
413: }
414:
415: /**
416: * Retrieves the value of a named content property.
417: *
418: * @param name the name of the property
419: * @return the value of the named content property; or
420: * <p><code>null</code> if no such property could be found
421: * @see #hasProperty(String)
422: * @since 1.0
423: */
424: public String getProperty(String name) {
425: if (null == mProperties) {
426: return null;
427: }
428:
429: return mProperties.get(name);
430: }
431:
432: /**
433: * Retrieves the content data properties of the stored
434: * <code>Content</code> instance.
435: *
436: * @return the content data properties; or
437: * <p><code>null</code> if no content data properties are present
438: * @see #setProperties(Map)
439: * @see #hasProperties()
440: * @since 1.0
441: */
442: public Map<String, String> getProperties() {
443: return mProperties;
444: }
445: }
|