cancel
Showing results for 
Search instead for 
Did you mean: 

help me. create content model by java!!!

linhnh
Champ in-the-making
Champ in-the-making
hi all. i'm new member. i've problem.plz help me
I want to create new content model by java. but i cant do it. and plz tell me about : how to create it and how to implement it ? and What IDE to create it is best.
sorry because my English not good.
thanks all.
6 REPLIES 6

dhalupa
Champ on-the-rise
Champ on-the-rise

linhnh
Champ in-the-making
Champ in-the-making
You might consider reading following resources

http://wiki.alfresco.com/wiki/Developer_Guide#Creating_new_Content_Models

http://ecmarchitect.com/images/articles/alfresco-content/content-article.pdf

Kind regards,

Denis
Thanks for your reply.
But I want to create content model by java not by xml. 
I can create content item by java and content model by xml but I can create content model by java then I want anyone tell me about it
thanks

davidc
Star Contributor
Star Contributor
If you must create a model via Java you can take the following approach…

Use the class org.alfresco.repo.dictionary.M2Model to create a model…

M2Model model = M2Model.createModel("test:mymodel");
model.createNamespace("mynamespace", "test");
model.createImport(NamespaceService.DICTIONARY_MODEL_1_0_URI, NamespaceService.DICTIONARY_MODEL_PREFIX);
model.createImport(NamespaceService.CONTENT_MODEL_1_0_URI, NamespaceService.CONTENT_MODEL_PREFIX);

You can also create a model from an existing XML file, allowing you to edit the model via code.

Then you can compile the model using…

dictionaryDAO.putModel(model);

where dictionaryDAO is a reference to bean id "dictionaryDAO".  This validates the model and makes it available for use by the other services.

linhnh
Champ in-the-making
Champ in-the-making
If you must create a model via Java you can take the following approach…

Use the class org.alfresco.repo.dictionary.M2Model to create a model…

M2Model model = M2Model.createModel("test:mymodel");
model.createNamespace("mynamespace", "test");
model.createImport(NamespaceService.DICTIONARY_MODEL_1_0_URI, NamespaceService.DICTIONARY_MODEL_PREFIX);
model.createImport(NamespaceService.CONTENT_MODEL_1_0_URI, NamespaceService.CONTENT_MODEL_PREFIX);

You can also create a model from an existing XML file, allowing you to edit the model via code.

Then you can compile the model using…

dictionaryDAO.putModel(model);

where dictionaryDAO is a reference to bean id "dictionaryDAO".  This validates the model and makes it available for use by the other services.

thanks for your reply.
But in my Source Alfresco download from internet haven't class org.alfresco.repo.dictionary.M2Model.   and I use AlfrescoCommunity-2.0.  Smiley Sad .
thanks

linhnh
Champ in-the-making
Champ in-the-making
If you must create a model via Java you can take the following approach…

Use the class org.alfresco.repo.dictionary.M2Model to create a model…

M2Model model = M2Model.createModel("test:mymodel");
model.createNamespace("mynamespace", "test");
model.createImport(NamespaceService.DICTIONARY_MODEL_1_0_URI, NamespaceService.DICTIONARY_MODEL_PREFIX);
model.createImport(NamespaceService.CONTENT_MODEL_1_0_URI, NamespaceService.CONTENT_MODEL_PREFIX);

You can also create a model from an existing XML file, allowing you to edit the model via code.

Then you can compile the model using…

dictionaryDAO.putModel(model);

where dictionaryDAO is a reference to bean id "dictionaryDAO".  This validates the model and makes it available for use by the other services.
If you must create a model via Java you can take the following approach…

Use the class org.alfresco.repo.dictionary.M2Model to create a model…

M2Model model = M2Model.createModel("test:mymodel");
model.createNamespace("mynamespace", "test");
model.createImport(NamespaceService.DICTIONARY_MODEL_1_0_URI, NamespaceService.DICTIONARY_MODEL_PREFIX);
model.createImport(NamespaceService.CONTENT_MODEL_1_0_URI, NamespaceService.CONTENT_MODEL_PREFIX);

You can also create a model from an existing XML file, allowing you to edit the model via code.

Then you can compile the model using…

dictionaryDAO.putModel(model);

where dictionaryDAO is a reference to bean id "dictionaryDAO".  This validates the model and makes it available for use by the other services.
thanks davidc
I create my model into FirstFoundationClient project. and mycode is

/*

*/
package org.alfresco.sample;

import java.io.Serializable;
import java.util.HashMap;
import java.util.Map;

import org.alfresco.model.ContentModel;
import org.alfresco.repo.content.MimetypeMap;
import org.alfresco.repo.transaction.TransactionUtil;
import org.alfresco.repo.transaction.TransactionUtil.TransactionWork;
import org.alfresco.service.ServiceRegistry;
import org.alfresco.service.cmr.repository.ChildAssociationRef;
import org.alfresco.service.cmr.repository.ContentService;
import org.alfresco.service.cmr.repository.ContentWriter;
import org.alfresco.service.cmr.repository.NodeRef;
import org.alfresco.service.cmr.repository.NodeService;
import org.alfresco.service.cmr.repository.StoreRef;
import org.alfresco.service.cmr.search.ResultSet;
import org.alfresco.service.cmr.search.SearchService;
import org.alfresco.service.cmr.security.AuthenticationService;
import org.alfresco.service.namespace.NamespaceService;
import org.alfresco.service.namespace.QName;
import org.alfresco.service.transaction.TransactionService;
import org.alfresco.util.ApplicationContextHelper;
import org.springframework.context.ApplicationContext;
import org.alfresco.repo.dictionary.M2Model;

public class FirstFoundationClient
{

    public static void main(String[] args)
    {
       System.out.print("hhhh");
        // initialise app content
        ApplicationContext ctx = ApplicationContextHelper.getApplicationContext();
        // get registry of services
        final ServiceRegistry serviceRegistry = (ServiceRegistry) ctx.getBean(ServiceRegistry.SERVICE_REGISTRY);
       
        // use TransactionWork to wrap service calls in a user transaction
        TransactionService transactionService = serviceRegistry.getTransactionService();
        TransactionWork<Object> exampleWork = new TransactionWork<Object>()
        {
            public Object doWork() throws Exception
            {
                doExample(serviceRegistry);
                doExample2(serviceRegistry);
                return null;
            }
        };
        TransactionUtil.executeInUserTransaction(transactionService, exampleWork);
        System.exit(0);
    }

   
    public static void doExample(ServiceRegistry serviceRegistry) throws Exception
    {
       //
        // authenticate
       //
        AuthenticationService authenticationService = serviceRegistry.getAuthenticationService();
        authenticationService.authenticate("admin", "admin".toCharArray());
       
        //
        // locate the company home node
        //
        SearchService searchService = serviceRegistry.getSearchService();
        StoreRef storeRef = new StoreRef(StoreRef.PROTOCOL_WORKSPACE, "SpacesStore");
        ResultSet resultSet = searchService.query(storeRef, SearchService.LANGUAGE_LUCENE, "PATH:"/app:company_home"");
        NodeRef companyHome = resultSet.getNodeRef(0);

        //
        // create new content node within company home
        //
       
        // assign name
        String name = "Foundation API sample (" + System.currentTimeMillis() + ")";
        Map<QName, Serializable> contentProps = new HashMap<QName, Serializable>();
        contentProps.put(ContentModel.PROP_NAME, name);

        // create content node
        NodeService nodeService = serviceRegistry.getNodeService();
        ChildAssociationRef association = nodeService.createNode(companyHome,
              ContentModel.PROP_NAME,
              QName.createQName(NamespaceService.CONTENT_MODEL_PREFIX, name),
                ContentModel.TYPE_CONTENT,
                contentProps);
        NodeRef content = association.getChildRef();
       
        // add titled aspect (for Web Client display)
        Map<QName, Serializable> titledProps = new HashMap<QName, Serializable>();
        titledProps.put(ContentModel.PROP_TITLE, name);
        titledProps.put(ContentModel.PROP_DESCRIPTION, name);
        nodeService.addAspect(content, ContentModel.ASPECT_TITLED, titledProps);
       
        //
        // write some content to new node
        //

        ContentService contentService = serviceRegistry.getContentService();
        ContentWriter writer = contentService.getWriter(content, ContentModel.PROP_CONTENT, true);
        writer.setMimetype(MimetypeMap.MIMETYPE_TEXT_PLAIN);
        writer.setEncoding("UTF-8");
        String text = "The quick brown fox jumps over the lazy dog";
        writer.putContent(text);
        System.out.print("Linh");
       
    }
    public static void doExample2(ServiceRegistry serviceRegistry) throws Exception
    {
       //
        // authenticate
       //
        AuthenticationService authenticationService = serviceRegistry.getAuthenticationService();
        authenticationService.authenticate("admin", "admin".toCharArray());
       
        //
        // locate the company home node
     
        //create new content model
        //
        M2Model model = M2Model.createModel("test:mymodel");
        model.createNamespace("mynamespace", "test");
        model.createImport(NamespaceService.DICTIONARY_MODEL_1_0_URI, NamespaceService.DICTIONARY_MODEL_PREFIX);
        model.createImport(NamespaceService.CONTENT_MODEL_1_0_URI, NamespaceService.CONTENT_MODEL_PREFIX);
        model.createPropertyType("haha:text");
        //model.setName("llll");
        System.out.print("linhhhh"+model.getName());
       
    }
   
   
}

it can create new content item in doexample but dont create new content model. it cannot display in webclient . where is wrong?how to correct it?

linhnh
Champ in-the-making
Champ in-the-making
thanks davidc.
Now I can create newmodel by java.
and how to import to Alfresco ?