1 43 44 45 package org.jahia.services.fields; 46 47 import java.io.File ; 48 import java.io.IOException ; 49 import java.lang.reflect.Constructor ; 50 import java.lang.reflect.InvocationTargetException ; 51 import java.util.Enumeration ; 52 import java.util.Hashtable ; 53 import java.util.Map ; 54 import java.util.Set ; 55 import java.util.SortedSet ; 56 import java.util.TreeSet ; 57 import java.util.Vector ; 58 59 import org.apache.log4j.Logger; 60 import org.jahia.bin.Jahia; 61 import org.jahia.data.ConnectionTypes; 62 import org.jahia.data.JahiaDOMObject; 63 import org.jahia.data.JahiaData; 64 import org.jahia.data.containers.JahiaContainer; 65 import org.jahia.data.containers.JahiaContainerList; 66 import org.jahia.data.events.JahiaEvent; 67 import org.jahia.data.fields.FieldTypes; 68 import org.jahia.data.fields.JahiaField; 69 import org.jahia.data.fields.JahiaFieldDefinition; 70 import org.jahia.data.fields.JahiaFieldSet; 71 import org.jahia.data.fields.LoadFlags; 72 import org.jahia.exceptions.JahiaException; 73 import org.jahia.exceptions.JahiaInitializationException; 74 import org.jahia.params.ParamBean; 75 import org.jahia.registries.JahiaFieldDefinitionsRegistry; 76 import org.jahia.registries.ServicesRegistry; 77 import org.jahia.services.acl.JahiaBaseACL; 78 import org.jahia.services.containers.FieldsChangeEventListener; 79 import org.jahia.services.containers.JahiaContainersService; 80 import org.jahia.services.files.JahiaTextFileService; 81 import org.jahia.services.pages.ContentPage; 82 import org.jahia.services.usermanager.JahiaUser; 83 import org.jahia.services.version.ActivationTestResults; 84 import org.jahia.services.version.ContentObjectEntryState; 85 import org.jahia.services.version.EntryLoadRequest; 86 import org.jahia.services.version.JahiaSaveVersion; 87 import org.jahia.services.version.StateModificationContext; 88 import org.jahia.settings.SettingsBean; 89 import org.jahia.utils.xml.XMLSerializationOptions; 90 import org.jahia.utils.xml.XmlWriter; 91 92 93 public class JahiaFieldBaseService extends JahiaFieldService { 94 95 private static Logger logger = Logger.getLogger (JahiaFieldBaseService.class); 96 97 98 private static JahiaFieldBaseService instance; 99 100 private static SettingsBean jSettings; 101 102 private JahiaFieldsDB fieldsDB; 103 private JahiaFieldDefinitionsDB f_defs; 104 private JahiaFieldUtilsDB f_utils; 105 106 private Hashtable fieldClassNames; 107 private Hashtable fieldClassConstructor = new Hashtable (); 108 109 public static final String FIELD_CACHE = "FieldCache"; 111 113 114 117 protected JahiaFieldBaseService () { 118 logger.debug ("***** Starting up the Field Services *****"); 119 } 120 121 122 127 public static synchronized JahiaFieldBaseService getInstance () { 128 129 if (instance == null) { 130 instance = new JahiaFieldBaseService (); 131 } 132 return instance; 133 } 134 135 136 141 public void init (SettingsBean settings) 142 throws JahiaInitializationException { 143 JahiaFieldBaseService.jSettings = settings; 144 145 fieldsDB = new JahiaFieldsDB (JahiaFieldBaseService.jSettings); 146 f_defs = new JahiaFieldDefinitionsDB (); 147 f_utils = new JahiaFieldUtilsDB (); 148 149 fieldClassNames = FieldTypes.getInstance ().getFieldClassNames (); 152 153 155 createFileDefinitionRepository (); 157 } 158 159 160 168 public JahiaField createJahiaField (int ID, 169 int jahiaID, 170 int pageID, 171 int ctnid, 172 int fieldDefID, 173 int fieldType, 174 int connectType, 175 String fieldValue, 176 int rank, 177 int aclID, 178 int versionID, 179 int versionStatus, 180 String languageCode) 181 throws JahiaException { 182 JahiaField theField; 183 184 fieldValue = checkFieldEnumerationValues (fieldValue); 185 186 try { 187 188 logger.debug ("fieldType: " + fieldType + " class: " 189 + fieldClassNames.get (new Integer (fieldType))); 190 191 Integer fieldTypeInt = new Integer (fieldType); 194 Constructor thisConstructor = null; 195 if (fieldClassConstructor.containsKey (fieldTypeInt)) { 196 thisConstructor = (Constructor ) fieldClassConstructor.get (fieldTypeInt); 197 } else { 198 Class theParams[] = {Integer .class, 200 Integer .class, 201 Integer .class, 202 Integer .class, 203 Integer .class, 204 Integer .class, 205 Integer .class, 206 String .class, 207 Integer .class, 208 Integer .class, 209 Integer .class, 210 Integer .class, 211 String .class}; 212 213 String fieldClassName = (String ) fieldClassNames 214 .get(new Integer (fieldType)); 215 if (fieldClassName != null) { 216 thisConstructor = Class.forName(fieldClassName) 217 .getDeclaredConstructor(theParams); 218 fieldClassConstructor.put(fieldTypeInt, thisConstructor); 219 } else { 220 throw new JahiaException("Error accessing field", 221 "Couldn't find field class name for type " 222 + fieldType + " pageID=" + pageID 223 + " fieldDefID=" + fieldDefID + " ctnID=" 224 + ctnid, JahiaException.PERSISTENCE_ERROR, 225 JahiaException.ERROR_SEVERITY); 226 } 227 } 228 229 230 Object args[] = {new Integer (ID), new Integer (jahiaID), 232 new Integer (pageID), new Integer (ctnid), 233 new Integer (fieldDefID), 234 new Integer (fieldType), 235 new Integer (connectType), fieldValue, 236 new Integer (rank), new Integer (aclID), 237 new Integer (versionID), new Integer (versionStatus), 238 new String (languageCode)}; 239 240 241 theField = (JahiaField) thisConstructor.newInstance (args); 243 244 } catch (ClassNotFoundException cnfe) { 245 logger.debug ("exception (class nf) " + cnfe.toString (), cnfe); 246 throw new JahiaException ("JahiaFieldBaseService:createJahiaField", 247 "Class not found!", 248 JahiaException.PAGE_ERROR, JahiaException.CRITICAL_SEVERITY, cnfe); 249 250 } catch (NoSuchMethodException nsme) { 251 logger.debug ("createJahiaField: (method nf) " + nsme.toString (), nsme); 252 throw new JahiaException ("JahiaFieldBaseService:createJahiaField", 253 "Method not found!", 254 JahiaException.PAGE_ERROR, JahiaException.CRITICAL_SEVERITY, nsme); 255 256 } catch (IllegalAccessException iae) { 257 logger.debug ("createJahiaField: (illegal access) " + iae.toString (), iae); 258 throw new JahiaException ("JahiaFieldBaseService:createJahiaField", 259 "Illegal access", 260 JahiaException.PAGE_ERROR, JahiaException.CRITICAL_SEVERITY, iae); 261 262 } catch (InvocationTargetException ite) { 263 logger.debug ("createJahiaField: (invocation) " + ite.toString (), ite); 264 throw new JahiaException ("JahiaFieldBaseService:createJahiaField", 265 "InvocationTarget exception", 266 JahiaException.PAGE_ERROR, JahiaException.CRITICAL_SEVERITY, ite); 267 268 } catch (InstantiationException ie) { 269 logger.debug ("createJahiaField: (instantiation) " + ie.toString (), ie); 270 throw new JahiaException ("JahiaFieldBaseService:createJahiaField", 271 "Instantiation exception", 272 JahiaException.PAGE_ERROR, JahiaException.CRITICAL_SEVERITY, ie); 273 } 274 275 return theField; 277 } 278 279 280 299 public JahiaField createJahiaField (int ID, int jahiaID, int pageID, int ctnid, 300 int fieldDefID, int fieldType, int connectType, 301 String fieldValue, int rank, int aclID) 302 throws JahiaException { 303 fieldValue = checkFieldEnumerationValues (fieldValue); 304 305 return createJahiaField (ID, jahiaID, pageID, ctnid, fieldDefID, fieldType, 306 connectType, fieldValue, rank, aclID, 0, 307 EntryLoadRequest.STAGING_WORKFLOW_STATE, 308 Jahia.getSettings ().getDefaultLanguageCode ()); 309 } 310 311 312 319 public synchronized void cloneField (int fieldID, int newPageID, int parentAclID, 320 ParamBean jParams) 321 throws JahiaException { 322 JahiaField theField = loadField (fieldID, LoadFlags.NOTHING, jParams); 324 325 cloneField (theField, 0, newPageID, parentAclID, false); 327 } 328 329 330 339 public synchronized JahiaField cloneField (JahiaField theField, int newctnid, 340 int newPageID, int parentAclID, 341 boolean childrenCloned) 342 throws JahiaException { 343 logger.debug ("cloneField(): begin"); 345 theField = loadField (theField.getID (), LoadFlags.ALL, null); 346 if (theField == null) { 347 throw new JahiaException ("Could not clone field.", 348 "Could not get the JahiaField object.", 349 JahiaException.PAGE_ERROR, JahiaException.CRITICAL_SEVERITY); 350 } 351 352 353 JahiaBaseACL aclObj = theField.getACL(); 355 if (aclObj == null) { 356 throw new JahiaException ("Could not clone container.", 357 "Could not get JahiaBaseACL object.", 358 JahiaException.PAGE_ERROR, JahiaException.CRITICAL_SEVERITY); 359 } 360 361 JahiaBaseACL clonedACL = (JahiaBaseACL) aclObj.clone (); 362 if (clonedACL == null) { 363 throw new JahiaException ("Could not clone container.", 364 "Could not clone acl.", 365 JahiaException.PAGE_ERROR, JahiaException.CRITICAL_SEVERITY); 366 } 367 clonedACL.setParentID (parentAclID); 368 369 370 376 JahiaField clonedField = null; 377 378 clonedField = 379 theField.cloneField (newctnid, newPageID, clonedACL.getID (), childrenCloned); 380 381 408 409 logger.debug ("cloneField(): call saveField"); 411 412 saveField (clonedField, parentAclID, null); 414 415 return clonedField; 416 417 } 418 419 420 427 public JahiaFieldSet buildFieldStructureForPage (JahiaData jData, 428 EntryLoadRequest loadVersion) 429 throws JahiaException { 430 438 JahiaFieldSet theSet = new JahiaFieldSet (jData); 440 461 486 return theSet; 487 } 489 490 497 public Vector getNonContainerFieldIDsInPage (int pageID) 498 throws JahiaException { 499 return f_utils.dbGetNonContainerFieldIDsInPage (pageID); 500 } 502 510 public Vector getNonContainerFieldIDsInPageByWorkflowState (int pageID, 511 EntryLoadRequest loadVersion) 512 throws JahiaException { 513 return f_utils.dbGetNonContainerFieldIDsInPageByWorkflowState (pageID, loadVersion); 514 } 515 516 529 public SortedSet getNonContainerFieldEntryStateInPage (int pageID) 530 throws JahiaException { 531 SortedSet entryStates = new TreeSet (); 532 Vector fieldIDs = getNonContainerFieldIDsInPage (pageID); 533 Enumeration fieldIDEnum = fieldIDs.elements (); 534 while (fieldIDEnum.hasMoreElements ()) { 535 Integer curFieldID = (Integer ) fieldIDEnum.nextElement (); 536 ContentField curField = ContentField.getField (curFieldID.intValue ()); 537 entryStates.addAll (curField.getEntryStates ()); 538 } 539 return entryStates; 540 } 541 542 550 public Vector getPagefieldIDsInPage (int pageID) 551 throws JahiaException { 552 return f_utils.db_get_pagefield_ids_in_page (pageID); 553 } 555 556 564 public int getFieldID (String fieldName, int pageID) 565 throws JahiaException { 566 return f_utils.db_get_field_id (fieldName, pageID); 567 } 569 570 577 public Vector getAllFieldIDs (int siteID) 578 throws JahiaException { 579 580 return f_utils.db_get_all_fields_id (siteID); 581 582 } 583 584 585 590 public Vector getAllFieldIDs () 591 throws JahiaException { 592 return f_utils.db_get_all_fields_id (); 593 } 595 596 602 public Vector getAllFieldDefinitionIDs () 603 throws JahiaException { 604 return f_utils.db_get_all_field_definition_ids (); 605 } 607 608 609 618 619 658 659 660 672 public JahiaField loadField (int fieldID, int loadFlag) 673 throws JahiaException { 674 return loadField (fieldID, loadFlag, null); 675 } 677 678 689 public JahiaField loadField (int fieldID, ParamBean jParams) 690 throws JahiaException { 691 return loadField (fieldID, LoadFlags.ALL, jParams); 692 } 694 695 708 710 public JahiaField loadField (int fieldID, int loadFlag, ParamBean jParams) 711 throws JahiaException { 712 if (jParams != null) { 713 return loadField (fieldID, loadFlag, jParams, jParams.getEntryLoadRequest ()); 714 } else { 715 logger.debug ( 716 "LEGACY : Loading current version by default ! Try to use the newer APIs to avoid this message! "); 717 return loadField (fieldID, loadFlag, jParams, EntryLoadRequest.CURRENT); 718 719 } 720 } 721 722 public JahiaField loadField (int fieldID, int loadFlag, ParamBean jParams, 723 EntryLoadRequest loadVersion) 724 throws JahiaException { 725 JahiaField theField = null; 726 if (logger.isDebugEnabled()) { 728 String msg = "loading field: id=" + fieldID ; 729 if ( loadVersion != null ){ 730 msg += " loadVersion=[" + loadVersion.toString() + "]"; 731 } 732 logger.debug(msg); 733 } 734 735 ContentField contentField = null; 736 if (theField == null) { 737 contentField = ContentField.getField(fieldID); 739 if (contentField == null) { 740 return null; 741 } 742 theField = contentFieldToJahiaField(contentField, loadVersion); 743 } 744 745 if (theField == null) { 746 return null; 747 } 748 749 if ( loadVersion.isCurrent() 750 && theField.getWorkflowState() != EntryLoadRequest.ACTIVE_WORKFLOW_STATE ){ 751 return null; 753 } else if ( loadVersion.isStaging() ){ 754 if (!loadVersion.isWithMarkedForDeletion() 755 && contentField.isMarkedForDelete(theField.getLanguageCode())) { 756 return null; 758 } 759 } 760 761 772 773 if (jParams == null) { 775 } else { 777 JahiaUser currentUser = jParams.getUser (); 778 if (currentUser != null) { 779 if (theField.getctnid() > 0) { 783 JahiaContainersService jahiaContainersService = ServicesRegistry.getInstance().getJahiaContainersService(); 784 JahiaData jData = (JahiaData) jParams.getRequest().getAttribute( "org.jahia.data.JahiaData" ); 785 786 JahiaContainer container = jData != null && jData.containers() != null && loadVersion.equals(jParams.getEntryLoadRequest()) ? jData.containers().getContainer(theField.getctnid()) : 787 jahiaContainersService.loadContainer(theField.getctnid(), loadFlag, jParams, loadVersion); 788 if (container != null) { 789 JahiaContainerList containerList = jData != null && jData.containers() != null && loadVersion.equals(jParams.getEntryLoadRequest()) ? jData.containers().getContainerList(container.getListID()) : 790 jahiaContainersService.loadContainerList(container.getListID(), loadFlag, jParams); 791 if (containerList!=null && !containerList.isFieldReadable(theField.getDefinition().getName(), currentUser)) { 792 if (theField.getValue().toUpperCase().indexOf("JAHIA_LINKONLY") == -1) { 793 theField.setValue(""); } 795 return theField; 796 } 797 } 798 } 799 else if (!theField.checkReadAccess (currentUser)) { 801 if (theField.getValue ().toUpperCase ().indexOf ("JAHIA_LINKONLY") == -1) { 803 theField.setValue (""); } 805 return theField; 806 } 807 } else { 809 throw new JahiaException ("No user present !", 810 "No current user defined in the params in loadField() method.", 811 JahiaException.USER_ERROR, JahiaException.ERROR_SEVERITY); 812 } 813 } 814 816 loadField(theField,loadFlag,jParams,loadVersion); 821 822 return theField; 823 824 } 826 836 private JahiaField loadField (JahiaField theField, int loadFlag, ParamBean jParams, 837 EntryLoadRequest loadVersion) 838 throws JahiaException { 839 840 if ( theField == null ){ 841 return null; 842 } 843 844 if (logger.isDebugEnabled()) { 845 logger.debug( 846 "loading field: id=" + theField.getID() + " loadVersion=[" + 847 loadVersion.toString()); 848 } 849 850 852 boolean fieldLoadedSuccessfully = false; 853 try { 855 jParams.setSubstituteEntryLoadRequest (loadVersion); 856 logger.debug ("Calling field specific load"); 857 theField.load (loadFlag, jParams); 858 fieldLoadedSuccessfully = true; 859 } catch (Throwable t) { 860 logger.debug ("Error calling field load method fid[" + theField.getID () + "]", t); 861 867 } finally { 868 jParams.resetSubstituteEntryLoadRequest (); 869 } 870 871 if (theField.getValue () != null && theField.getValue ().equals ("<empty>")) { 872 theField.setValue (""); 873 } 874 875 if ((theField != null) && (fieldLoadedSuccessfully)) { 876 if ( !(loadVersion.isVersioned()) ) { 877 } 883 } 884 885 return theField; 886 } 888 896 public void saveField (JahiaField theField, int parentAclID, ParamBean jParams) 897 throws JahiaException { 898 logger.debug ("saving field #" + theField.getID ()); 899 String createField = ""; 901 if (theField.getObject () instanceof String ) { 902 createField = (String ) theField.getObject (); 903 } 904 905 if (jParams != null) { 907 JahiaUser currentUser = jParams.getUser (); 908 if (currentUser != null) { 909 logger.debug ("checking rights..."); 910 if ((!theField.checkWriteAccess (currentUser)) && (theField.getID () != 0)) { 913 if (createField.equals ("createFieldWithCurrentID")) { 914 logger.debug ("write rights OK"); 915 } else { 916 logger.debug ("NO write right"); 917 return; 918 } 919 } 920 } else { 922 throw new JahiaException ("No user present !", 923 "No current user defined in the params in saveField() method.", 924 JahiaException.USER_ERROR, JahiaException.ERROR_SEVERITY); 925 } 926 } 927 928 int savemode = 1; 930 931 if (createField.equals ("createFieldWithCurrentID")) { 932 theField.setObject (null); 934 savemode = 0; 936 } 937 938 if (theField.getID () == 0) { 939 int theFieldID = ServicesRegistry.getInstance ().getJahiaIncrementorsDBService () 941 .autoIncrement ("jahia_fields_data"); 942 theField.setID (theFieldID); 943 logger.debug("New field ID attributed="+ theFieldID); 944 savemode = 0; 945 } 946 947 int pageDefID = 0; 948 949 if ( theField.getDefinition().getJahiaID() != 0 ){ 950 ContentPage jahiaPage = ContentPage.getPage(theField.getPageID()); 951 pageDefID = jahiaPage.getPageTemplateID(jParams); 952 } 953 954 String errorMsg = ""; 955 if (theField.getDefinition ().getName ().equals ("")) { 956 errorMsg = "Error in FieldBaseService : field name value is an empty string"; 957 } 958 if (theField.getDefinition ().getTitle (pageDefID).equals ("")) { 959 errorMsg = "Error in FieldBaseService : field title value is an empty string"; 960 } 961 if ((theField.getValue () != null) && theField.getValue ().equals ("")) { 962 theField.setValue ("<empty>"); 963 } 964 965 if (errorMsg != "") { 966 throw new JahiaException ("Cannot update fields in the database", 968 errorMsg, JahiaException.DATABASE_ERROR, JahiaException.CRITICAL_SEVERITY); 969 } 970 971 int fieldType = theField.getDefinition ().getType (pageDefID); 973 if (fieldType == FieldTypes.UNDEFINED) { 974 fieldType = theField.getType (); 975 } 976 977 String tmpVal = ""; 979 if (fieldType == FieldTypes.BIGTEXT) { 980 tmpVal = "<text>"; 981 } 982 if (theField.getValue () != null) { 983 tmpVal = theField.getValue (); 984 } 985 986 if (theField.getAclID () == 0) { 988 if ( theField.getctnid() == 0){ 989 JahiaBaseACL acl = new JahiaBaseACL (); 990 if (acl != null) { 991 if (!acl.create (parentAclID)) { 993 String message = "Could not create an ACL object for a new field in container."; 994 logger.debug (message + " -> Stop container creation!"); 995 throw new JahiaException ("AddContainer_Engine", message, 996 JahiaException.ACL_ERROR, JahiaException.CRITICAL_SEVERITY); 997 } else { 998 logger.debug ("ACL [" + acl.getID () + "] has just been created!"); 999 } 1000 } else { 1001 throw new JahiaException ("JahiaFieldBaseService.saveField", "Could not instanciate the JahiaBaseACL class", 1002 JahiaException.ACL_ERROR, JahiaException.CRITICAL_SEVERITY); 1003 } 1004 theField.setAclID (acl.getID ()); 1006 } else { 1007 theField.setAclID (parentAclID); 1008 } 1009 } 1010 1011 1012 if (savemode == 0) 1014 { 1016 logger.debug ("CREATE - savemode: " + savemode); 1017 theField.setValue (tmpVal); 1020 1021 fieldsDB.db_create_field (theField, 1022 ServicesRegistry.getInstance ().getJahiaVersionService () 1023 .getSiteSaveVersion (theField.getJahiaID ())); 1024 1025 if ( theField != null ){ 1027 FieldsChangeEventListener listener = new FieldsChangeEventListener(); 1028 listener.notifyChange(theField.getID()); 1029 } 1030 1031 1033 1039 1040 1052 1053 } else 1054 { 1056 logger.debug ("UPDATE - savemode: " + savemode); 1057 1059 1062 logger.debug ("UPDATE - step1 "); 1063 1064 1068 theField.setValue (tmpVal); 1069 1070 logger.debug ("UPDATE - step2 "); 1071 1072 fieldsDB.db_update_field (theField, 1073 ServicesRegistry.getInstance ().getJahiaVersionService () 1074 .getSiteSaveVersion (theField.getJahiaID ())); 1075 1076 logger.debug ("UPDATE - step3 "); 1077 1078 1080 1086 1087 invalidateCacheField(theField.getID()); 1088 1089 1104 1105 logger.debug ("UPDATE - step4 "); 1106 1107 if (logMe (theField)) { 1109 JahiaEvent theEvent = new JahiaEvent (this, jParams, theField); 1110 ServicesRegistry.getInstance ().getJahiaEventService (). 1111 fireUpdateField (theEvent); 1112 } 1113 } 1114 1116 theField.save (jParams); 1119 1120 if (jParams != null) { 1122 if (logMe (theField)) { 1124 JahiaEvent theEvent = new JahiaEvent (this, jParams, theField); 1125 if (savemode == 0) { 1126 ServicesRegistry.getInstance ().getJahiaEventService (). 1127 fireAddField (theEvent); 1128 } else { 1129 ServicesRegistry.getInstance ().getJahiaEventService (). 1130 fireUpdateField (theEvent); 1131 } 1132 } 1133 } 1134 1135 } 1137 1138 1143 1144 public void deleteField (int fieldID, ParamBean jParams) 1145 throws JahiaException { 1146 ContentField contentField = ContentField.getField (fieldID); 1147 jParams.setSubstituteEntryLoadRequest (EntryLoadRequest.STAGED); 1148 JahiaField theField = contentFieldToJahiaField (contentField, 1149 jParams.getEntryLoadRequest ()); 1150 jParams.resetSubstituteEntryLoadRequest (); 1151 1153 boolean doIndexField = !ServicesRegistry.getInstance ().getJahiaSitesService () 1154 .isSiteToBeDeleted (theField.getJahiaID ()); 1155 1156 if (theField.getConnectType () == ConnectionTypes.LOCAL) { 1157 if (doIndexField) { 1158 ServicesRegistry.getInstance ().getJahiaSearchService () 1159 .removeFieldFromSearchEngine (theField); 1160 } 1161 } 1162 1163 if (theField.getConnectType () == ConnectionTypes.HTMLEDITOR) { 1165 if (doIndexField) { 1166 ServicesRegistry.getInstance ().getJahiaSearchService () 1167 .removeFieldFromSearchEngine (theField); 1168 } 1169 } 1170 if (theField.getConnectType () == ConnectionTypes.HTMLEDITORAX) { 1171 if (doIndexField) { 1172 ServicesRegistry.getInstance ().getJahiaSearchService () 1173 .removeFieldFromSearchEngine (theField); 1174 } 1175 } 1176 1177 int pageDefID = ServicesRegistry.getInstance ().getJahiaPageService (). 1178 lookupPage (theField.getPageID (), jParams).getPageTemplateID (); 1179 1180 int fieldType = theField.getDefinition ().getType (pageDefID); 1181 if (fieldType == FieldTypes.UNDEFINED) { 1182 fieldType = theField.getType (); 1183 } 1184 1185 JahiaSaveVersion saveVersion = ServicesRegistry.getInstance ().getJahiaVersionService () 1187 .getSiteSaveVersion (theField.getJahiaID ()); 1188 theField.delete (jParams); 1190 1191 1201 if (logMe (theField)) { 1202 JahiaEvent theEvent = new JahiaEvent (this, jParams, theField); 1203 ServicesRegistry.getInstance ().getJahiaEventService (). 1204 fireDeleteField (theEvent); 1205 } 1206 1207 fieldsDB.db_delete_field (theField.getID (), saveVersion); 1208 if (!saveVersion.isStaging ()) { 1210 invalidateCacheField(theField.getID()); 1211 } 1212 } 1214 public void markPageFieldsLanguageForDeletion (int pageID, 1215 JahiaUser user, 1216 String languageCode, 1217 StateModificationContext 1218 stateModifContext) 1219 throws JahiaException { 1220 Vector fieldIDs = f_utils.getActiveOrStagedFieldIDsInPage (pageID); 1221 for (int i = 0; i < fieldIDs.size (); i++) { 1222 int id = ((Integer ) fieldIDs.elementAt (i)).intValue (); 1223 1224 ContentField theField = ContentField.getField (id); 1225 if (theField != null && (theField.getContainerID () == 0)) { 1229 theField.markLanguageForDeletion (user, languageCode, stateModifContext); 1230 } 1231 } 1232 } 1233 1234 public void purgePageFields (int pageID) 1235 throws JahiaException { 1236 Vector fieldIDs = f_utils.dbGetNonContainerFieldIDsInPage (pageID); 1237 for (int i = 0; i < fieldIDs.size (); i++) { 1238 int id = ((Integer ) fieldIDs.elementAt (i)).intValue (); 1239 ContentField theField = ContentField.getField (id); 1240 theField.purge (); 1241 } 1242 } 1243 1244 public ActivationTestResults areFieldsValidForActivation ( 1245 Set languageCodes, 1246 int pageID, 1247 JahiaUser user, 1248 JahiaSaveVersion saveVersion, 1249 ParamBean jParams, 1250 StateModificationContext stateModifContext) 1251 throws JahiaException { 1252 1253 Vector fieldIDs = f_utils.db_get_only_staged_field_ids_in_page (pageID); 1254 1255 ActivationTestResults activationResults = new ActivationTestResults (); 1256 for (int i = 0; i < fieldIDs.size (); i++) { 1259 int id = ((Integer ) fieldIDs.elementAt (i)).intValue (); 1260 1261 ContentField theField = ContentField.getField (id); 1262 ActivationTestResults fieldResult = theField.isValidForActivation (languageCodes, 1263 jParams, stateModifContext); 1264 1265 if (fieldResult.getStatus () == ActivationTestResults.FAILED_OPERATION_STATUS) { 1266 fieldResult.setStatus (ActivationTestResults.PARTIAL_OPERATION_STATUS); 1267 fieldResult.moveErrorsToWarnings (); 1268 } 1269 activationResults.merge (fieldResult); 1270 1271 } 1272 return activationResults; 1273 1274 } 1275 1276 public ActivationTestResults areNonContainerFieldsValidForActivation ( 1277 Set languageCodes, 1278 int pageID, 1279 JahiaUser user, 1280 JahiaSaveVersion saveVersion, 1281 ParamBean jParams, 1282 StateModificationContext stateModifContext) 1283 throws JahiaException { 1284 1285 Vector fieldIDs = f_utils.db_get_only_staged_non_container_field_ids_in_page (pageID); 1286 1287 ActivationTestResults activationResults = new ActivationTestResults (); 1288 for (int i = 0; i < fieldIDs.size (); i++) { 1291 int id = ((Integer ) fieldIDs.elementAt (i)).intValue (); 1292 1293 ContentField theField = ContentField.getField (id); 1294 ActivationTestResults fieldResult = theField.isValidForActivation (languageCodes, 1295 jParams, stateModifContext); 1296 activationResults.merge (fieldResult); 1297 1298 } 1299 return activationResults; 1300 1301 } 1302 1303 1304 1312 public ActivationTestResults activateStagedFields ( 1313 Set languageCodes, 1314 int pageID, 1315 JahiaUser user, 1316 JahiaSaveVersion saveVersion, 1317 ParamBean jParams, 1318 StateModificationContext stateModifContext) throws JahiaException { 1319 1320 ActivationTestResults activationResults = new ActivationTestResults (); 1321 1322 1324 activationResults.merge ( 1325 areFieldsValidForActivation (languageCodes, pageID, user, saveVersion, jParams, 1326 stateModifContext)); 1327 if (activationResults.getStatus () == ActivationTestResults.FAILED_OPERATION_STATUS) { 1328 return activationResults; 1329 } 1330 1331 Vector fieldIDs = f_utils.db_get_only_staged_field_ids_in_page (pageID); 1332 for (int i = 0; i < fieldIDs.size (); i++) { 1335 int id = ((Integer ) fieldIDs.elementAt (i)).intValue (); 1336 logger.debug ("Attempting to validate field : " + id); 1337 1338 1340 ContentField contentField = ContentField.getField (id); 1341 if ( contentField instanceof ContentPageField ){ 1342 ActivationTestResults fieldResult = 1355 contentField.isValidForActivation (languageCodes, jParams, stateModifContext); 1356 if ( fieldResult.getStatus () == 1357 ActivationTestResults.FAILED_OPERATION_STATUS ){ 1358 continue; 1359 } 1360 } 1361 1362 JahiaEvent theEvent = new JahiaEvent( saveVersion, jParams, contentField ); 1364 ServicesRegistry.getInstance().getJahiaEventService() 1365 .fireBeforeFieldActivation(theEvent); 1366 1367 ActivationTestResults contentResult = contentField.activate (languageCodes, 1368 saveVersion.getVersionID (), jParams, stateModifContext); 1369 1370 activationResults.merge (contentResult); 1371 if (contentResult.getStatus () == ActivationTestResults.COMPLETED_OPERATION_STATUS) { 1372 invalidateCacheField(id); 1374 } else { 1375 logger.debug ( 1376 "Field " + id + " activation not completely performed. testResult=" + contentResult); 1377 } 1378 } 1379 1380 1381 return activationResults; 1382 } 1383 1384 1398 public void setFieldsLanguageStates ( 1399 Set languageCodes, 1400 int newWorkflowState, 1401 int pageID, 1402 ParamBean jParams, 1403 StateModificationContext stateModifContext) throws JahiaException { 1404 Vector fieldIDs = f_utils.db_get_only_staged_field_ids_in_page (pageID); 1405 for (int i = 0; i < fieldIDs.size (); i++) { 1408 int id = ((Integer ) fieldIDs.elementAt (i)).intValue (); 1409 1410 1412 ContentField contentField = ContentField.getField (id, true); 1413 contentField.setWorkflowState (languageCodes, newWorkflowState, jParams, 1414 stateModifContext); 1415 } 1416 1417 } 1418 1419 1420 1434 public Map getFieldsLanguagesState (int pageID) 1435 throws JahiaException { 1436 1437 return f_utils.getFieldsLanguagesState(pageID); 1438 } 1439 1440 1449 public JahiaFieldDefinition loadFieldDefinition (int defID) 1450 throws JahiaException { 1451 return f_defs.db_load_field_definition (defID); 1452 } 1454 1467 public JahiaFieldDefinition loadFieldDefinition (int siteID, 1468 String definitionName) 1469 throws JahiaException { 1470 return f_defs.db_load_field_definition (siteID, definitionName); 1471 } 1473 1474 1481 public void saveFieldDefinition (JahiaFieldDefinition theDef) 1482 throws JahiaException { 1483 if (theDef.getID () == 0 || theDef.getID () == -1) { 1484 f_defs.db_create_field_definition (theDef); 1485 } else { 1486 f_defs.db_update_field_defprop (theDef); 1487 } 1488 } 1490 1495 public void deleteFieldDefinition (int fieldDefID) 1496 throws JahiaException { 1497 1498 logger.debug ("fieldDef=" + fieldDefID); 1499 JahiaFieldDefinitionsRegistry.getInstance ().removeFieldDefinition (fieldDefID); 1501 1502 f_defs.db_delete_field_definition (fieldDefID); 1504 } 1505 1506 1507 1512 protected JahiaTextFileService getTextFileService () { 1513 1514 return ServicesRegistry.getInstance ().getJahiaTextFileService (); 1515 } 1516 1517 private boolean logMe (JahiaField theField) { 1519 boolean out = false; 1520 int ctnid = theField.getctnid (); 1521 if (ctnid > 0) { 1522 ServicesRegistry sReg = ServicesRegistry.getInstance (); 1523 if (sReg != null) { 1524 JahiaContainersService ctnSrv = sReg.getJahiaContainersService (); 1525 if (ctnSrv != null) { 1526 try { 1527 JahiaContainer container = ctnSrv.loadContainerInfo (ctnid, 1528 EntryLoadRequest.STAGED); 1529 if (container != null) { 1530 String containerName = container.getDefinition ().getName (); 1531 if (!containerName.equals ("PortletList")) { 1532 out = true; 1533 } 1534 } 1535 } catch (JahiaException je) { 1536 } 1538 } 1539 } 1540 } 1541 return out; 1542 } 1543 1544 1550 public JahiaDOMObject getFieldsAsDOM (int siteID) throws JahiaException { 1551 1552 return fieldsDB.getFieldsAsDOM (siteID); 1553 1554 } 1555 1556 1557 1563 public JahiaDOMObject getFieldDefsAsDOM (int siteID) throws JahiaException { 1564 1565 return f_defs.getFieldDefsAsDOM (siteID); 1566 1567 } 1568 1569 1570 1576 public JahiaDOMObject getFieldDefPropsAsDOM (int siteID) throws JahiaException { 1577 1578 return f_defs.getFieldDefPropsAsDOM (siteID); 1579 1580 } 1581 1582 1589 public Vector getAclIDs (int siteID) 1590 throws JahiaException { 1591 return f_utils.db_get_all_acl_id (siteID); 1592 } 1593 1594 1595 1608 public JahiaField contentFieldToJahiaField ( 1609 ContentField contentField, 1610 EntryLoadRequest entryLoadRequest) 1611 throws JahiaException { 1612 1613 logger.debug ("Making JahiaField facade for field " + contentField.getID ()); 1614 1615 ContentObjectEntryState entryState = 1616 contentField.getEntryState (entryLoadRequest); 1617 1618 if (entryState == null) { 1619 logger.debug ( 1620 "Entry state " + entryLoadRequest + " not found for field " + contentField.getID () + ", returning null field !"); 1621 return null; 1622 } 1623 JahiaField jahiaField = createJahiaField ( 1624 contentField.getID (), 1625 contentField.getSiteID (), 1626 contentField.getPageID (), 1627 contentField.getContainerID (), 1628 contentField.getFieldDefID (), 1629 contentField.getType (), 1630 contentField.getConnectType (), 1631 contentField.getDBValue (entryState), 1632 0, contentField.getAclID (), 1634 entryState.getVersionID (), 1635 entryState.getWorkflowState (), 1636 entryState.getLanguageCode ()); 1637 1638 logger.debug ( 1639 "Returning JahiaField facade for field " + contentField.getID () + " using language code=" + jahiaField.getLanguageCode ()); 1640 1641 return jahiaField; 1642 } 1643 1644 public void serializeNonContainerFieldsToXML (XmlWriter xmlWriter, 1645 XMLSerializationOptions 1646 xmlSerializationOptions, 1647 int pageID, 1648 ParamBean paramBean) 1649 throws IOException { 1650 try { 1651 Vector fieldIDs = f_utils.db_get_only_active_non_container_field_ids_in_page ( 1652 pageID); 1653 1654 for (int i = 0; i < fieldIDs.size (); i++) { 1657 int id = ((Integer ) fieldIDs.elementAt (i)).intValue (); 1658 1659 ContentField theField = ContentField.getField (id); 1660 theField.serializeToXML (xmlWriter, xmlSerializationOptions, paramBean); 1661 } 1662 } catch (JahiaException je) { 1663 1664 } 1665 1666 } 1667 1668 1676 public String composeFieldDefDefaultValueFilePath (int siteID, String name) { 1677 StringBuffer buff = new StringBuffer (Jahia.getSettings ().getJahiaVarDiskPath ()); 1678 buff.append (File.separator); 1679 buff.append ("field_definitions"); 1680 buff.append (File.separator); 1681 buff.append (siteID); 1682 buff.append ("_"); 1683 buff.append (name); 1684 buff.append ("_defaultvalue.txt"); 1685 return buff.toString (); 1686 } 1687 1688 1693 public void invalidateCacheField (int fieldID) { 1694 ContentField.removeFromCache(fieldID); 1697 } 1698 1699 1705 public void allowFieldsPreloadingForPage(int pageId){ 1706 ContentFieldDB.getInstance().allowFieldsPreloadingForPage(pageId); 1707 } 1708 1709 private void createFileDefinitionRepository () { 1710 1711 StringBuffer buff = 1712 new StringBuffer (Jahia.getSettings ().getJahiaVarDiskPath ()); 1713 buff.append (File.separator); 1714 buff.append ("field_definitions"); 1715 File f = new File (buff.toString ()); 1716 f.mkdir (); 1717 } 1718 1719 private String checkFieldEnumerationValues (String fieldValue) { 1720 String value = fieldValue; 1721 if (fieldValue != null 1722 && (fieldValue.toLowerCase ().startsWith ("<jahia_multivalue"))) { 1723 value = ""; 1724 int pos = fieldValue.indexOf ("["); 1725 if (pos != -1) { 1726 int pos2 = fieldValue.indexOf ("]>"); 1727 if (pos2 != -1) { 1728 value = fieldValue.substring (pos2 + 2, fieldValue.length ()); 1729 } 1730 } 1731 } 1732 return value; 1733 } 1734} 1735 | Popular Tags |