[opencms-dev] Create a sibling dynamically

Mario Jäger m.jaeger at alkacon.com
Tue Jul 26 08:33:25 CEST 2011


Hi,

The <copy-resources> node in the opencms-modules.xml is currently 
provided for that, to copy an already existing resource to a configured 
target. With the following example configuration the file 
/system/modules/mymodule/pages/index.html is copied as sibling in that 
folder, where a file from a special resource type is new created:

...
<resourcetypes>
   ...
   <copy-resources>
     <copy-resource source="/system/modules/mymodule/pages/index.html" 
target="${resource.folder.path}" type="sibling"/>
     ...
   </copy-resources>
   ...
</resourcetypes>
...

But, that does not match your requirement. Because, you want to have the 
currently new created file as sibling in another site. That is why, you 
have to create a new class. I have done that for an arbitrary resource 
type. Therefore, I have created the class MyResourceTypeXmlContent, 
which extends the class CmsResourceTypeXmlContent and which overwrites 
the method createResource() in the following way (Please note, that I 
have hard coded, that the sibling is always created in the other site 
"mysite" as test.html. You still have to adapt, that the sibling in the 
other site is in the same path like in the default site and that the 
sibling has the same name like the new created file):

public class MyResourceTypeXmlContent extends CmsResourceTypeXmlContent {

     /**
      * @see 
org.opencms.file.types.I_CmsResourceType#createResource(org.opencms.file.CmsObject, 
org.opencms.db.CmsSecurityManager, java.lang.String, byte[], java.util.List)
      */
     @Override
     public CmsResource createResource(
         CmsObject cms,
         CmsSecurityManager securityManager,
         String resourcename,
         byte[] content,
         List properties) throws CmsException {

         // now create the resource using the super class
         CmsResource resource = super.createResource(cms, 
securityManager, resourcename, content, properties);

         // get an CmsObject to have access to the other site to create 
the sibling
         CmsObject cmsCopy = OpenCms.initCmsObject(cms);
         cmsCopy.getRequestContext().setSiteRoot("/");

         // create the sibling
         // TODO: the target path is still to adapt!
         String target = "/sites/mysite/test.html";
         try {
             // copy the resource
             cmsCopy.copyResource(resource.getRootPath(), target, 
CmsResource.COPY_AS_SIBLING);
         } catch (Exception e) {
             // could not copy the resource
         }

         return resource;
     }
}


I have configured that class in the opencms-modules.xml for the special 
resource type then:

...
<resourcetypes>
   <type class="mymodule.MyResourceTypeXmlContent" 
name="specialResourceType" id="9999">
...

Then, the file /sites/mysite/test.html is created automatically as 
sibling from a new file from the special resource type.

Kind Regards,
Mario.



Am 25.07.2011 16:10, schrieb R. Jonyo:
>
>
> Thanks Mario. I intend to automatically create sibling only for a special
> resource type. Is it simpler to use<copy-resources>  on opencms-modules.xml?
> if not then i guess i have to create a new class. Also If i have to use
> <copy-resources>  what is the xml syntax.
>
>
>
> regards,
> R.Jonyo
>
>
>
>
>
> Mario Jäger wrote:
>>
>> Hi,
>>
>> You do not write if you want to have the sibling for all resource types
>> or for a special resource type only.
>>
>> If you only want to have it for a special resource type and that is a
>> xml content for example, then you could create a new class which extends
>> the class org.opencms.file.types.CmsResourceTypeXmlContent and which
>> overrides the method createResource(). In that method you could
>> implement that aditional to the new resource automatically the sibling
>> in the other site path is created. That new class is to configure in the
>> WEB-INF/config/opencms-modules.xml in the "class" attribute in the
>> <resourcetypes><type>  node from the special resource type. The new class
>> should be in your own module.
>>
>> There is a possibility already to configure that other resources are
>> created automatically when a new resource is created with the
>> <copy-resources>  node in the opencms-modules.xml. But, you can not use
>> that configuration element for your requirement without to make
>> enhancements on that feature. I would advice to use that possibility
>> only if you want to have siblings for all resource types. Because, I
>> think it is more complicated to enhance that configuration element to
>> your requirement without to touch the OpenCms core.
>>
>> Kind Regards,
>> Mario.
>>
>>
>>
>> Am 21.07.2011 10:23, schrieb R. Jonyo:
>>>
>>> Hi,
>>>
>>> Anyone who has code to create a sibling dynamically?
>>> e.g. i manually create a resource on "/sites/default/test.html" and a
>>> sibling is automatically created on " /sites/mysite/test.html". I guess
>>> on
>>> has to use ModuleAction but i do not have the right code. Please help!
>>>
>>>
>>>
>>> regards,
>>> R.Jonyo
>>
>>
>> --
>> -------------------
>> Mario Jäger
>>
>> Alkacon Software GmbH  - The OpenCms Experts
>> http://www.alkacon.com - http://www.opencms.org
>>
>> _______________________________________________
>> This mail is sent to you from the opencms-dev mailing list
>> To change your list options, or to unsubscribe from the list, please visit
>> http://lists.opencms.org/mailman/listinfo/opencms-dev
>>
>>
>


-- 
-------------------
Mario Jäger

Alkacon Software GmbH  - The OpenCms Experts
http://www.alkacon.com - http://www.opencms.org



More information about the opencms-dev mailing list