KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > blandware > atleap > model > core > ContentResource


1 /*
2  * Copyright 2004 Blandware (http://www.blandware.com)
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16 package com.blandware.atleap.model.core;
17
18 import java.util.ArrayList JavaDoc;
19 import java.util.Iterator JavaDoc;
20 import java.util.List JavaDoc;
21 import java.util.Date JavaDoc;
22
23
24 /**
25  * <p>Represents some resource with binary content. This can be document, image
26  * or file.</p>
27  * <p>
28  * Saved resources may be used in various places, such as pages or menu items.
29  * </p>
30  * <p>Every Content Resource has a list of {@link Role}s that are allowed to
31  * access it. If that list is empty, everyone is allowed to access such Content
32  * Resource, otherwise only users that have at least one role from that list can
33  * access the Content Resource.</p>
34  * <p>
35  * Binary data that forms the resource is stored in attached {@link ResourceData}
36  * object.
37  * </p>
38  * <p>
39  * For each content resource there's an URI by which it's accessible. For each
40  * content resource there are two operations: 'view' and 'download'. Every
41  * resource may be downloaded, but only some of them may be viewed.
42  * </p>
43  * <p>
44  * When content resource is created, its type will be determined by uploaded
45  * file extension. Currently, there are three resource types: images, documents
46  * and files (those that are neither image nor document). For known extensions,
47  * also mimi type is detected. For all other files mime type is <code>null</code>.
48  * </p>
49  * <p>
50  * As pages, content resources have usage counter that stores number of links
51  * on particular content resource.
52  * </p>
53  * <p><a HREF="ContentResource.java.htm"><i>View Source</i></a>
54  * </p>
55  * <p/>
56  *
57  * @author Andrey Grebnev <a HREF="mailto:andrey.grebnev@blandware.com">&lt;andrey.grebnev@blandware.com&gt;</a>
58  * @author Sergey Zubtcovskii <a HREF="mailto:sergey.zubtcovskii@blandware.com">&lt;sergey.zubtcovskii@blandware.com&gt;</a>
59  * @version $Revision: 1.46 $ $Date: 2005/12/18 16:43:42 $
60  * @see ContentDocument
61  * @see ContentImage
62  * @see ResourceData
63  * @hibernate.class table="`al_core_resource`" lazy="false"
64  * @struts.form include-all="false" extends="BaseForm"
65  * @hibernate.cache usage="read-write"
66  */

67 public class ContentResource extends BaseObject {
68
69     //~ Instance variables
70

71     /**
72      * The ID of this content resource
73      */

74     protected Long JavaDoc id;
75     /**
76      * URI under which this content resource will be accessible
77      */

78     protected String JavaDoc uri;
79     /**
80      * Description of this resource
81      */

82     protected String JavaDoc description;
83     /**
84      * Resource data corresponding to this resource
85      */

86     protected ResourceData resourceData;
87     /**
88      * Size of resource data in bytes
89      */

90     protected Long JavaDoc size;
91     /**
92      * Mime-type of this resource
93      */

94     protected String JavaDoc mimeType;
95     /**
96      * Type of this resource (image, document, file)
97      */

98     protected String JavaDoc type;
99     /**
100      * Number of links to this resource
101      */

102     protected Integer JavaDoc usageCounter;
103     /**
104      * List od roles which are allowed to access this resource
105      */

106     protected List JavaDoc roles = new ArrayList JavaDoc();
107     /**
108      * Version of this resource (used in optimistic locking)
109      */

110     protected Long JavaDoc version;
111     /**
112      * Date and time of last update
113      */

114     protected Date JavaDoc lastUpdatedDatetime = null;
115
116     /**
117      * List of CFVs linked to this resource
118      */

119     protected List JavaDoc linkedContentFieldValues = new ArrayList JavaDoc();
120     /**
121      * List of menu items linked to this resource
122      */

123     protected List JavaDoc linkedMenuItems = new ArrayList JavaDoc();
124
125     //~ Methods
126

127     /**
128      * Returns the id of this content resource
129      *
130      * @return id
131      * @hibernate.id column="`id`"
132      * generator-class="increment" unsaved-value="null"
133      * @struts.form-field
134      */

135     public Long JavaDoc getId() {
136         return id;
137     }
138
139     /**
140      * Sets id of this content resource
141      *
142      * @param id the id
143      */

144     public void setId(Long JavaDoc id) {
145         this.id = id;
146     }
147
148     /**
149      * Returns the URI of the resource. Usually it is the name with extension.
150      *
151      * @return URI
152      * @hibernate.property
153      * @hibernate.column name="`uri`" not-null="true" unique="true" length="252"
154      * @struts.form-field
155      * @struts.validator type="required"
156      * @struts.validator type="mask" msgkey="core.contentResource.errors.uri"
157      * @struts.validator-args arg0resource="core.contentResource.form.uri"
158      * @struts.validator-var name="mask" value="^(\/[a-zA-Z0-9\-_\040]+)+\.[a-zA-Z0-9]+$"
159      */

160     public String JavaDoc getUri() {
161         return uri;
162     }
163
164     /**
165      * Sets the URI of the resource
166      *
167      * @param uri the URI
168      */

169     public void setUri(String JavaDoc uri) {
170         this.uri = uri;
171     }
172
173     /**
174      * Returns the description of this content resource
175      *
176      * @return description
177      * @hibernate.property
178      * @hibernate.column name="`description`" not-null="false" unique="false" length="252"
179      * @struts.form-field
180      */

181     public String JavaDoc getDescription() {
182         return description;
183     }
184
185     /**
186      * Sets the description of this content resource
187      *
188      * @param description the description
189      */

190     public void setDescription(String JavaDoc description) {
191         this.description = description;
192     }
193
194     /**
195      * Gets resource data that holds binary data owned by this content resource
196      *
197      * @return the resource data
198      * @hibernate.one-to-one class="com.blandware.atleap.model.core.ResourceData" constrained="true" cascade="delete" outer-join="false" lazy="false"
199      */

200     public ResourceData getResourceData() {
201         return resourceData;
202     }
203
204     /**
205      * Sets resource data that holds binary data owned by this content resource
206      *
207      * @param resourceData the resource data
208      */

209     public void setResourceData(ResourceData resourceData) {
210         this.resourceData = resourceData;
211     }
212
213     /**
214      * Gets size of resource (that's the same as size of resource data in bytes)
215      *
216      * @return size of resource
217      * @hibernate.property
218      * @hibernate.column name="`data_size`" not-null="true" unique="false"
219      * @struts.form-field
220      * @struts.validator type="long"
221      */

222     public Long JavaDoc getSize() {
223         return size;
224     }
225
226     /**
227      * Sets size of resource
228      *
229      * @param size size of resource
230      */

231     public void setSize(Long JavaDoc size) {
232         this.size = size;
233     }
234
235     /**
236      * Returns mime-type of resource content (e.g. 'image/jpeg');
237      *
238      * @return mime-type
239      * @hibernate.property
240      * @hibernate.column name="`mimetype`" not-null="false" unique="false" length="252"
241      * @struts.form-field
242      */

243     public String JavaDoc getMimeType() {
244         return mimeType;
245     }
246
247     /**
248      * Sets mime-type of resource content
249      *
250      * @param mimeType the mime-type to set
251      */

252     public void setMimeType(String JavaDoc mimeType) {
253         this.mimeType = mimeType;
254     }
255
256     /**
257      * Returns type of resource content (e.g. 'image');
258      *
259      * @return type
260      * @hibernate.property
261      * @hibernate.column name="`type`" not-null="true" unique="false" length="252"
262      */

263     public String JavaDoc getType() {
264         return type;
265     }
266
267     /**
268      * Sets type of resource content
269      *
270      * @param type the type to set
271      */

272     public void setType(String JavaDoc type) {
273         this.type = type;
274     }
275
276     /**
277      * Gets usage counter for this content resource (that is a number of links
278      * to this resource)
279      *
280      * @return usage counter
281      * @hibernate.property column="`usage_counter`" not-null="false"
282      */

283     public Integer JavaDoc getUsageCounter() {
284         return usageCounter;
285     }
286
287     /**
288      * Sets usage counter for this content resource (that is a number of links
289      * to this resource)
290      *
291      * @param usageCounter new usage counter value
292      */

293     public void setUsageCounter(Integer JavaDoc usageCounter) {
294         this.usageCounter = usageCounter;
295     }
296
297     /**
298      * Gets list of roles that are assigned to this content resource
299      *
300      * @return list of roles
301      * @hibernate.bag table="`al_core_resource_role`" cascade="none" lazy="true" inverse="false" outer-join="false"
302      * @hibernate.collection-key column="`resource_id`"
303      * @hibernate.collection-many-to-many class="com.blandware.atleap.model.core.Role"
304      * column="`rolename`" outer-join="false"
305      * @hibernate.cache usage="read-write"
306      */

307     public List JavaDoc getRoles() {
308         return roles;
309     }
310
311     /**
312      * Sets list of roles that are assigned to this content resource
313      *
314      * @param roles list of roles
315      */

316     public void setRoles(List JavaDoc roles) {
317         this.roles = roles;
318     }
319
320     /**
321      * Gets all resource roles, represented as string
322      *
323      * @return roles separated with commas
324      */

325     public String JavaDoc getRolesAsString() {
326         StringBuffer JavaDoc roles = new StringBuffer JavaDoc();
327         if ( this.roles != null && this.roles.size() > 0 ) {
328             for ( Iterator JavaDoc i = this.roles.iterator(); i.hasNext(); ) {
329                 Role role = (Role) i.next();
330                 roles.append(role.getTitle());
331                 if ( i.hasNext() ) {
332                     roles.append(", ");
333                 }
334             }
335         }
336         return roles.toString();
337     }
338
339     /**
340      * Adds a connection between this content resource and a given role
341      *
342      * @param role the role to be connected with
343      */

344     public void addRole(Role role) {
345         if ( !role.getResources().contains(this) ) {
346             role.getResources().add(this);
347         }
348         if ( !getRoles().contains(role) ) {
349             getRoles().add(role);
350         }
351     }
352
353     /**
354      * Removes a connection between this content resource and a given role
355      *
356      * @param role the role
357      */

358     public void removeRole(Role role) {
359         role.getResources().remove(this);
360         getRoles().remove(role);
361     }
362
363
364     /**
365      * Gets list of CFVs this resource is linked with
366      *
367      * @return list of CFVs
368      * @hibernate.bag table="`al_core_field_value_resource`" cascade="none" lazy="true" inverse="false" outer-join="false"
369      * @hibernate.collection-key column="`resource_id`"
370      * @hibernate.collection-many-to-many class="com.blandware.atleap.model.core.ContentFieldValue" column="`field_value_id`" outer-join="false"
371      */

372     public List JavaDoc getLinkedContentFieldValues() {
373         return linkedContentFieldValues;
374     }
375
376     /**
377      * Sets list of CFVs this resource is linked with
378      *
379      * @param linkedContentFieldValues list of CFVs
380      */

381     public void setLinkedContentFieldValues(List JavaDoc linkedContentFieldValues) {
382         this.linkedContentFieldValues = linkedContentFieldValues;
383     }
384
385     /**
386      * Gets list of menu items this context resource is linked with
387      *
388      * @return list of menu items
389      * @hibernate.bag table="`al_core_menu_item_resource`" cascade="none" lazy="true" inverse="false" outer-join="false"
390      * @hibernate.collection-key column="`resource_id`"
391      * @hibernate.collection-many-to-many class="com.blandware.atleap.model.core.MenuItem" column="`menu_item_id`" outer-join="false"
392      */

393     public List JavaDoc getLinkedMenuItems() {
394         return linkedMenuItems;
395     }
396
397     /**
398      * Sets list of menu items this content resource is linked with
399      *
400      * @param linkedMenuItems list of menu items
401      */

402     public void setLinkedMenuItems(List JavaDoc linkedMenuItems) {
403         this.linkedMenuItems = linkedMenuItems;
404     }
405
406     /**
407      * Returns true if this resource is in use, i.e. there are links on it from content field values or menu items
408      * or its usage counter is greater than zero
409      *
410      * @return <code>true</code> if this resource is in use
411      */

412     public boolean isInUse() {
413         return !linkedContentFieldValues.isEmpty() || !linkedMenuItems.isEmpty() || usageCounter.intValue() > 0;
414     }
415
416     /**
417      * Returns whether there are some objects linked to this page by URI that
418      * cannot be modified to have correct links when this page URI is changed
419      *
420      * @return whether there are unmodifiable linked objects
421      */

422     public boolean doUnmodifiableLinkedObjectsExist() {
423         for (Iterator JavaDoc i = linkedMenuItems.iterator(); i.hasNext();) {
424             MenuItem menuItem = (MenuItem) i.next();
425             if (!menuItem.isDynamic()) {
426                 return true;
427             }
428         }
429         return false;
430     }
431
432     /**
433      * Returns version of this content resource
434      *
435      * @return version
436      * @struts.form-field
437      * @hibernate.version column="`version`" type="long" unsaved-value="null"
438      */

439     public Long JavaDoc getVersion() {
440         return version;
441     }
442
443     /**
444      * Sets version of this content resource
445      *
446      * @param version the new version
447      */

448     public void setVersion(Long JavaDoc version) {
449         this.version = version;
450     }
451
452     /**
453      * Gets date/time when this content resource was created/last updated
454      *
455      * @return last updated data/time
456      * @hibernate.property
457      * @hibernate.column name="`last_updated`" not-null="true" unique="false"
458      * @struts.form-field
459      * @struts.validator type="customdate"
460      */

461     public Date JavaDoc getLastUpdatedDatetime() {
462         return lastUpdatedDatetime;
463     }
464
465     /**
466      * Sets date/time when this content resource was created/last updated
467      *
468      * @param lastUpdatedDatetime last updated data/time
469      */

470     public void setLastUpdatedDatetime(Date JavaDoc lastUpdatedDatetime) {
471         this.lastUpdatedDatetime = lastUpdatedDatetime;
472     }
473
474
475     public boolean equals(Object JavaDoc o) {
476         if ( this == o ) {
477             return true;
478         }
479         if ( !(o instanceof ContentResource) ) {
480             return false;
481         }
482
483         final ContentResource contentResource = (ContentResource) o;
484
485         if ( uri != null ? !uri.equals(contentResource.uri) : contentResource.uri != null ) {
486             return false;
487         }
488
489         return true;
490     }
491
492     public int hashCode() {
493         return (uri != null ? uri.hashCode() : 0);
494     }
495
496 }
497
Popular Tags