📄 jahiacontainerset.java
字号:
//// ____.// __/\ ______| |__/\. _______// __ .____| | \ | +----+ \// _______| /--| | | - \ _ | : - \_________// \\______: :---| : : | : | \________>// |__\---\_____________:______: :____|____:_____\// /_____|//// . . . i n j a h i a w e t r u s t . . .////// JahiaContainerSet// EV 01.01.2001 Happy New Year, Folks !//// addContainerList// declareField// declareContainer// getContainer( containerName )// getContainer( containerName, index )// getContainer( ctnid )// getContainerList( containerName )// getContainerList( containerListID )//package org.jahia.data.containers;import java.util.*; // HashTableimport javax.servlet.*; // ServletContextimport javax.servlet.http.*; // HttpServletRequestimport org.jahia.utils.*; // JahiaConsoleimport org.jahia.data.*; // JahiaDataimport org.jahia.data.fields.*; // LoadFlagsimport org.jahia.registries.*; // ContainersDefinitionsRegistryimport org.jahia.exceptions.JahiaException;import org.jahia.content.*;import org.jahia.params.*;import org.jahia.services.search.ContainerSearcher;import org.jahia.services.search.JahiaSearchConstant;import org.jahia.data.search.JahiaSearchResult;import org.jahia.services.containers.JahiaContainersLogic;import java.lang.Runtime;public class JahiaContainerSet{ private static final String CLASS_NAME = JahiaContainerSet.class.getName(); private JahiaData jData; private JahiaContainersLogic c_logic; private boolean isStructureAlreadyBuild = false; // by default the container set is loaded with the container lists but without their containers. /** * @associates JahiaContainerList */ private Hashtable containerLists; /** * @associates String */ private Set declaredContainers; // list of per-request declared containers /** * @associates String */ private Set declaredFields; /** * Optional cached containers fields */ private Hashtable cachedFieldsFromContainers; // vector of container list loaded wihtout their containers ( needed to build a container set with container list loaded without their containers ) private Vector unstructuredContainerLists; //------------------------------------------------------------------------- /*** * constructor * * @param jData JahiaData * */ public JahiaContainerSet( JahiaData jData ) { this.jData = jData; this.containerLists = new Hashtable(); this.declaredContainers = new HashSet(); this.declaredFields = new HashSet(); this.cachedFieldsFromContainers = new Hashtable(); this.c_logic = new JahiaContainersLogic(); } // end constructor //------------------------------------------------------------------------- /*** * Use this constructor to instantiate a container set with a vector * of container list info bean ( container list loaded without their containers ). * * @param jData JahiaData * @param Hashtable cachedFieldsFromContainers * @param Vector vector of container lists loaded without containers */ public JahiaContainerSet( JahiaData jData , Hashtable cachedFieldsFromContainers, Vector ctnLists) { this.jData = jData; this.containerLists = new Hashtable(); this.declaredContainers = new HashSet(); this.declaredFields = new HashSet(); this.c_logic = new JahiaContainersLogic(); if ( cachedFieldsFromContainers != null ){ this.cachedFieldsFromContainers = cachedFieldsFromContainers; } this.unstructuredContainerLists = ctnLists; } // end constructor //------------------------------------------------------------------------- /*** * adds a container list to the set. * * @param aContainerList the JahiaContainerList to add * @see org.jahia.data.containers.JahiaContainerList * */ public synchronized void addContainerList( JahiaContainerList aContainerList ) throws JahiaException { containerLists.put( aContainerList.getDefinition().getName(), aContainerList ); } // end addContainerList //------------------------------------------------------------------------- /** * Check and Build the container list structure dependencies if not done. * @Author NK */ public synchronized void checkContainerStructure() throws JahiaException { if ( !this.isStructureAlreadyBuild ){ if ( this.unstructuredContainerLists == null ) { JahiaConsole.println(CLASS_NAME+".checkContainerStructure","List of unstructured container list is null !!!!!"); } else { Vector v = new Vector(); Enumeration enum = this.unstructuredContainerLists.elements(); while ( enum.hasMoreElements() ){ JahiaContainerList cList = ( JahiaContainerList) enum.nextElement(); if ( !cList.isContainersLoaded() ){ // load the the containers for this one // apply containers search and filtering. try { Vector ctnids = doContainerFilterSearchSort(jData.params(),cList.getDefinition().getName(),cList.getID()); if ( ctnids != null ){ cList = ServicesRegistry.getInstance().getJahiaContainersService() .loadContainerList( cList.getID(), ctnids, LoadFlags.ALL, jData.params(), this.cachedFieldsFromContainers); } else { cList = ServicesRegistry.getInstance().getJahiaContainersService() .loadContainerList( cList.getID(), LoadFlags.ALL, jData.params(), this.cachedFieldsFromContainers); } } catch ( Throwable t ){ t.printStackTrace(); } } if ( cList != null ){ v.add(cList); } } // sorts logical tree c_logic.logic_sort_container_tree( v ); this.containerLists = new Hashtable(); for (int i=0; i < v.size(); i++) { this.addContainerList( (JahiaContainerList) v.elementAt(i) ); } } } this.isStructureAlreadyBuild = true; } //------------------------------------------------------------------------- /*** * enables a jahia template programmer to declare a field in a container * * @param fieldName the field name * @param fieldTitle the field title * @param fieldType the field tpe * @param defaultValue the default field value * * @exception raises a critical JahiaException if the container has been already declared * @exception raises a critical JahiaException if one of the containerFields have not been declared * @exception raises a critical JahiaException if one of the fields has the same name as the container * */ public void declareField( String fieldName, String fieldTitle, int fieldType, String defaultValue ) throws JahiaException { // ensure that the data provided by the user has no special chars in it fieldName = FormDataManager.getInstance().encode( fieldName ); fieldTitle = FormDataManager.getInstance().encode( fieldTitle ); defaultValue = FormDataManager.getInstance().encode( defaultValue ); // we check if a field with the same name was not already declared, of if the field has no // empty name or title boolean isDeclaredField = checkDeclaredField(fieldName); boolean isJahiaDataDeclaredField = jData.fields().checkDeclared(fieldName); boolean isDeclaredContainer = checkDeclared(fieldName); if ((!isDeclaredField) && (!isJahiaDataDeclaredField) && (!fieldName.equals("")) && (!fieldTitle.equals("")) && (!isDeclaredContainer) ) { // first, let's check to see if the declared field has already a field definition in the // FieldsDefinitionsRegistry (which means also in the jahia_fields_def table) JahiaFieldDefinition aDef = JahiaFieldDefinitionsRegistry.getInstance().getDefinition( jData.params().getSiteID(), fieldName ); int pageDefID = jData.params().getPage().getPageTemplateID(); if (aDef != null) { // okay, it seems the definition already exists. // now has it the same data than in the database (title, type, defaultval) ? if (!((aDef.getTitle(pageDefID).equals(fieldTitle)) && (aDef.getType(pageDefID) == fieldType) && (aDef.getDefaultValue(pageDefID).equals(defaultValue)))) { // well, data is not the same in the registry and in the declare() method ! // this means the user has changed the field declaration in the template... // before doing anything else, we have to be sure that the user didn't change the // field data type, because this could crash the whole application ! if ((aDef.getType(pageDefID) != fieldType) && (aDef.getType(pageDefID) != -1)) { // okay, this user think he's smart : he tried to change the data type of // fields already existing... // let's send him a Jahia error to learn him a little lesson String errorMsg = "Cannot change field type because field data already exists : " + fieldName; JahiaConsole.println( "JahiaContainerSet.declareField", errorMsg + " -> BAILING OUT" ); throw new JahiaException( errorMsg, errorMsg, JahiaException.TEMPLATE_ERROR, JahiaException.CRITICAL ); } else { // okay, no alert ahead, the type hasn't been modified // let's synchronize the data between : // - the template declaration (file) // - the database declaration (database) // - the registry declaration (memory) // the synchronizeData method handles all this for us... pfew :) JahiaConsole.println("JahiaContainerSet.declareField","Setting data for pageDef " + pageDefID ); aDef.setType( fieldType, pageDefID ); aDef.setTitle( fieldTitle, pageDefID ); aDef.setDefaultValue( defaultValue, pageDefID ); JahiaFieldDefinitionsRegistry.getInstance().setDefinition( aDef ); } } } else { // hell, the definition doesn't exist in the memory ! // this can mean two things : // - either this is the first time Jahia encounters this template // - or the database data was screwed // in any case, we got to : // - add it into the registry (memory) // - add it into the database (database) // - change the values in jData (memory) Hashtable subDefs = new Hashtable(); subDefs.put( new Integer(pageDefID), new JahiaFieldSubDefinition( 0, 0, pageDefID, fieldTitle, fieldType, defaultValue ) ); aDef = new JahiaFieldDefinition( 0, jData.params().getPage().getJahiaID(), fieldName, subDefs ); JahiaFieldDefinitionsRegistry.getInstance().setDefinition( aDef ); } declaredFields.add( fieldName ); } else { // the field is already declared, or has a null name/title : let the user have it... ;) String errorMsg = ""; if ("".equals(fieldName)) { errorMsg = " Field has null name : " + fieldTitle; } if ("".equals(fieldTitle)) { errorMsg += " Field has null title : " + fieldName; } if (isDeclaredField) { errorMsg += " Field already declared : " + fieldName; } if (isJahiaDataDeclaredField) { errorMsg += " Field already declared in JahiaData field set : " + fieldName; } if (isDeclaredContainer) { errorMsg += " Field name already used by a container : " + fieldName; } // JahiaConsole.println( "JahiaContainerSet.declareField", errorMsg + " -> BAILING OUT" ); throw new JahiaException( errorMsg, errorMsg, JahiaException.TEMPLATE_ERROR, JahiaException.CRITICAL ); } } // end declareField //------------------------------------------------------------------------- /*** * enables a jahia template programmer to declare a container * * @param containerName the container name * @param containerTitle the container title * @param containerFields the fields (or containers) in the container * * @exception raises a critical JahiaException if the container has been already declared * @exception raises a critical JahiaException if one of the containerFields have not been declared * @exception raises a critical JahiaException if one of the fields has the same name as the container * * In order to let a container include itself, the keyword "_self" in the containeFields must be used. * */ public void declareContainer( String containerName, String containerTitle, Vector containerFields ) throws JahiaException { declareContainer(containerName, containerTitle, containerFields, -1, -1); } // end declareContainer /** * enables a jahia template programmer to declare a container, specifying * scrollable window size and offset for container lists that will grow very * large in size. * * @param containerName the container name * @param containerTitle the container title * @param containerFields the fields (or containers) in the container
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -