1 4 package org.jahia.data.fields; 5 6 import java.util.HashMap ; 7 import java.util.Hashtable ; 8 import java.util.Iterator ; 9 import java.util.Properties ; 10 import java.util.Set ; 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 39 public JahiaPageField( Integer ID_, 40 Integer jahiaID_, 41 Integer pageID_, 42 Integer ctnid_, 43 Integer fieldDefID_, 44 Integer fieldType_, 45 Integer connectType_, 46 String fieldValue_, 47 Integer rank_, 48 Integer aclID_, 49 Integer versionID_, 50 Integer versionStatus_, 51 String 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 } 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 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 nfe) { 81 pageId = -1; 82 } 83 if (pageId != -1) { 84 85 JahiaPage thePage = null; 86 try { 87 91 thePage = contentPageField.getPage(jParams); 92 } catch ( JahiaException je ){ 93 String 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 CrossReferenceManager.getInstance() 106 .preloadObjectXRefsByRefObjectKey( 107 ContentPageKey.getChildInstance( 108 String.valueOf(jParams.getPageID()))); 109 110 if ( ContentPageXRefManager.getInstance() != null ){ 114 if ( jParams != null ){ 116 try { 117 Set pageIDs = ContentPageXRefManager.getInstance().getPageIDs(thePage.getID()); 118 if ( pageIDs != null && 119 !pageIDs.contains(new Integer (jParams.getPageID())) ){ 120 ContentPageXRefManager.getInstance().setPageID(thePage.getID(), 121 jParams.getPageID()); 122 } 123 } catch ( Throwable 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 if (jParams == null) { 136 break; 142 } 143 144 if (thePage.checkReadAccess (jParams.getUser())) { 145 148 JahiaPageDefinition def = thePage.getPageTemplate (); 149 150 if (def != null) { 151 this.setValue (thePage.getURL(jParams)); 153 } else { 154 163 164 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 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 241 } 242 243 public boolean save(ParamBean jParams) 244 throws JahiaException 245 { 246 String tmpVal = this.getValue(); 247 if (!tmpVal.equals("<empty>")) { 248 int pageId = -1; 249 try { 250 pageId = Integer.parseInt( tmpVal ); 251 } catch (NumberFormatException nfe) { 252 pageId = -1; 253 } 254 if (pageId != -1) { 255 JahiaPage thePage = (JahiaPage) this.getObject(); 257 if (thePage != null) { 258 259 thePage.commitChanges(true); 260 261 264 this.setValue( Integer.toString(thePage.getID()) ); 265 tmpVal = Integer.toString( thePage.getID() ); } else { 267 this.setValue( "" ); 268 tmpVal = "<No Page>"; } 270 } 271 } else { 272 tmpVal = "<No Page>"; } 274 ContentPageField contentPageField = (ContentPageField)ContentField.getField(this.getID(), true); 275 int pageIDToSet; 276 try { 277 pageIDToSet = Integer.parseInt(this.getValue()); 278 } catch (NumberFormatException 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 nfe) { 301 JahiaConsole.printe("JahiaPageField.delete", nfe); 302 } 303 } 304 305 public String getEngineName() 306 { 307 return "org.jahia.engines.shared.Page_Field"; 308 } 309 310 public String getFieldContent4Ranking() 311 { 312 String 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 getIconNameOff() 326 { 327 return "page"; 328 } 329 330 public String 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 t ) { 342 t.printStackTrace(); 343 } 344 345 if (thePage != null) { 346 this.setValue( Integer.toString(thePage.getID()) ); 347 } 348 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 (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 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 )(this.getProperties()).clone() ); 400 401 return clonedField; 402 } 403 404 public Object clone() 405 { 406 Object objItem = this.getObject(); 407 JahiaPageField pf = new JahiaPageField (new Integer (ID), new Integer (jahiaID), 408 new Integer (pageID), 409 new Integer (ctnid), 410 new Integer (fieldDefID), 411 new Integer (fieldType), 412 new Integer (connectType), 413 fieldValue, new Integer (rank), 414 new Integer (aclID), 415 new Integer (versionID), 416 new Integer (workflowState), 417 new String (getLanguageCode())); 418 pf.setObject(objItem); 419 return pf; 420 } 421 422 425 public boolean isShared (){ 426 return true; 427 } 428 429 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 451 public HashMap getValuesForSearch () throws JahiaException { 452 453 HashMap values = new HashMap (); 454 try { 455 456 ContentPage contentPage = 457 ServicesRegistry.getInstance() 458 .getJahiaPageService().lookupContentPage(Integer.parseInt(this.getValue()),false); 459 if ( contentPage != null ){ 460 Hashtable titles = contentPage.getTitles((this.getWorkflowState()>1)); 461 Iterator keys = titles.keySet().iterator(); 462 while ( keys.hasNext() ){ 463 String langCode = (String )keys.next(); 464 String val = (String )titles.get(langCode); 465 if ( val!= null ){ 466 String [] vals = {val}; 467 values.put(langCode,vals); 468 } 469 } 470 } 471 } catch ( Throwable t ){ 472 } 474 return values; 475 } 476 477 } 478 | Popular Tags |