KickJava   Java API By Example, From Geeks To Geeks.

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


1 // JahiaPageField
2
// YG 17.07.2001
3

4 package org.jahia.data.fields;
5
6 import java.util.HashMap JavaDoc;
7 import java.util.Hashtable JavaDoc;
8 import java.util.Iterator JavaDoc;
9 import java.util.Properties JavaDoc;
10 import java.util.Set JavaDoc;
11
12 import org.apache.log4j.Logger;
13 import org.jahia.content.CrossReferenceManager;
14 import org.jahia.content.ContentPageKey;
15 import org.jahia.content.ContentPageXRefManager;
16 import org.jahia.data.FormDataManager;
17 import org.jahia.exceptions.JahiaException;
18 import org.jahia.exceptions.JahiaPageNotFoundException;
19 import org.jahia.params.ParamBean;
20 import org.jahia.registries.ServicesRegistry;
21 import org.jahia.services.fields.ContentField;
22 import org.jahia.services.fields.ContentPageField;
23 import org.jahia.services.pages.ContentPage;
24 import org.jahia.services.pages.JahiaPage;
25 import org.jahia.services.pages.JahiaPageDefinition;
26 import org.jahia.utils.JahiaConsole;
27 import org.jahia.engines.login.Login_Engine;
28 import org.jahia.engines.logout.Logout_Engine;
29
30 public class JahiaPageField extends JahiaField
31 {
32     private static Logger logger = Logger.getLogger(JahiaPageField.class);
33
34         /***
35         * constructor
36         * YG 17.07.2001
37         *
38         */

39     public JahiaPageField( Integer JavaDoc ID_,
40                             Integer JavaDoc jahiaID_,
41                             Integer JavaDoc pageID_,
42                             Integer JavaDoc ctnid_,
43                             Integer JavaDoc fieldDefID_,
44                             Integer JavaDoc fieldType_,
45                             Integer JavaDoc connectType_,
46                             String JavaDoc fieldValue_,
47                             Integer JavaDoc rank_,
48                             Integer JavaDoc aclID_,
49                             Integer JavaDoc versionID_,
50                             Integer JavaDoc versionStatus_,
51                             String JavaDoc languageCode_)
52     {
53         super(ID_, jahiaID_, pageID_, ctnid_, fieldDefID_, fieldType_, connectType_,
54               fieldValue_, rank_, aclID_, versionID_, versionStatus_, languageCode_);
55
56         if ( isShared() ){
57             this.languageCode = ContentField.SHARED_LANGUAGE;
58         }
59
60     } // end constructor
61

62
63     public void load(int loadFlag, ParamBean jParams)
64     throws JahiaException
65     {
66
67
68         if ((loadFlag & (LoadFlags.PAGE | LoadFlags.ALL)) != 0 )
69         {
70
71             ContentPageField contentPageField = (ContentPageField)ContentField.getField(getID());
72             String JavaDoc val = contentPageField.getValue(jParams);
73             this.setValue(FormDataManager.getInstance().htmlEncode(val));
74             this.setRawValue(val);
75
76             int pageId = 0;
77             if (!this.getValue().equals("<NoPage>")) {
78             try {
79                 pageId = Integer.parseInt( this.getValue() );
80             } catch (NumberFormatException JavaDoc nfe) {
81                 pageId = -1;
82             }
83             if (pageId != -1) {
84
85                 JahiaPage thePage = null;
86                 try {
87                     /*
88                     thePage = ServicesRegistry.getInstance().
89                               getJahiaPageService().lookupPage (pageID, jParams);
90                      */

91                     thePage = contentPageField.getPage(jParams);
92                 } catch ( JahiaException je ){
93                     // an unexpected error occured with requested page
94
String JavaDoc msg = "An exception occured with requested pageid="
95                                  + pageId + "\n" + je.getMessage();
96                     logger.debug(msg,je);
97                     thePage = null;
98                 }
99                 if ( thePage != null && thePage.getPageType() != -1 ){
100                     this.setObject (thePage);
101                 }
102                 if (thePage != null) {
103
104                     // Performance issue
105
CrossReferenceManager.getInstance()
106                         .preloadObjectXRefsByRefObjectKey(
107                         ContentPageKey.getChildInstance(
108                         String.valueOf(jParams.getPageID())));
109
110                     // now let's update the xref manager from this page field to
111
// the actual page (actually the call uses the reverse order
112
// for parameter call)
113
if ( ContentPageXRefManager.getInstance() != null ){
114                         //JahiaConsole.println("PageXRefManager","PageXRefManager instance is not null ");
115
if ( jParams != null ){
116                             try {
117                                 Set JavaDoc pageIDs = ContentPageXRefManager.getInstance().getPageIDs(thePage.getID());
118                                 if ( pageIDs != null &&
119                                      !pageIDs.contains(new Integer JavaDoc(jParams.getPageID())) ){
120                                     ContentPageXRefManager.getInstance().setPageID(thePage.getID(),
121                                                                         jParams.getPageID());
122                                 }
123                             } catch ( Throwable JavaDoc t ){
124                                 logger.debug("Exception registering page cross reference", t);
125                             }
126                         }
127                     } else {
128                         JahiaConsole.println("PageXRefManager","PageXRefManager instance is null ");
129                     }
130
131                     switch (thePage.getPageType ()) {
132                         case JahiaPage.TYPE_DIRECT :
133
134                             // MJ 01.04.2001
135
if (jParams == null) {
136                                 //this.setObject( null );
137
//if (this.getValue().toUpperCase().indexOf("JAHIA_LINKONLY") == -1)
138
//{
139
// this.setValue( "");
140
//}
141
break;
142                             }
143
144                             if (thePage.checkReadAccess (jParams.getUser())) {
145                                 // check if the page definition of this page is visible or not
146
// if not deactivate the page !
147

148                                 JahiaPageDefinition def = thePage.getPageTemplate ();
149
150                                 if (def != null) {
151                                     //if ((def != null) && (def.isVisible())) {
152
this.setValue (thePage.getURL(jParams));
153                                 } else {
154                                     // FIXME : NK
155
// If a direct page has its template deleted,
156
// it should not mean we have to reset the page ????????
157
/*
158                                     this.setObject( null );
159                                     if (this.getValue().toUpperCase().indexOf("JAHIA_LINKONLY") == -1)
160                                     {
161                                         this.setValue( "");
162                                     }*/

163
164                                     // set the url to the page , but we sure have a page template not found when accessing it
165
this.setValue (thePage.getURL(jParams));
166
167                                 }
168                             } else {
169                                 this.setObject( null );
170                                 if (this.getValue().toUpperCase().indexOf("JAHIA_LINKONLY") == -1)
171                                 {
172                                     this.setValue( "");
173                                 }
174                             }
175
176                             break;
177
178                         case JahiaPage.TYPE_LINK :
179                             JahiaPage tempPage = null;
180                             try {
181                                 tempPage =
182                                         ServicesRegistry.getInstance().
183                                         getJahiaPageService().lookupPage(
184                                         thePage.
185                                         getPageLinkID(), jParams);
186                             } catch (JahiaPageNotFoundException jpnfe) {
187                                 logger.debug("Page linked (pageID="+thePage.getPageLinkID()+") not found in page field " + getID() + ", id_jahia_pages_data=" + thePage.getID(), jpnfe);
188                                 this.setObject(null);
189                                 this.setValue("Page linked (pageID="+thePage.getPageLinkID()+") not found in page field " + getID());
190                             }
191
192                             if ((tempPage != null) && (jParams != null) && (jParams.getUser() != null))
193                             {
194                                 // no it's not, let's return null.
195
if (!tempPage.checkReadAccess (jParams.getUser()))
196                                 {
197                                     this.setObject (null);
198                                     if (this.getValue().toUpperCase().indexOf("JAHIA_LINKONLY") == -1)
199                                     {
200                                         this.setValue ("");
201                                     }
202                                 }
203                             } else {
204                                 if (jParams != null) {
205                                     if (jParams.getEngine().equals(ParamBean.CORE_ENGINE_NAME) ||
206                                         jParams.getEngine().equals(Login_Engine.ENGINE_NAME) ||
207                                         jParams.getEngine().equals(Logout_Engine.ENGINE_NAME)) {
208                                         this.setObject(null);
209                                         if (this.getValue().toUpperCase().
210                                             indexOf("JAHIA_LINKONLY") == -1) {
211                                             this.setValue("");
212                                         }
213                                     }
214                                 }
215                             }
216
217                             break;
218                         }
219
220                 } else {
221                     if (this.getValue().toUpperCase().indexOf("JAHIA_LINKONLY") == -1)
222                     {
223                         this.setValue ("");
224                     }
225                 }
226             } else {
227                 if (this.getValue().toUpperCase().indexOf("JAHIA_LINKONLY") == -1)
228                 {
229                     this.setValue ("");
230                 }
231             }
232         } else {
233                 if (this.getValue().toUpperCase().indexOf("JAHIA_LINKONLY") == -1)
234                 {
235                     this.setValue ("");
236                 }
237             }
238         }
239         //System.out.println("######## end loadField fieldValue: "+this.getValue()+"#########");
240

241     }
242
243     public boolean save(ParamBean jParams)
244     throws JahiaException
245     {
246         String JavaDoc tmpVal = this.getValue();
247         if (!tmpVal.equals("<empty>")) {
248             int pageId = -1;
249             try {
250                 pageId = Integer.parseInt( tmpVal );
251             } catch (NumberFormatException JavaDoc nfe) {
252                 pageId = -1;
253             }
254             if (pageId != -1) {
255                 // checks to see if the page already exists
256
JahiaPage thePage = (JahiaPage) this.getObject();
257                 if (thePage != null) {
258
259                     thePage.commitChanges(true);
260
261                     //ServicesRegistry.getInstance().
262
// getJahiaPageService().savePageInfo( thePage );
263

264                     this.setValue( Integer.toString(thePage.getID()) );
265                     tmpVal = Integer.toString( thePage.getID() ); // Very useful !???
266
} else {
267                     this.setValue( "" );
268                     tmpVal = "<No Page>"; // Very useful !???
269
}
270             }
271         } else {
272             tmpVal = "<No Page>"; // Very useful !???
273
}
274         ContentPageField contentPageField = (ContentPageField)ContentField.getField(this.getID(), true);
275         int pageIDToSet;
276         try {
277             pageIDToSet = Integer.parseInt(this.getValue());
278         } catch (NumberFormatException JavaDoc nfe) {
279             pageIDToSet = -1;
280         }
281         contentPageField.setPageID(pageIDToSet, jParams.getUser());
282         ServicesRegistry.getInstance().getJahiaSearchService().indexField(getID(), true, jParams, true);
283
284         return true;
285     }
286
287     public void delete(ParamBean jParams)
288     throws JahiaException
289     {
290         try {
291             int pageId = Integer.parseInt( this.getValue() );
292             JahiaPage thePage = ServicesRegistry.getInstance().
293                 getJahiaPageService().lookupPage (pageId, jParams);
294
295             if (thePage != null) {
296                 ServicesRegistry.getInstance().getJahiaPageService().
297                     deletePage (thePage, jParams);
298             }
299
300         } catch (NumberFormatException JavaDoc nfe) {
301             JahiaConsole.printe("JahiaPageField.delete", nfe);
302         }
303     }
304
305     public String JavaDoc getEngineName()
306     {
307         return "org.jahia.engines.shared.Page_Field";
308     }
309
310     public String JavaDoc getFieldContent4Ranking()
311     {
312         String JavaDoc fieldInfo = "";
313         JahiaPage page = (JahiaPage)this.getObject();
314         if (page != null)
315         {
316             fieldInfo = page.getTitle();
317         }
318         else
319         {
320            fieldInfo = this.getValue();
321         }
322         return fieldInfo;
323     }
324
325     public String JavaDoc getIconNameOff()
326     {
327         return "page";
328     }
329
330     public String JavaDoc getIconNameOn()
331     {
332         return "page_on";
333     }
334
335     public JahiaField cloneField(int newctnid, int newPageID, int clonedAclID, boolean childrenCloned)
336     throws JahiaException
337     {
338         JahiaPage thePage = null;
339         try {
340             thePage = (JahiaPage) this.getObject();
341         } catch ( Throwable JavaDoc t ) {
342             t.printStackTrace();
343         }
344
345         if (thePage != null) {
346             this.setValue( Integer.toString(thePage.getID()) );
347         }
348         //System.out.println(" value set: " + this.getValue());
349

350         JahiaPage clonedPage = null;
351         JahiaField clonedField = null;
352         if ((thePage != null) && (thePage.getPageType() == JahiaPage.TYPE_URL | thePage.getPageType() == JahiaPage.TYPE_LINK))
353         {
354             clonedPage = ServicesRegistry.getInstance().getJahiaPageService().
355                          createPage(thePage.getJahiaID(), newPageID,
356                                     thePage.getPageType(), thePage.getTitle(),
357                                     thePage.getPageTemplateID(), thePage.getRemoteURL(),
358                                     thePage.getPageLinkID(), thePage.getCreator(),
359                                     clonedAclID, null);
360
361             clonedField = ServicesRegistry.getInstance().getJahiaFieldService().
362                                  createJahiaField(0, this.getJahiaID(),
363                                  newPageID, newctnid,
364                                  this.getFieldDefID(), this.getType(),
365                                  this.getConnectType(),
366                                  (new Integer JavaDoc(clonedPage.getID())).toString(), this.getRank(),
367                                  clonedAclID, this.getVersionID(), this.getWorkflowState(),
368                                  this.getLanguageCode());
369
370             clonedField.setObject( clonedPage );
371
372         }
373         else
374         {
375             if (!childrenCloned) {
376                 this.setValue("");
377             }
378             clonedField = ServicesRegistry.getInstance().getJahiaFieldService().
379                                  createJahiaField(0, this.getJahiaID(),
380                                  newPageID, newctnid,
381                                  this.getFieldDefID(), this.getType(),
382                                  this.getConnectType(),
383                                  this.getValue(), this.getRank(),
384                                  clonedAclID, this.getVersionID(), this.getWorkflowState(),
385                                  this.getLanguageCode());
386
387             clonedField.setObject( this.getObject() );
388         }
389
390         //toDebug("cloneField(): value = "+this.getValue());
391
if (clonedField == null)
392         {
393             throw new JahiaException ("Could not clone field.",
394                 "Could not instanciate a new JahiaField object.",
395                 JahiaException.PAGE_ERROR, JahiaException.CRITICAL_SEVERITY);
396         }
397
398         clonedField.setRawValue(this.getRawValue());
399         clonedField.setProperties( (Properties JavaDoc)(this.getProperties()).clone() );
400
401         return clonedField;
402     }
403
404     public Object JavaDoc clone()
405     {
406         Object JavaDoc objItem = this.getObject();
407         JahiaPageField pf = new JahiaPageField (new Integer JavaDoc(ID), new Integer JavaDoc(jahiaID),
408                                         new Integer JavaDoc(pageID),
409                                         new Integer JavaDoc(ctnid),
410                                         new Integer JavaDoc(fieldDefID),
411                                         new Integer JavaDoc(fieldType),
412                                         new Integer JavaDoc(connectType),
413                                         fieldValue, new Integer JavaDoc(rank),
414                                         new Integer JavaDoc(aclID),
415                                         new Integer JavaDoc(versionID),
416                                         new Integer JavaDoc(workflowState),
417                                         new String JavaDoc(getLanguageCode()));
418         pf.setObject(objItem);
419         return pf;
420     }
421
422     /**
423      * Is this kind of field shared (i.e. not one version for each language, but one version for every language)
424      */

425     public boolean isShared (){
426         return true;
427     }
428
429     /**
430      * Copy the internal value of current language to another language.
431      * Must be implemented by conctrete field for specific implementation.
432      *
433      * @param aField A same field in another language
434      //@todo : complete specific
435      */

436     public void copyValueInAnotherLanguage (JahiaField aField,ParamBean jParams)
437     throws JahiaException {
438         if ( aField == null ){
439             return;
440         }
441         aField.setValue(this.getValue());
442         aField.setRawValue(this.getValue());
443         aField.setObject(this.getObject());
444     }
445
446     /**
447      * Returns an Hashmap of language_code/value used by search index engine
448      *
449      * @return
450      */

451     public HashMap JavaDoc getValuesForSearch () throws JahiaException {
452
453         HashMap JavaDoc values = new HashMap JavaDoc();
454         try {
455
456             ContentPage contentPage =
457                     ServicesRegistry.getInstance()
458                     .getJahiaPageService().lookupContentPage(Integer.parseInt(this.getValue()),false);
459             if ( contentPage != null ){
460                 Hashtable JavaDoc titles = contentPage.getTitles((this.getWorkflowState()>1));
461                 Iterator JavaDoc keys = titles.keySet().iterator();
462                 while ( keys.hasNext() ){
463                     String JavaDoc langCode = (String JavaDoc)keys.next();
464                     String JavaDoc val = (String JavaDoc)titles.get(langCode);
465                     if ( val!= null ){
466                         String JavaDoc[] vals = {val};
467                         values.put(langCode,vals);
468                     }
469                 }
470             }
471         } catch ( Throwable JavaDoc t ){
472             //t.printStackTrace();
473
}
474         return values;
475     }
476
477 }
478
Popular Tags