KickJava   Java API By Example, From Geeks To Geeks.

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


1 // JahiaFileFieldWrapper
2
// YG 17.07.2001
3

4 package org.jahia.data.fields;
5
6 import java.util.ArrayList JavaDoc;
7 import java.util.HashMap JavaDoc;
8 import java.util.Properties JavaDoc;
9
10 import org.jahia.data.FormDataManager;
11 import org.jahia.data.ConnectionTypes;
12 import org.jahia.data.files.JahiaFileField;
13 import org.jahia.data.files.JahiaFile;
14 import org.jahia.exceptions.JahiaException;
15 import org.jahia.params.ParamBean;
16 import org.jahia.registries.ServicesRegistry;
17 import org.jahia.services.fields.ContentField;
18 import org.jahia.services.fields.ContentFileField;
19 import org.jahia.services.filemanager.JahiaFileFieldsManager;
20 import org.jahia.services.version.EntryLoadRequest;
21 import org.jahia.services.version.EntrySaveRequest;
22 import org.jahia.services.webdav.JahiaWebdavBaseService;
23 import org.jahia.services.webdav.DAVFileAccess;
24 import org.jahia.sharing.JahiaDataSourceManager;
25
26 public class JahiaFileFieldWrapper extends JahiaField implements JahiaAllowApplyChangeToAllLangField
27 {
28
29     private static org.apache.log4j.Logger logger =
30                 org.apache.log4j.Logger.getLogger(JahiaFileFieldWrapper.class);
31
32         /***
33         * constructor
34         * YG 17.07.2001
35         *
36         */

37     public JahiaFileFieldWrapper(Integer JavaDoc ID,
38                             Integer JavaDoc jahiaID,
39                             Integer JavaDoc pageID,
40                             Integer JavaDoc ctnid,
41                             Integer JavaDoc fieldDefID,
42                             Integer JavaDoc fieldType,
43                             Integer JavaDoc connectType,
44                             String JavaDoc fieldValue,
45                             Integer JavaDoc rank,
46                             Integer JavaDoc aclID,
47                             Integer JavaDoc versionID,
48                             Integer JavaDoc versionStatus,
49                             String JavaDoc languageCode )
50     {
51         super(ID, jahiaID, pageID, ctnid, fieldDefID, fieldType, connectType,
52               fieldValue, rank, aclID, versionID, versionStatus, languageCode);
53
54         if ( isShared() ){
55             this.languageCode = ContentField.SHARED_LANGUAGE;
56         }
57
58
59         JahiaFileField fField;
60         JahiaFile file = new JahiaFile(-1, // filemanager id
61
-1, // folder id
62
"", // upload user
63
"", // realname
64
"", // storage name
65
0, // modif date
66
0, // size
67
"", // type
68
"", // title
69
"", // descr
70
String.valueOf(ServicesRegistry.getInstance()
71                 .getJahiaVersionService().getCurrentVersionID()), // version
72
JahiaFile.STATE_ACTIVE );
73         fField = new JahiaFileField(file,new Properties JavaDoc());
74         fField.setID(-1);
75         fField.setDownloadUrl("#");
76         setObject(fField);
77     } // end constructor
78

79
80     public void load(int loadFlag, ParamBean jParams)
81             throws JahiaException {
82         ContentFileField contentFileField =
83                 (ContentFileField)ContentFileField.getField(getID());
84         String JavaDoc val = contentFileField.getValue(jParams);
85         this.setValue(FormDataManager.getInstance().htmlEncode(val));
86
87         switch (this.getConnectType())
88         {
89             case (ConnectionTypes.LOCAL) :
90                 //this.setValue(this.getValue());
91
// if (!this.getValue().equals("<empty>")) {
92
this.setRawValue(this.getValue());
93                     this.setValue(FormDataManager.getInstance().htmlEncode(this.getValue()));
94 // }
95
break;
96             case (ConnectionTypes.DATASOURCE) :
97             if ((loadFlag & LoadFlags.DATASOURCE) != 0) {
98                 this.setValue( JahiaDataSourceManager.getInstance().
99                     fields().getInstance().getRemoteFieldValue(
100                     this.getValue() ));
101             }
102             }
103
104
105             JahiaFileField fField = null;
106             if (jParams != null) {
107                 if (jParams.getSiteID() == this.getJahiaID()) {
108                     fField = JahiaWebdavBaseService.getInstance().getJahiaFileField(jParams, val);
109                 } else {
110                     fField = JahiaWebdavBaseService.getInstance().getJahiaFileField(jParams, ServicesRegistry.getInstance().getJahiaSitesService().getSite(getSiteID()), jParams.getUser(), val);
111                 }
112             } else {
113                 DAVFileAccess davAccess = JahiaWebdavBaseService.getInstance().getDAVFileAccess(ServicesRegistry.getInstance().getJahiaSitesService().getSite(getSiteID()), val);
114                 if ( davAccess != null ){
115                     fField = davAccess.getJahiaFileField();
116                 }
117             }
118             this.setObject(fField);
119
120     }
121
122 // public void load(int loadFlag, ParamBean jParams)
123
// throws JahiaException
124
// {
125
// logger.debug("loading field : " + this.getID() );
126
//
127
// ContentFileField contentFileField =
128
// (ContentFileField)ContentFileField.getField(getID());
129
// String val = contentFileField.getValue(jParams);
130
// this.setValue(FormDataManager.getInstance().htmlEncode(val));
131
//
132
// int fileFieldID = -1;
133
// try {
134
// fileFieldID = Integer.parseInt(this.getValue());
135
// } catch ( Throwable t ){
136
// // this can fail in the case of a default value.
137
// }
138
// JahiaFileField fField = JahiaFileFieldsManager.getInstance()
139
// .getJahiaFileField(fileFieldID);
140
//
141
// // set the download url
142
// String dUrl = "#";
143
// if (fField != null)
144
// {
145
// //System.out.println("JahiaFieldBaseService::loadField() fField is not null and id=" + fField.getFileID() );
146
// if ( fField.getFileID() != -1 )
147
// {
148
// this.setObject(fField);
149
// Vector params = new Vector();
150
// params.add("actionFileDownload");
151
// params.add(this);
152
// dUrl = ((Filemanager_Engine)EnginesRegistry.getInstance().
153
// getEngine("filemanager")).renderLink(jParams, params);
154
// }
155
//
156
// fField.setDownloadUrl(dUrl);
157
// this.setObject(fField);
158
// }
159
// }
160

161
162
163     /**
164      * save the information specific to a type
165      * save the Object in relation with the field
166      * a JahiaPage or a file, for example.
167      *
168      */

169     public boolean save(ParamBean jParams)
170     throws JahiaException {
171
172         String JavaDoc pagePrefix = getID()+";";
173         ContentFileField contentFileField =
174                 (ContentFileField)ContentFileField.getField(getID());
175
176             String JavaDoc val = contentFileField.getValue(jParams);
177         if (val == null) val ="";
178 // DAVFileAccess oldObjectDav = JahiaWebdavBaseService.getInstance().getDAVFileAccess(jParams, val);
179
//
180
// if (oldObjectDav.hasRevisions()) {
181
// String oldList = (String) oldObjectDav.getProperty("http://www.jahia.org/", "fieldsinuse");
182
// if (oldList == null) {
183
// oldList = "";
184
// }
185
// if (oldList.startsWith(pagePrefix)) {
186
// oldList = oldList.substring(pagePrefix.length());
187
// } else if (oldList.indexOf(";"+pagePrefix)>0) {
188
// oldList = oldList.substring(0,oldList.indexOf(";"+pagePrefix))+ oldList.substring(oldList.indexOf(";"+pagePrefix)+pagePrefix.length());
189
// }
190
// oldObjectDav.setProperty("http://www.jahia.org/", "fieldsinuse", oldList, true);
191
// }
192

193         JahiaFileField fField = (JahiaFileField)this.getObject();
194
195             if ( this.getWorkflowState() == EntryLoadRequest.ACTIVE_WORKFLOW_STATE ){
196                 // if the current field is active, a staging entry is going to be created.
197
// we force insert a new jahia file field for the staging entry.
198
fField.setID(-1);
199             }
200 // JahiaFileFieldsManager.getInstance().insertJahiaFileField(fField);
201
EntrySaveRequest saveRequest =
202                     new EntrySaveRequest(jParams.getUser(), this.getLanguageCode());
203             contentFileField.setFile(fField, saveRequest);
204             ServicesRegistry.getInstance().getJahiaSearchService().indexField(getID(),true, jParams, true);
205
206 // DAVFileAccess objectDav = JahiaWebdavBaseService.getInstance().getDAVFileAccess(jParams, fField.getRealName());
207
// if (objectDav.hasRevisions()) {
208
// String list = (String) objectDav.getProperty("http://www.jahia.org/", "fieldsinuse");
209
// if (list == null) {
210
// list = "";
211
// }
212
// if (!list.startsWith(pagePrefix) && list.indexOf(";"+pagePrefix)==-1) {
213
// objectDav.setProperty("http://www.jahia.org/", "fieldsinuse", list + pagePrefix, true);
214
// }
215
// }
216

217         return true;
218     }
219
220 // public boolean save(ParamBean jParams)
221
// throws JahiaException {
222
//
223
// logger.debug("Save File Field...value : " + getValue());
224
//
225
// JahiaFileField fField = (JahiaFileField)this.getObject();
226
//
227
// if ( fField != null ){
228
//
229
// if ( this.getWorkflowState() == EntryLoadRequest.ACTIVE_WORKFLOW_STATE ){
230
// // if the current field is active, a staging entry is going to be created.
231
// // we force insert a new jahia file field for the staging entry.
232
// fField.setID(-1);
233
// }
234
// JahiaFileFieldsManager.getInstance().insertJahiaFileField(fField);
235
// ContentFileField contentFileField = (ContentFileField) ContentFileField.getField(getID());
236
// EntrySaveRequest saveRequest =
237
// new EntrySaveRequest(jParams.getUser(), this.getLanguageCode());
238
// contentFileField.setFile(fField, saveRequest);
239
// ServicesRegistry.getInstance().getJahiaSearchService().indexField(getID(),jParams);
240
// }
241
// return true;
242
// }
243

244
245     /**
246      * delete the information specific to a type
247      * delete the Object in relation with the field
248      * a JahiaPage or a file, for example.
249      *
250      */

251     public void delete(ParamBean jParams)
252             throws JahiaException {
253
254         String JavaDoc pagePrefix = getID()+";";
255         JahiaFileField fField = (JahiaFileField)this.getObject();
256             DAVFileAccess objectDav = JahiaWebdavBaseService.getInstance().getDAVFileAccess(jParams, fField.getRealName());
257         if (objectDav.hasRevisions()) {
258             String JavaDoc list = (String JavaDoc) objectDav.getProperty("http://www.jahia.org/", "fieldsinuse");
259             if (list == null) {
260                 list = "";
261             }
262             if (!list.startsWith(pagePrefix) && list.indexOf(";"+pagePrefix)==-1) {
263                 objectDav.setProperty("http://www.jahia.org/", "fieldsinuse", list + pagePrefix, true);
264             }
265         }
266     }
267
268 // public void delete(ParamBean jParams)
269
// throws JahiaException
270
// {
271
// int fileFieldID = -1;
272
// try {
273
// fileFieldID = Integer.parseInt(this.getValue());
274
// } catch ( Throwable t ){
275
//
276
// }
277
//
278
// JahiaFileFieldsManager.getInstance().deleteJahiaFileField(fileFieldID);
279
// }
280

281     public String JavaDoc getEngineName()
282     {
283         return "org.jahia.engines.shared.DAVFile_Field";
284 // return "org.jahia.engines.shared.File_Field";
285
}
286
287     public String JavaDoc getFieldContent4Ranking()
288     {
289         String JavaDoc fieldInfo = "";
290         JahiaFileField fle = (JahiaFileField)this.getObject();
291         if (fle != null)
292         {
293            fieldInfo = fle.getFileFieldTitle();
294         }
295         else
296         {
297            fieldInfo = this.getValue().substring(getValue().lastIndexOf('/')+1);
298         }
299         return fieldInfo;
300     }
301
302     public String JavaDoc getIconNameOff()
303     {
304         return "file";
305     }
306
307     public String JavaDoc getIconNameOn()
308     {
309         return "file_on";
310     }
311
312     public JahiaField cloneField(int newctnid, int newPageID, int clonedAclID, boolean childrenCloned)
313     throws JahiaException
314     {
315         JahiaField clonedField = ServicesRegistry.getInstance().getJahiaFieldService().
316                                  createJahiaField(0, this.getJahiaID(),
317                                  newPageID, newctnid,
318                                  this.getFieldDefID(), this.getType(),
319                                  this.getConnectType(),
320                                  this.getValue(), this.getRank(),
321                                  clonedAclID, this.getVersionID(), this.getWorkflowState(),
322                                  this.getLanguageCode());
323
324         //toDebug("cloneField(): value = "+this.getValue());
325
if (clonedField == null)
326         {
327             throw new JahiaException ("Could not clone field.",
328                 "Could not instanciate a new JahiaField object.",
329                 JahiaException.PAGE_ERROR, JahiaException.CRITICAL_SEVERITY);
330         }
331
332         clonedField.setRawValue(this.getRawValue());
333         // clonedField.setObject( this.getObject() );
334
clonedField.setProperties( (Properties JavaDoc)(this.getProperties()).clone() );
335
336         int theFieldID = ServicesRegistry.getInstance().getJahiaIncrementorsDBService().autoIncrement( "jahia_fields_data" );
337         clonedField.setID(theFieldID);
338         clonedField.setObject(getObject());
339
340         boolean flag = JahiaFileFieldsManager.getInstance().copyFileField(this.getID(), clonedField.getID(), clonedField.getPageID());
341
342         return clonedField;
343     }
344
345     public Object JavaDoc clone()
346     {
347         Object JavaDoc objItem = this.getObject();
348         JahiaFileFieldWrapper filef = new JahiaFileFieldWrapper (new Integer JavaDoc(ID), new Integer JavaDoc(jahiaID),
349                                         new Integer JavaDoc(pageID),
350                                         new Integer JavaDoc(ctnid),
351                                         new Integer JavaDoc(fieldDefID),
352                                         new Integer JavaDoc(fieldType),
353                                         new Integer JavaDoc(connectType),
354                                         fieldValue, new Integer JavaDoc(rank),
355                                         new Integer JavaDoc(aclID),
356                                         new Integer JavaDoc(versionID),
357                                         new Integer JavaDoc(workflowState),
358                                         new String JavaDoc(getLanguageCode()) );
359         filef.setObject(objItem);
360         return filef;
361     }
362
363     /**
364      * Is this kind of field shared (i.e. not one version for each language, but one version for every language)
365      */

366     public boolean isShared (){
367         return false;
368     }
369
370     /**
371      * Copy the internal value of current language to another language.
372      * Must be implemented by conctrete field for specific implementation.
373      *
374      * @param aField A same field in another language
375      */

376     public void copyValueInAnotherLanguage (JahiaField aField,ParamBean jParams)
377     throws JahiaException {
378         if ( aField == null ){
379             return;
380         }
381         // check the field is in different language first.
382
if ( this.getLanguageCode() != aField.getLanguageCode() ){
383             aField.setValue(this.getValue()); // force create a new Jahia File Filed
384
aField.setRawValue(this.getRawValue());
385             JahiaFileField fField =
386                     (JahiaFileField)this.getObject();
387             aField.setObject(fField);
388         }
389     }
390
391     /**
392      * Copy the internal value of this field to the passed field.
393      * Should be implemented by concrete field for specific implementation.
394      *
395      * Can be used to restore versioned value to a staging field.
396      *
397      * @param aField A same field in another language
398      */

399     public void copyValueToAnotherField (JahiaField aField,ParamBean jParams)
400     throws JahiaException{
401
402         if ( aField == null ){
403             return;
404         }
405         // check the field is in different language first.
406
aField.setValue(this.getValue()); // force create a new Jahia File Filed
407
aField.setRawValue(this.getRawValue());
408         JahiaFileField fField =
409                 (JahiaFileField)this.getObject();
410         aField.setObject(fField);
411     }
412
413     /**
414      * Returns an Hashmap of language_code/value used by search index engine
415      *
416      * @return
417      */

418     public HashMap JavaDoc getValuesForSearch () throws JahiaException {
419
420         HashMap JavaDoc values = new HashMap JavaDoc();
421         ArrayList JavaDoc arrayList = new ArrayList JavaDoc();
422         JahiaFileField fField = (JahiaFileField)this.getObject();
423         if ( fField != null ){
424             arrayList.add(fField);
425             values.put(this.getLanguageCode(),arrayList.toArray());
426         } else {
427             String JavaDoc[] vals = new String JavaDoc[0];
428             arrayList.add("");
429             vals = (String JavaDoc[])arrayList.toArray(vals);
430             values.put(this.getLanguageCode(),vals);
431         }
432         return values;
433     }
434
435
436 }
437
Popular Tags