cancel
Showing results for 
Search instead for 
Did you mean: 

how to create custom content model using JCR?

naveenkumar
Champ in-the-making
Champ in-the-making
Hi, I am trying to create custom content using JCR. I created a new custom tag for content and modifies some xmls. but unable to create content using JCR. Please if anyone can help me…

this is my code………..in the code - "dml is my custom tag"

Node rootNode = session.getRootNode();
           Node companyHome = rootNode.getNode("app:company_home");
   
           // create the content node
           String name = "JCR sample (" + System.currentTimeMillis() + ")";
           Node content = companyHome.addNode("dml:" + name, "dml:newscontent");
          // Node content = companyHome.addNode("cm:" + name, "cm:content");
           content.setProperty("cm:name", name);

           // add titled aspect (for Web Client display)
           //content.addMixin("cm:titled");
          // content.setProperty("cm:title", name);
           //content.setProperty("cm:description", name);
           content.setProperty("dml:Headline", name);

           //
           // write some content to new node
           //
          //content.setProperty("cm:content", "The quick brown fox jumps over the lazy dog");
          
           //
           // To set the content mime type, we need to use an Alfresco native service
           // as there isn't an equivalent call in JCR
           //
           setMimeType(context, content, MimetypeMap.MIMETYPE_TEXT_PLAIN);
2 REPLIES 2

davidc
Star Contributor
Star Contributor
Can you provide more info?

What error are you getting?
What's your model?
What bits of xml did you modify?

naveenkumar
Champ in-the-making
Champ in-the-making
Runs fine but content is not seen in the webclient…check my console..

18:01:41,765 ERROR [transform.magick.AbstractImageMagickContentTransformer] ImageMagickContentTransformer not available: Failed to perform ImageMagick transformation: 
Execution result:
   os:         Windows XP
   command:    imconvert "C:\DOCUME~1\139872\LOCALS~1\Temp\Alfresco\ImageMagickContentTransformer_init_source_9586.gif"  "C:\DOCUME~1\139872\LOCALS~1\Temp\Alfresco\ImageMagickContentTransformer_init_target_9587.png"
   succeeded:  false
   exit code:  1
   out:       
   err:        CreateProcess: imconvert "C:\DOCUME~1\139872\LOCALS~1\Temp\Alfresco\ImageMagickContentTransformer_init_source_9586.gif" "C:\DOCUME~1\139872\LOCALS~1\Temp\Alfresco\ImageMagickContentTransformer_init_target_9587.png" error=2
18:01:45,140 WARN  [remoting.rmi.RmiRegistryFactoryBean] Could not detect RMI registry - creating new one
18:01:47,546 WARN  [alfresco.util.OpenOfficeConnectionTester] A connection to OpenOffice could not be established.
18:01:56,328 INFO  [domain.schema.SchemaBootstrap] No changes were made to the schema.
18:01:58,593 INFO  [repo.admin.ConfigurationChecker] The Alfresco root data directory ('dir.root') is: C:\Alfresco2.1\alf_data
18:01:58,781 INFO  [admin.patch.PatchExecuter] Checking for patches to apply …
18:01:59,015 INFO  [repo.module.ModuleServiceImpl] Found 0 module(s).
18:01:59,500 INFO  [service.descriptor.DescriptorService] Alfresco JVM - v1.5.0_11-b03; maximum heap size 63.563MB
18:01:59,500 WARN  [service.descriptor.DescriptorService] Alfresco JVM - WARNING - maximum heap size 63.563MB is less than recommended 512MB
18:01:59,500 INFO  [service.descriptor.DescriptorService] Alfresco started (Community Network): Current version 2.1.0 (R1 444) schema 62 - Installed version 2.1.0 (R1 443) schema 62



Here is my class:

package org.alfresco.sample;

import javax.jcr.Node;
import javax.jcr.Repository;
import javax.jcr.RepositoryException;
import javax.jcr.Session;
import javax.jcr.SimpleCredentials;

import org.alfresco.jcr.api.JCRNodeRef;
import org.alfresco.model.ContentModel;
import org.alfresco.repo.content.MimetypeMap;
import org.alfresco.service.ServiceRegistry;
import org.alfresco.service.cmr.repository.ContentData;
import org.alfresco.service.cmr.repository.NodeRef;
import org.alfresco.service.cmr.repository.NodeService;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;



/**
* Simple client example demonstrating the use of the Alfresco JCR (JSR-170) API.
*
* The client creates a content node in the "Company Home" folder.  The content
* may be viewed and operated on within the Alfresco Web Client.  Note: the web client
* will need to be re-started after executing this sample to see the changes in
* effect.
*
* This client demonstrates the "Embedded Repository" deployment option as described
* in the Alfresco Respotiory Architecture docucment -
* http://wiki.alfresco.com/wiki/Alfresco_Repository_Architecture
*/
public class FirstJCRClient
{
   
    public static void main(String[] args)
       throws Exception
   {
       // access the Alfresco JCR Repository (here it's via programmatic approach, but it could also be injected)
       ApplicationContext context = new ClassPathXmlApplicationContext("classpath:alfresco/application-context.xml");
       Repository repository = (Repository)context.getBean("JCR.Repository");
   
       // login to workspace (here we rely on the default workspace defined by JCR.Repository bean)
       Session session = repository.login(new SimpleCredentials("admin", "admin".toCharArray()));
   
       try
       {
           // first, access the company home
           Node rootNode = session.getRootNode();
           Node companyHome = rootNode.getNode("app:company_home");
   
           // create the content node
           String name = "JCR sample (" + System.currentTimeMillis() + ")";
           Node content = companyHome.addNode("dml:" + name, "dml:newscontent");
           content.setProperty("cm:name", name);

           // add titled aspect (for Web Client display)
           content.addMixin("cm:titled");
           content.setProperty("cm:title", name);
           content.setProperty("cm:description", name);
           content.setProperty("dml:PublicIdentifier", name);
           //
           // write some content to new node
           //
           content.setProperty("cm:content", "The quick brown fox jumps over the lazy dog");
          
           //
           // To set the content mime type, we need to use an Alfresco native service
           // as there isn't an equivalent call in JCR
           //
           setMimeType(context, content, MimetypeMap.MIMETYPE_TEXT_PLAIN);

           // save changes
           session.save();
       }
       finally
       {
           session.logout();
           System.exit(0);
       }
   }   

   
    /**
     * Demonstrates the mixed use of JCR API calls and Alfresco Foundation API calls.
     *
     * Here, the Foundation API is used to set the mimetype of the content.
     *
     * @param context  application context
     * @param node   the JCR Node to adjust
     * @param mimeType  the mimetype to set
     * @throws RepositoryException
     */
   private static void setMimeType(ApplicationContext context, Node node, String mimeType)
       throws RepositoryException
   {
      // retrieve service registry
        ServiceRegistry serviceRegistry = (ServiceRegistry) context.getBean(ServiceRegistry.SERVICE_REGISTRY);
        NodeService nodeService = serviceRegistry.getNodeService();
      
       // convert the JCR Node to an Alfresco Node Reference
       NodeRef nodeRef = JCRNodeRef.getNodeRef(node);
   
       // retrieve the Content Property (represented as a ContentData object in Alfresco)
       ContentData content = (ContentData)nodeService.getProperty(nodeRef, ContentModel.PROP_CONTENT);
      
       // update the Mimetype
       content = ContentData.setMimetype(content, mimeType);
       nodeService.setProperty(nodeRef, ContentModel.PROP_CONTENT, content);
   }

}


Check my custom model::

<?xml version="1.0" encoding="UTF-8"?> 

<!– Definition of new Model –>

<!– The important part here is the name - Note: the use of the my: namespace
which is defined further on in the document –>
<model name="dml:content" xmlns="http://www.alfresco.org/model/dictionary/1.0">

<!– Optional meta-data about the model –>
   <description>Modele NewsML</description>
   <author>Christophe</author>
   <version>0.1</version>

<!– Imports are required to allow references to definitions in other models –>
   <imports>
<!– Import Alfresco Dictionary Definitions –>
   <import uri="http://www.alfresco.org/model/dictionary/1.0" prefix="d"/>
<!– Import Alfresco Content Domain Model Definitions –>
   <import uri="http://www.alfresco.org/model/content/1.0" prefix="cm"/>
</imports>

<!– Introduction of new namespaces defined by this model –>
<!– NOTE: The following namespace my.new.model should be changed to reflect your own namespace –>
   <namespaces>
   <namespace uri="com.company.dml.model" prefix="dml"/>
  </namespaces>
<types>
<type name="dml:newscontent">
         <title>Newscontent</title>
         <parent>cm:content</parent>
         <properties>
            <property name="dml:ProviderId">
               <type>d:text</type>
            </property>
            <property name="dml:DateId">
               <type>d:long</type>
            </property>
            <property name="dml:NewsItemId">
               <type>d:long</type>
            </property>
            <property name="dml:RevisionId">
               <type>d:long</type>
            </property>
            <property name="dml:PublicIdentifier">
               <type>d:text</type>
            </property>
           </properties>
      </type>

</types></model>

I have doubt about namespace uri.. is that correct???

Here is my example-model-context.xml…

<?xml version='1.0' encoding='UTF-8'?> 
<!DOCTYPE beans PUBLIC '-//SPRING//DTD BEAN//EN' 'http://www.springframework.org/dtd/spring-beans.dtd'>

<beans>

<!– Registration of new models –>
<bean id="extension.dictionaryBootstrap" parent="dictionaryModelBootstrap" depends-on="dictionaryBootstrap">
<property name="models">
<list>
<value>alfresco/extension/newsModel.xml</value>
</list>
</property>
</bean>

</beans>


For your information i am able to create content using custom model through web client… i am able to see the meta properties…. when i try to use FirstJcrClient not able to see…

Please help me David…

Thanks
Naveen