Projet

General

Profil

ORM API

beCPG provides a powerful Alfresco Model to Java Pojo Mapping for manipulating beCPG entity.

It allows to map Java Pojo with Alfresco data models and use java api to manipulate these objects.
The mapping is based on Java 5 annotations.

There is a set of annotation that represents alfresco properties or associations

@AlfQname --> Specify the alfresco qName 
@AlfType --> Match to an alfresco type
@AlfProp --> Match to an alfresco properties
@AlfMlText -->  Math a special mltext properties

@AlfSingleAssoc --> Match to a single target association
@AlfMultiAssoc --> Match to a multiple target association
@AlfEnforced --> Save field even if null 
@AlfReadOnly --> Doesn't save the property

And a special set of annotations that represent beCPG datalists (will not be covered in details)

@DataList
@DataListIdentifierAttr
@DataListView
@MultiLevelDataList

Let's says you want to create and manipulate deliverable types objects.

First create your alfresco models :

        <type name="pjt:deliverable">
            <properties>
                <property name="pjt:dlDescription">
                    <type>d:text</type>
                    <mandatory>true</mandatory>
                </property>
                <property name="pjt:dlState">
                    <type>d:text</type>
                    <constraints>
                        <constraint ref="pjt:deliverableStates" />
                    </constraints>
                </property>
                <property name="pjt:dlUrl">
                    <type>d:text</type>
                </property>
            </properties>
            <associations>
                <association name="pjt:dlTask">
                    <source>
                        <mandatory>true</mandatory>
                        <many>true</many>
                    </source>
                    <target>
                        <class>pjt:taskList</class>
                        <mandatory>true</mandatory>
                        <many>false</many>
                    </target>
                </association>
                <association name="pjt:dlContent">
                    <source>
                        <mandatory>false</mandatory>
                        <many>true</many>
                    </source>
                    <target>
                        <class>cm:content</class>
                        <mandatory>false</mandatory>
                        <many>false</many>
                    </target>
                </association>
            </associations>
        </type>

Then create the corresponding Java Object with beCPG annotations

@AlfType
@AlfQname(qname = "pjt:deliverable")
public class Deliverable extends BeCPGDataObject {

    private NodeRef task;
    private DeliverableState state = DeliverableState.Planned;
    private String description;
    private String url;
    private Integer completionPercent = 0;
    private NodeRef content;

    @AlfSingleAssoc
    @AlfQname(qname = "pjt:dlTask")
    public NodeRef getTask() {
        return task;
    }

    @AlfProp
        @AlfQname(qname = "pjt:dlUrl")
    public String getUrl() {
         return url;
     }

      @AlfProp
    @AlfQname(qname = "pjt:dlState")
     public DeliverableState getState() {
         return state;
     }

    @AlfProp
    @AlfQname(qname = "pjt:dlDescription")
    public String getDescription() {
        return description;
    }

    @AlfProp
    @AlfQname(qname = "pjt:completionPercent")
    public Integer getCompletionPercent() {
        return completionPercent;
    }

    @AlfSingleAssoc
    @AlfQname(qname = "pjt:dlContent")
    public NodeRef getContent() {
        return content;
    }
   ...
}

Your class should implement BeCPGDataObject. Setters, hashCode, equals and toString are not represented but mandatory.

Then use AlfrescoRepository class to CRUD your new POJO. That's it.

public class TestORM {

    @Resource
    protected AlfrescoRepository<Deliverable> alfrescoRepository;

    @Test
    public void test() {
        //CRUD
        Deliverable deliverable = new Deliverable();
        deliverable.setParentNodeRef(getParentNodeRef());

        //Create
        alfrescoRepository.save(deliverable);

        NodeRef deliverableNodeRef = deliverable.getNodeRef();

        //Retrieve
        deliverable = alfrescoRepository.findOne(deliverableNodeRef);

        deliverable.setDescription("Test");
        //Update
        alfrescoRepository.save(deliverable);
    }

    ...
}