KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jahia > data > fields > JahiaField


1 /*
2  * ____.
3  * __/\ ______| |__/\. _______
4  * __ .____| | \ | +----+ \
5  * _______| /--| | | - \ _ | : - \_________
6  * \\______: :---| : : | : | \________>
7  * |__\---\_____________:______: :____|____:_____\
8  * /_____|
9  *
10  * . . . i n j a h i a w e t r u s t . . .
11  *
12  *
13  *
14  * ----- BEGIN LICENSE BLOCK -----
15  * Version: JCSL 1.0
16  *
17  * The contents of this file are subject to the Jahia Community Source License
18  * 1.0 or later (the "License"); you may not use this file except in
19  * compliance with the License. You may obtain a copy of the License at
20  * http://www.jahia.org/license
21  *
22  * Software distributed under the License is distributed on an "AS IS" basis,
23  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
24  * for the rights, obligations and limitations governing use of the contents
25  * of the file. The Original and Upgraded Code is the Jahia CMS and Portal
26  * Server. The developer of the Original and Upgraded Code is JAHIA Ltd. JAHIA
27  * Ltd. owns the copyrights in the portions it created. All Rights Reserved.
28  *
29  * The Shared Modifications are Jahia View Helper.
30  *
31  * The Developer of the Shared Modifications is Jahia Solution S�rl.
32  * Portions created by the Initial Developer are Copyright (C) 2002 by the
33  * Initial Developer. All Rights Reserved.
34  *
35  * Contributor(s):
36  * 31-OCT-2003, Xo3 SA : Eric Vassalli
37  * 17-JUL-2003, Xo3 SA : Yvan Gentil
38  * 19-AUG-2003, Jahia Solutions Sarl: Fulco Houkes
39  *
40  * ----- END LICENSE BLOCK -----
41  */

42
43
44 package org.jahia.data.fields;
45
46 import java.util.ArrayList JavaDoc;
47 import java.util.HashMap JavaDoc;
48 import java.util.Properties JavaDoc;
49 import java.io.Serializable JavaDoc;
50
51 import org.jahia.exceptions.JahiaException;
52 import org.jahia.params.ParamBean;
53 import org.jahia.registries.JahiaFieldDefinitionsRegistry;
54 import org.jahia.registries.ServicesRegistry;
55 import org.jahia.services.acl.ACLNotFoundException;
56 import org.jahia.services.acl.ACLResourceInterface;
57 import org.jahia.services.acl.JahiaBaseACL;
58 import org.jahia.services.fields.ContentField;
59 import org.jahia.services.usermanager.JahiaUser;
60 import org.jahia.services.version.EntryLoadRequest;
61 import org.jahia.utils.textdiff.HunkTextDiffVisitor;
62 import org.jahia.content.PropertiesInterface;
63 import org.jahia.utils.JahiaTools;
64
65 public abstract class JahiaField implements Cloneable JavaDoc, Serializable JavaDoc, ACLResourceInterface, PropertiesInterface {
66
67     public static final String JavaDoc MULTIPLE_VALUES_SEP = "$$$";
68
69     protected int ID;
70     protected int jahiaID;
71     protected int pageID;
72     protected int ctnid;
73     protected int fieldDefID;
74     protected int fieldType;
75     protected int connectType;
76     protected String JavaDoc fieldValue = "";
77     protected String JavaDoc fieldRawValue = "";
78     protected int rank;
79     protected int aclID;
80     protected int versionID;
81     protected int workflowState;
82     protected String JavaDoc languageCode;
83     protected Object JavaDoc objectItem; // can be used to pass any object
84
protected Properties JavaDoc properties;
85     private boolean propertiesLoaded = false;
86     protected boolean hasChanged = false;
87
88     protected JahiaField( Integer JavaDoc ID,
89                             Integer JavaDoc jahiaID,
90                             Integer JavaDoc pageID,
91                             Integer JavaDoc ctnid,
92                             Integer JavaDoc fieldDefID,
93                             Integer JavaDoc fieldType,
94                             Integer JavaDoc connectType,
95                             String JavaDoc fieldValue,
96                             Integer JavaDoc rank,
97                             Integer JavaDoc aclID,
98                             Integer JavaDoc versionID,
99                             Integer JavaDoc workflowState,
100                             String JavaDoc languageCode)
101     {
102         setID( ID.intValue() );
103         this.jahiaID = jahiaID.intValue();
104         this.pageID = pageID.intValue();
105         this.fieldDefID = fieldDefID.intValue();
106         this.ctnid = ctnid.intValue();
107         this.fieldType = fieldType.intValue();
108         this.connectType = connectType.intValue();
109         this.fieldValue = fieldValue;
110         this.rank = rank.intValue();
111         this.aclID = aclID.intValue();
112         this.versionID = versionID.intValue();
113         this.workflowState = workflowState.intValue();
114         this.languageCode = languageCode;
115         this.properties = null;
116     }
117
118     /**
119      * accessor methods
120      * EV 31.10.2000
121      *
122      */

123     public int getID() { return ID; }
124     public int getJahiaID() { return jahiaID; }
125     public int getSiteID() { return jahiaID; } // FIXME_MULTISITE Hollis humm jahiaID or siteID ?
126
public int getPageID() { return pageID; }
127     public int getFieldDefID() { return fieldDefID; }
128     public int getctnid() { return ctnid; }
129     public int getType() { return fieldType; }
130     public int getConnectType() { return connectType; }
131     public int getRank() { return rank; }
132     public final int getAclID() { return aclID; }
133     public int getVersionID() { return versionID; }
134     public int getWorkflowState() { return workflowState; }
135     public boolean hasChanged() { return hasChanged; }
136
137     public String JavaDoc getLanguageCode() {
138         return languageCode;
139     }
140
141     public final JahiaBaseACL getACL() {
142         JahiaBaseACL acl = null;
143         try {
144             acl = new JahiaBaseACL(getAclID());
145         } catch ( Throwable JavaDoc t ) {
146             t.printStackTrace();
147         }
148         return acl;
149     }
150
151     public Object JavaDoc getObject() { return objectItem; }
152
153     public void setID( int ID ) { this.ID = ID; }
154     public void setAclID( int aclID ) { this.aclID = aclID; }
155     public void setType( int fieldType ) { this.fieldType = fieldType; }
156
157     /**
158      * Sets the value. Usually we make a call to FormDataManager.decode before
159      * setting this value.
160      * @param fieldValue
161      */

162     public void setValue( String JavaDoc fieldValue ) { registerChange(); this.fieldValue = fieldValue; }
163     public void setctnid( int ctnid ) { registerChange(); this.ctnid = ctnid; }
164     public void setFieldDefID(int fieldDefID) { registerChange(); this.fieldDefID = fieldDefID; }
165
166     public void setConnectType( int connectType ) { registerChange(); this.connectType = connectType; }
167     public void setObject( Object JavaDoc objectItem ) { registerChange(); this.objectItem = objectItem; }
168     // end accessor methods
169

170     public String JavaDoc getValue() {
171         return fieldValue;
172     }
173
174     /**
175      * Returns a string[] tokens of values separated by separator JahiaField.MULTIPLE_VALUES_SEP
176      *
177      * @return
178      */

179     public String JavaDoc[] getValues() {
180         return JahiaTools.getTokens(getValue(),JahiaField.MULTIPLE_VALUES_SEP);
181     }
182
183     /**
184      * Sets the internal raw value. This means that the value we set has never
185      * been processed.
186      * @return
187      */

188     public String JavaDoc getRawValue() {
189         return fieldRawValue;
190     }
191
192     public void setRawValue(String JavaDoc fieldRawValue) {
193         registerChange();
194         this.fieldRawValue = fieldRawValue;
195     }
196
197
198     public Properties JavaDoc getProperties () {
199         if (propertiesLoaded) {
200             return properties;
201         } else {
202             return null;
203         }
204     }
205
206     public void setProperties (Properties JavaDoc properties) {
207         this.properties = properties;
208         propertiesLoaded = true;
209     } // end Properties methods
210

211     // abstract methods
212
// YG 17.07.2001
213
// Must be implemented into inherited class
214
// because they depends of field types
215

216     /**
217     * load the information specific to a type
218     * set the Object parameter with a JahiaPage
219     * or a file, for example.
220     *
221     */

222     public abstract void load(int loadFlag, ParamBean jParams)
223     throws JahiaException;
224
225     /**
226     * save the information specific to a type
227     * save the Object in relation with the field
228     * a JahiaPage or a file, for example.
229     *
230     */

231     public abstract boolean save(ParamBean jParams)
232     throws JahiaException;
233
234     /**
235     * delete the information specific to a type
236     * delete the Object in relation with the field
237     * a JahiaPage or a file, for example.
238     *
239     */

240     public abstract void delete(ParamBean jParams)
241     throws JahiaException;
242
243     /**
244     * return the name of the engine
245     * which correspond to the field type.
246     *
247     */

248     public abstract String JavaDoc getEngineName();
249
250     /**
251     * return the information
252     * to display for the ranking.
253     * the filename or the page name for example.
254     *
255     */

256     public abstract String JavaDoc getFieldContent4Ranking()
257     throws JahiaException;
258
259     /**
260     * return the "off" icon name
261     *
262     */

263     public abstract String JavaDoc getIconNameOff();
264
265     /**
266     * return the "on" icon name
267     *
268     */

269     public abstract String JavaDoc getIconNameOn();
270
271
272     /**
273     * clone the field and copy the relating file
274     * if necessary.
275     *
276     */

277     public abstract JahiaField cloneField(int newctnid, int newPageID, int clonedAclID, boolean childrenCloned)
278     throws JahiaException;
279
280
281     /**
282      * Clone
283      */

284     public abstract Object JavaDoc clone();
285     //{
286
//return new JahiaField (ID, jahiaID, pageID, ctnid, fieldDefID,
287
// fieldType, connectType, fieldValue, rank, aclID);
288
//}
289

290     // end abstract methods
291

292
293     /***
294         * getDefinition
295         *
296         */

297     public JahiaFieldDefinition getDefinition()
298     throws JahiaException
299     {
300         JahiaFieldDefinition theDef = JahiaFieldDefinitionsRegistry.getInstance().getDefinition( fieldDefID );
301         if (theDef != null) {
302             return theDef;
303         } else {
304             String JavaDoc msg = "JahiaField definition " + fieldDefID + " not found in definitions registry !";
305             throw new JahiaException( "Synchronisation error in database",
306                                         msg, JahiaException.DATABASE_ERROR, JahiaException.CRITICAL_SEVERITY );
307         }
308     } // end getDefinition
309

310
311
312     //-------------------------------------------------------------------------
313
/**
314      * Check if the user has read access for the specified field. Read access means
315      * displaying field data.
316      *
317      * @param user Reference to the user.
318      *
319      * @return Return true if the user has read access for the specified field,
320      * or false in any other case.
321      */

322     public final boolean checkReadAccess (JahiaUser user)
323     {
324         return checkAccess (user, JahiaBaseACL.READ_RIGHTS);
325     }
326
327     //-------------------------------------------------------------------------
328
/**
329      * Check if the user has Write access for the specified field. Write access means
330      * updating field data.
331      *
332      * @param user Reference to the user.
333      *
334      * @return Return true if the user has write access for the specified field,
335      * or false in any other case.
336      */

337     public final boolean checkWriteAccess (JahiaUser user)
338     {
339         return checkAccess (user, JahiaBaseACL.WRITE_RIGHTS);
340     }
341
342
343     //-------------------------------------------------------------------------
344
/**
345      * Check if the user has Admin access for the specified field. Admin access means
346      * setting rights on data.
347      *
348      * @param user Reference to the user.
349      *
350      * @return Return true if the user has admin access for the specified field,
351      * or false in any other case.
352      */

353     public final boolean checkAdminAccess (JahiaUser user)
354     {
355         return checkAccess (user, JahiaBaseACL.ADMIN_RIGHTS);
356     }
357
358
359     // Note : There are no Admin rights on fields.
360

361     //-------------------------------------------------------------------------
362
private boolean checkAccess (JahiaUser user, int permission)
363     {
364         if (user == null) {
365             return false;
366         }
367
368         //JahiaConsole.println ("->> field checkAccess : field ["+Integer.toString (ID)+
369
// "], permission ["+Integer.toString (permission)+"], user ["+user.getName()+"]");
370

371         boolean result = false;
372         try
373         {
374             // Try to instanciate the ACL.
375
JahiaBaseACL fieldACL = new JahiaBaseACL (aclID);
376
377             // Test the access rights
378
result = fieldACL.getPermission (user, permission);
379
380             // destroy the object.
381
fieldACL = null;
382         }
383         catch (ACLNotFoundException ex) {
384             //JahiaConsole.println ("JahiaField", "Could not find the ACL ["+Integer.toString(aclID)+
385
// "] for field ["+Integer.toString(ID)+"]");
386
}
387         catch (JahiaException ex) {
388             logger.error("JahiaException caught in checkAccess.", ex);
389         }
390
391         //if (!result) {
392
// JahiaConsole.println ("JahiaField", "Permission denied for user ["+
393
// user.getName()+"] to field ["+Integer.toString(ID)+
394
// "] for access permission ["+Integer.toString(permission)+"]");
395
//}
396
return result;
397     }
398
399
400
401
402     /**
403      * Generate an html anchor composed by the fieldID
404      *
405      * @author NK
406      */

407     public String JavaDoc getAnchor(){
408
409         StringBuffer JavaDoc anchor = new StringBuffer JavaDoc("<A NAME=");
410         anchor.append(this.getID());
411         anchor.append("></A>");
412
413         return anchor.toString();
414     }
415
416     /**
417      * Is this kind of field shared (i.e. not one version for each language, but one version for every language)
418      */

419     public abstract boolean isShared ();
420
421     private void registerChange(){
422         this.hasChanged = true;
423     }
424
425     /**
426      * Copy the internal value of current language to another language.
427      * Must be implemented by conctrete field for specific implementation.
428      *
429      * @param aField A same field in another language
430      */

431     public abstract void copyValueInAnotherLanguage (JahiaField aField,ParamBean jParams)
432     throws JahiaException;
433
434     /**
435      * Copy the internal value of this field to the passed field.
436      * Should be implemented by concrete field for specific implementation.
437      *
438      * Can be used to restore versioned value to a staging field.
439      *
440      * @param aField A same field in another language
441      */

442     public void copyValueToAnotherField (JahiaField aField,ParamBean jParams)
443     throws JahiaException{
444
445         aField.setValue(this.getValue());
446         aField.setRawValue(this.getValue());
447         aField.setObject(this.getObject());
448     }
449
450     /**
451      * Returns an Hashmap of language_code/values used by search index engine
452      *
453      * @return
454      */

455     public HashMap JavaDoc getValuesForSearch () throws JahiaException {
456
457         HashMap JavaDoc values = new HashMap JavaDoc();
458         if ( this.isShared() ){
459             values.put(ContentField.SHARED_LANGUAGE,this.getValues());
460         } else {
461             String JavaDoc[] vals = this.getValues();
462             if ( vals == null ){
463                 vals = new String JavaDoc[0];
464                 ArrayList JavaDoc aList = new ArrayList JavaDoc();
465                 aList.add("");
466                 vals = (String JavaDoc[])aList.toArray(vals);
467             }
468             values.put(this.getLanguageCode(),vals);
469         }
470         return values;
471     }
472
473     public void setVersionID(int versionID){
474         this.versionID = versionID;
475     }
476
477     public void setWorkflowState(int workflowState){
478         this.workflowState = workflowState;
479     }
480
481     public void setlanguageCode(String JavaDoc languageCode){
482         this.languageCode = languageCode;
483     }
484
485     /**
486      * Try to get the content object from the Jahia field
487      *
488      * @return The content field if success, otherwise null.
489      */

490     public ContentField getContentField() {
491         ContentField contentField = null;
492         try {
493             contentField = ContentField.getField(ID);
494         } catch (JahiaException je) {
495             logger.debug(je);
496         }
497         return contentField;
498     }
499
500
501     /**
502      * Return the value with highlighted differences
503      *
504      * @param jParams
505      * @return
506      */

507     public String JavaDoc getHighLightDiffValue(ParamBean jParams) {
508         return getHighLightDiffValue(jParams.getDiffVersionID(),jParams);
509     }
510
511     /**
512      * Return the value with highlighted differences
513      *
514      * @param jParams
515      * @return
516      */

517     public String JavaDoc getHighLightDiffValue(int diffVersionID, ParamBean jParams) {
518
519         if ( diffVersionID == 0 ){
520             return this.getValue();
521         }
522
523         String JavaDoc oldValue = this.getValue();
524         String JavaDoc newValue = "";
525         String JavaDoc mergedValue = "";
526
527         try {
528             EntryLoadRequest loadVersion =
529                     EntryLoadRequest.getEntryLoadRequest(diffVersionID,
530                     this.languageCode);
531
532             JahiaField jahiaField = ServicesRegistry.getInstance()
533                                   .getJahiaFieldService()
534                                   .loadField(this.getID(),LoadFlags.ALL,
535                                   jParams,loadVersion);
536
537             int newValueWorkflowState = this.getWorkflowState();
538             if ( jahiaField != null ){
539                 newValue = jahiaField.getValue();
540                 newValueWorkflowState = jahiaField.getWorkflowState();
541             }
542
543             /*
544             if ( jahiaField != null ){
545                 newValue = jahiaField.getValue();
546
547                 if ( jahiaField.getWorkflowState() == ContentObjectEntryState.WORKFLOW_STATE_VERSIONING_DELETED
548                      || jahiaField.getWorkflowState() == ContentObjectEntryState.WORKFLOW_STATE_START_STAGING
549                      && jahiaField.getVersionID() == -1 ){
550
551                     return HunkTextDiffVisitor.getDeletedText(oldValue);
552                 }
553             }*/

554
555             // Highlight text diff
556
HunkTextDiffVisitor hunkTextDiffV = null;
557             if ( this.getVersionID() == -1 && newValueWorkflowState==1){
558                 // currently marked for delete compared with active
559
return HunkTextDiffVisitor.getDeletedText(oldValue);
560             } else if ( this.getWorkflowState() < newValueWorkflowState ){
561                 hunkTextDiffV = new HunkTextDiffVisitor(oldValue,newValue);
562             } else {
563                 hunkTextDiffV = new HunkTextDiffVisitor(newValue,oldValue);
564             }
565             hunkTextDiffV.highLightDiff();
566             mergedValue = hunkTextDiffV.getMergedDiffText();
567
568             /*
569             // Highlight text diff
570             HunkTextDiffVisitor hunkTextDiffV =
571                     new HunkTextDiffVisitor(oldValue,newValue);
572             hunkTextDiffV.highLightDiff();
573             mergedValue = hunkTextDiffV.getMergedDiffText();
574             */

575         } catch ( Throwable JavaDoc t ){
576             t.printStackTrace();
577         }
578         return mergedValue;
579     }
580
581
582     private static org.apache.log4j.Logger logger =
583             org.apache.log4j.Logger.getLogger(JahiaField.class);
584
585
586
587 } // end JahiaField
588
Popular Tags