KickJava   Java API By Example, From Geeks To Geeks.

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


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.Collection JavaDoc;
20 import java.util.HashMap JavaDoc;
21 import java.util.Iterator JavaDoc;
22 import java.util.List JavaDoc;
23 import java.util.Map JavaDoc;
24
25
26 /**
27  * <p>Content Field (CF) for localizable. Content Field actually represents some field
28  * on the page (or any other Localizable) which have values for every (or some)
29  * {@link ContentLocale}s
30  * (in other words, Content Field represents some localizable content). Those
31  * values (which are represented with {@link ContentFieldValue}s) can be
32  * edited in edit mode; when not in edit mode, they are just rendered to page.
33  * </p>
34  * <p>Each Content Field belongs to {@link Localizable}, which is called
35  * <b>owner</b>. Content Fields are identified within their owner by
36  * <b>identifier</b>. Identifier may be indexed and have form of <em>foo[1.6]</em>.
37  * Any <code>double</code> value may be in place of <em>1.6</em>. Set of Content Fields
38  * with same base identifier (that is part of identifier before opening square
39  * bracket) may be iterated over with &lt;contentIterator&gt; tag.</p>
40  * <p>Content Field may be one of three types: <b>LINE_TYPE</b>,
41  * <b>MULTILINE_TYPE</b>, <b>HTML_TYPE</b>. First of them defines field which
42  * content is single-line text with no formatting; second defines field which
43  * content is multiline text with no formatting; and the last stands for a
44  * field which stores formatting along with text. Here 'with no formatting'
45  * technically means, that any HTML-sensitive characters will be escaped to
46  * be displayed as-is, not as HTML-tags, for example.</p>
47  * <p>Content Field may be <b>internal</b>. This means that the field is
48  * actually a property of some object and cannot be deleted and so on. For
49  * example, every {@link Page} has a <b>title</b>, which is internal Content Field.</p>
50  * <p><a HREF="ContentField.java.htm"><i>View Source</i></a>
51  * </p>
52  *
53  * @author Andrey Grebnev <a HREF="mailto:andrey.grebnev@blandware.com">&lt;andrey.grebnev@blandware.com&gt;</a>
54  * @author Sergey Zubtcovskii <a HREF="mailto:sergey.zubtcovskii@blandware.com">&lt;sergey.zubtcovskii@blandware.com&gt;</a>
55  * @version $Revision: 1.38 $ $Date: 2006/03/26 13:51:46 $
56  * @see ContentLocale
57  * @see ContentFieldValue
58  * @struts.form include-all="false" extends="BaseForm"
59  * @hibernate.class table="`al_core_field`" lazy="false"
60  */

61 public class ContentField extends BaseObject {
62
63     /**
64      * Constant for type field. It specifies to use input text to edit this feild.
65      */

66     public static final byte LINE_TYPE = 0;
67
68     /**
69      * Constant for type field. It specifies to use textarea to edit this feild.
70      */

71     public static final byte MULTILINE_TYPE = 1;
72
73     /**
74      * Constant for type field. It specifies to use HTML editor to edit this feild.
75      */

76     public static final byte HTML_TYPE = 2;
77
78     /**
79      * Constant for identifier field. It will be used for <code>TITLE</code> html tag.
80      */

81     public static final String JavaDoc TITLE_IDENTIFIER = "title";
82
83     /**
84      * Constant for identifier field. It will be used for <code>META</code> html tag with <code>keywrods</code> name.
85      */

86     public static final String JavaDoc KEYWORDS_IDENTIFIER = "keywords";
87
88     /**
89      * Constant for identifier field. It will be used for <code>META</code> html tag with <code>description</code> name.
90      */

91     public static final String JavaDoc DESCRIPTION_IDENTIFIER = "description";
92
93
94     //~ Instance fields ========================================================
95

96     /**
97      * The ID of this CF
98      */

99     protected Long JavaDoc id;
100     /**
101      * The identifier of this CF. It is unique per field owner.
102      */

103     protected String JavaDoc identifier;
104     /**
105      * The identifier of this CF without index.
106      */

107     protected String JavaDoc identifierWithoutIndex;
108     /**
109      * The index
110      */

111     protected Double JavaDoc index;
112     /**
113      * Type of field: is it line, multiline or html
114      */

115     protected byte type;
116     /**
117      * List of content field values that correspond to this field
118      */

119     protected List JavaDoc contentFieldValues = new ArrayList JavaDoc();
120     /**
121      * Owner of this field
122      */

123     protected Localizable owner;
124     /**
125      * If this field internal
126      */

127     protected Boolean JavaDoc internal;
128     /**
129      * Version of this field (used in optimistic locking)
130      */

131     protected Long JavaDoc version;
132
133     //~ Methods ================================================================
134

135     /**
136      * Returns the id.
137      *
138      * @return id
139      * @struts.form-field
140      * @hibernate.id column="`id`"
141      * generator-class="increment" unsaved-value="null"
142      */

143     public Long JavaDoc getId() {
144         return id;
145     }
146
147     /**
148      * Sets the id of CF
149      *
150      * @param id new id
151      */

152     public void setId(Long JavaDoc id) {
153         this.id = id;
154     }
155
156     /**
157      * Returns version
158      *
159      * @return version
160      * @struts.form-field
161      * @hibernate.version column="`version`" type="long" unsaved-value="null"
162      */

163     public Long JavaDoc getVersion() {
164         return version;
165     }
166
167     /**
168      * Sets version of CF
169      *
170      * @param version new version
171      */

172     public void setVersion(Long JavaDoc version) {
173         this.version = version;
174     }
175
176     /**
177      * Returns the identifier of the field
178      *
179      * @return the identifier
180      * @struts.form-field
181      * @struts.validator type="required"
182      * @struts.validator-args arg0resource="core.contentField.form.identifier"
183      * @struts.validator type="mask" msgkey="core.commons.errors.contentFieldIdentifier"
184      * @struts.validator-var name="mask" value="^([0-9a-zA-Z\_]+(\.|\-)?)*[0-9a-zA-Z\_]+(\[\-?([0-9]+\.)?[0-9]+\])?$"
185      * @hibernate.property
186      * @hibernate.column name="`identifier`" not-null="true" unique="false" length="252"
187      */

188     public String JavaDoc getIdentifier() {
189         return identifier;
190     }
191
192     /**
193      * Sets the identifier of the field
194      *
195      * @param identifier new identifier
196      */

197     public void setIdentifier(String JavaDoc identifier) {
198         this.identifier = identifier;
199         int k = identifier.indexOf('[');
200         if ( k != -1 ) {
201             index = Double.valueOf(identifier.substring(k + 1, identifier.indexOf(']', k)));
202             identifierWithoutIndex = identifier.substring(0, k);
203         } else {
204             index = null;
205             identifierWithoutIndex = identifier;
206         }
207     }
208
209     /**
210      * Returns identifier without index
211      *
212      * @return pure identifier with no index
213      */

214     public String JavaDoc getIdentifierWithoutIndex() {
215         return identifierWithoutIndex;
216     }
217
218     /**
219      * Gets the index of the field. If field is not indexed, will return
220      * <code>null</code>.
221      *
222      * @struts.form-field
223      * @struts.validator type="float"
224      * @struts.validator-args arg0resource="core.contentField.form.index"
225      * @return the index
226      */

227     public Double JavaDoc getIndex() {
228         return index;
229     }
230
231     /**
232      * Returns the list of contentFieldValues dependent on locale.
233      *
234      * @return list of CFVs
235      * @hibernate.bag name="contentFieldValues" inverse="true" lazy="true" cascade="delete" outer-join="false"
236      * @hibernate.collection-key column="`field_id`"
237      * @hibernate.collection-one-to-many class="com.blandware.atleap.model.core.ContentFieldValue"
238      */

239     public List JavaDoc getContentFieldValues() {
240         return contentFieldValues;
241     }
242
243     /**
244      * Sets the list of contentFieldValues dependent on locale.
245      *
246      * @param contentFieldValues list of CFVs
247      */

248     public void setContentFieldValues(List JavaDoc contentFieldValues) {
249         this.contentFieldValues = contentFieldValues;
250     }
251
252     /**
253      * Adds a content field value to the list of CFVs that are owned by this field
254      *
255      * @param contentFieldValue the CFV to be added
256      */

257     public void addContentFieldValue(ContentFieldValue contentFieldValue) {
258         contentFieldValue.setContentField(this);
259         contentFieldValues.add(contentFieldValue);
260     }
261
262     /**
263      * Updates ContentFieldValue
264      *
265      * @param contentFieldValue the value to be updated
266      * @return old ContentFieldValue object
267      */

268     public ContentFieldValue updateContentFieldValue(ContentFieldValue contentFieldValue) {
269         ContentFieldValue oldContentFieldValue = null;
270         if ( !contentFieldValue.getContentField().equals(this) ) {
271             int index = contentFieldValue.getContentField().contentFieldValues.indexOf(contentFieldValue);
272             if (index != -1) {
273                 oldContentFieldValue = (ContentFieldValue)contentFieldValue.getContentField().contentFieldValues.remove(index);
274             }
275             contentFieldValues.add(contentFieldValue);
276             contentFieldValue.setContentField(this);
277         }
278         return oldContentFieldValue;
279     }
280
281     /**
282      * Removes a content field value from the list of CFVs that are owned by this field
283      *
284      * @param contentFieldValue the CFV to be removed
285      */

286     public void removeContentFieldValue(ContentFieldValue contentFieldValue) {
287         contentFieldValues.remove(contentFieldValue);
288     }
289
290     /**
291      * Returns map with keys - contentFieldValue locale identifier and values - contentFieldValue
292      *
293      * @return a mapping from locale identifiers to CFVs
294      */

295     public Map JavaDoc getContentFieldValuesMap() {
296         Collection JavaDoc collection = getContentFieldValues();
297         Map JavaDoc map = new HashMap JavaDoc();
298         for ( Iterator JavaDoc iterator = collection.iterator(); iterator.hasNext(); ) {
299             ContentFieldValue contentFieldValue = (ContentFieldValue) iterator.next();
300             map.put(contentFieldValue.getContentLocale().getIdentifier(), contentFieldValue);
301         }
302         return map;
303     }
304
305     /**
306      * Returns the localizable owner
307      *
308      * @return a localizable
309      * @struts.form-field
310      * @hibernate.many-to-one column="`localizable_id`" not-null="true" outer-join="false" lazy="false"
311      */

312     public Localizable getOwner() {
313         return owner;
314     }
315
316     /**
317      * Sets the localizable owner
318      *
319      * @param owner the new owner
320      */

321     public void setOwner(Localizable owner) {
322         this.owner = owner;
323     }
324
325     /**
326      * Returns the type of the field
327      *
328      * @return whether this field is single-line, multy-line or html
329      * @struts.form-field
330      * @struts.validator type="required"
331      * @hibernate.property column="`type`" not-null="true" unique="false"
332      */

333     public byte getType() {
334         return type;
335     }
336
337     /**
338      * Sets the type of the field
339      *
340      * @param type type to set
341      */

342     public void setType(byte type) {
343         this.type = type;
344     }
345
346     /**
347      * Returns <code>Boolean.TRUE</code> if this content field is internal.
348      * Internal field is created and updated in a different way than other fields.
349      *
350      * @return whether this field is internal
351      * @hibernate.property column="`internal`" not-null="true" type="true_false"
352      */

353     public Boolean JavaDoc getInternal() {
354         return internal;
355     }
356
357     /**
358      * Sets whether this field is internal
359      *
360      * @param internal if <code>Boolean.TRUE</code>, that the field will be
361      * internal
362      */

363     public void setInternal(Boolean JavaDoc internal) {
364         this.internal = internal;
365     }
366
367     public boolean equals(Object JavaDoc o) {
368         if ( this == o ) {
369             return true;
370         }
371         if ( !(o instanceof ContentField) ) {
372             return false;
373         }
374
375         final ContentField contentField = (ContentField) o;
376
377         if ( identifier != null ? !identifier.equals(contentField.identifier) : contentField.identifier != null ) {
378             return false;
379         }
380         if ( owner != null ? !owner.equals(contentField.owner) : contentField.owner != null ) {
381             return false;
382         }
383
384         return true;
385     }
386
387     public int hashCode() {
388         int result;
389         result = (identifier != null ? identifier.hashCode() : 0);
390         result = 29 * result + (owner != null ? owner.hashCode() : 0);
391         return result;
392     }
393
394 }
395
Popular Tags