I am working on a JSP to import data into a page on a Jahia 4.1 server.
At this point, it is a proof of concept, just inserting a single
container into a list on an existing page. I have considerable problems
inserting a container programmatically.

After an attempt to fill more fields in my script, I get an
ArrayIndexOutOfBounds exception whenever I try to edit a container
inserted by my code (exception is included at the bottom of this
message).

This is the container definition of the page I am importing to:

<content:declareContainerList name='sbcList'
titleKey="com.loqutus.mfce.jahia.contentContainerListName"
        title="Bulletins List" bundleKey="<%=portalResourceBundleID%>"
>
    <content:declareContainer>
        <content:declareField name='sbcGroup'       title="Group"
         type="SharedSmallText" value="<%=groupValues.toString()%>" />
        <content:declareField name='sbcFile'        title="File"
         type="File" />
        <content:declareField name='sbcPages'       title="nrOfPages"
         type="Integer" />
        <content:declareField name='sbcDate'        title="date"
         type="Date" value='<jahia_calendar[mon yyyy]>' />
        <content:declareField name='sbcModels'      title="models"
         type="SharedSmallText" />
        <content:declareField name='sbcSubject'     title="subject"
         type="SmallText" />
    </content:declareContainer>
</content:declareContainerList>

I started with the following code, which worked just fine at first: I
could edit a container inserted by the code (most fields were null, but
that was because I had not added code to set those fields yet):

--- CODE BEGINS ---
try {
        int pageId = 231; //kludge, will do something smarter here later
        out.print("Starting import...<BR>");
        ServicesRegistry registry = ServicesRegistry.getInstance();
        JahiaContainerList itemsContainerList = 
         jData.containers().getAbsoluteContainerList("sbcList",pageId);
      JahiaVersionService verService =
registry.getJahiaVersionService();

      if (itemsContainerList != null) {
          out.print("Let's put something in!<br>");
          JahiaContainer container = new JahiaContainer(
            0, jData.params().getJahiaID(), jData.params().getPageID(),
            itemsContainerList.getID(), 0,
itemsContainerList.getAclID(),
            itemsContainerList.getctndefid(),
            verService.getCurrentVersionID(),
            EntryLoadRequest.STAGING_WORKFLOW_STATE);

          registry.getJahiaContainersService().saveContainer(container,
0,
            jData.params()); 

          JahiaContainerDefinition ctnDef = container.getDefinition();
          JahiaPage targetPage = registry.getJahiaPageService()
                                 .lookupPage(pageId, jParams);
          int templateId =  targetPage.getPageTemplateID();
          JahiaFieldDefinition fieldDef = ctnDef.findFieldInStructure( 
            "sbcGroup",  templateId);
          
          ContentSmallTextSharedLangField contentSmallTextField =
            ContentSmallTextSharedLangField.createSmallText(
            jParams.getSiteID(), pageId, container.getID(),
            fieldDef.getID(), container.getAclID(), 0, "new", jParams);

          JahiaFieldDefinition fileFieldDef =
ctnDef.findFieldInStructure(  
            "sbcFile",  templateId);
          DAVFileAccess acc = new DAVFileAccess(jParams.getSite(),  
            "/shared/servicebulletins/test.PDF");

          out.print("Got accessor" + acc);
          JahiaFileField myFileField = acc.getJahiaFileField();
          ContentFileField.createFile(jParams.getSiteID(), pageId,
            container.getID(), fileFieldDef.getID(),
container.getAclID(),
            0, myFileField, jParams);
      }
} catch (Throwable t) {
out.print("oops:" + t.getMessage());
}
--- CODE ENDS ---

The problems started after adding a field to the container definition (a
BigText field) but I have also removed that field.

I undid changes in the script that inserts the container. I had added
some code to assign values to additional fields. Still, containers added
by my original script cannot be edited anymore, even though the exact
same version of the script used to work right before.

Another (probably unrelated) is that ContentDateField.createDate does
not seem to work at all (generates a class cast exception).

Finally, I have seen some examples where people simply use
container.getField("fieldname") to access the fields after adding a new
container, but getField() keeps returning null in my program...

WHAT IS THE CORRECT WAY TO INSERT A CONTAINER INTO A CONTAINERLIST
PROGRAMMATICALLY???

------------------------------------------------------------------------
-
------ Included stack trace (seen when trying to edit a container)
------
------------------------------------------------------------------------
-
Exception StackTrace: Cause level : 0 (level 0 is the most precise
exception)
java.lang.ArrayIndexOutOfBoundsException: Array index out of range: 0
        at java.util.Vector.get(Vector.java(Compiled Code))
        at
org.jahia.engines.updatecontainer.UpdateContainer_Engine.processCurrentS
creen(UpdateContainer_Engine.java:357)
        at
org.jahia.engines.updatecontainer.UpdateContainer_Engine.handleActions(U
pdateContainer_Engine.java:198)
        at
org.jahia.operations.OperationManager.handleOperations(OperationManager.
java:311)
        at org.jahia.bin.JahiaAction.execute(JahiaAction.java:50)
        at
org.apache.struts.action.RequestProcessor.processActionPerform(RequestPr
ocessor.java:421)
        at
org.apache.struts.action.RequestProcessor.process(RequestProcessor.java:
226)
        at
org.apache.struts.action.ActionServlet.process(ActionServlet.java:1164)
        at org.jahia.bin.Jahia.process(Jahia.java:1539)
        at org.jahia.bin.Jahia.service(Jahia.java:854)

Reply via email to