[opencms-dev] Create resource programatically

Alejandro Alves alejandro.alves at gmail.com
Sun Jan 27 19:52:35 CET 2008


Hi,
I am traying to create resources programatically, it works fine but I do not
know how to do it for nested content nor for attachments. Can anyone help
me??

This is my code:

    public boolean createResource(int type, String shema, String path,
HashMap simpleData, HashMap nestedData)
    {
        boolean success = false;
        CmsObject cmsObject = cmsJspActionElement.getCmsObject();
        boolean exists = cmsObject.existsResource(path);
        if(!exists)
        {
                //Crear carpeta con nombre de usuario y meter ahí el
contenido
            String resource = path + "/" + name;
              //out.println(mfolder);

            //CmsXmlContentDefinition getContentDefinition();!!!!!!!!!
            //CmsXmlNestedContentDefinition
          try
          {
              CmsXmlContentDefinition def =
CmsXmlContentDefinition.unmarshal(schema,new
CmsXmlEntityResolver(cmsObject));
              CmsXmlContent content =
CmsXmlContentFactory.createDocument(cmsObject,
cmsJspActionElement.getRequestContext().getLocale(), "UTF-8",def);

              Locale localizacion = cmsJspActionElement.getRequestContext
().getLocale();
              I_CmsXmlContentValue contentValue = null;
              getIncludes();
              Set keys = simpleData.keySet();
              Iteartor itKeys = keys.iterator();

              while (itKeys.hasNext())
              {
                  String key = (String)itKeys.next();
                  Object valor = simpleData.get(key);

                  if (valor instanceof String[])
                  {
                    //contentValue = content.getValue(key, localizacion );
                    //int count = contentValue.getMaxIndex();
                    String[] values = (String[]) valor;
                    for (int i=0; i<values.length; i++)
                    {
                        if(i==0)
                        {
                            contentValue =content.getValue(key,
localizacion);
                            contentValue.setStringValue(cmsObject,
values[0]);
                        }
                        else
                        {
                            contentValue =content.addValue(cmsObject, key,
localizacion, i);
                              contentValue.setStringValue(cmsObject,
values[i]);

                        }
                    }
                  }
                  else
                  {
                      contentValue = content.getValue(key, localizacion );
                      contentValue.setStringValue(cmsObject, (String)valor);
                  }
              }

              keys = nestedData.keySet();
              itKeys = keys.iterator();

              while(itKeys.hasNext())
              {
                  String key = (String)itKeys.next();
                  String[] valor = simpleData.get(key);
                  CmsXmlContentValueSequence nestedSequence =
content.getValueSequence(key, localizacion);
                    int count = nestedSequence.getElementCount();
                    for (int i=0; valor.length; i++)
                    {
                      I_CmsXmlContentValue newValue =
nestedSequence.addValue(cmsObject, i);
                      newValue.setStringValue(cmsObject, "assad");
                    }

              }



                byte[] byteContent= content.marshal();
              cmsObject.createResource(path, type, byteContent, new
ArrayList(););
              cmsObject.unlockResource(path);
              success=true;
          }
          catch(Exception exc)
          {
              exc.printStackTrace();
              success=false;
          }
        }
        return success;
    }
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://webmail.opencms.org/pipermail/opencms-dev/attachments/20080127/0e6a328f/attachment.htm>


More information about the opencms-dev mailing list