How to implement a DAO using the Imixs EntityService

With help from the Imixs Workflow project you can implement a simple data access object with a minimal effort. The open source project provides a EJB service class named EntityService to store data based on a generic data object.

Before your data can be managed by the EntityService you need to create a simple data object – called ItemCollection. A ItemCollection contains a list of items that can store any type of data. There are no restrictions in the amount or size of the data stored into an ItemCollection. This is a example how to use the ItemCollection object:

  import org.imixs.workflow.ItemCollection;
  .....
  ItemCollection workitem=new ItemCollection();
  workitem.replaceItemValue("name","Anna");
  workitem.replaceItemValue("age",new Integer(32));

Each item stored into a ItemCollection has a item-name and a item-value. The item-name identifies a specific part of data contained in a ItemCollection. You can access a item-value by its name:

  ....
  String name=workitem.getItemValueString("name");
  int age=workitem.getItemValueInteger("age");
  ....

To store a ItemColleciton using the EntityService is symply by calling the method save(); This method returns an updats ItemCollection cotaning the property ‘$uniqueID’ which identifies the data object and the property ‘type’ which is used to categorize different kind of data objects.

This is an example of an implementation in a JSF ManagedBean class.

@javax.inject.Named("teamController")
@javax.enterprise.context.SessionScoped
public class TeamController implements Serializable {
 ....
 @EJB
 private org.imixs.workflow.jee.ejb.EntityService entityService;

 ....
    public String createAction(String action) {
        workitemItemCollection = new ItemCollection();
        FacesContext context = FacesContext.getCurrentInstance();
        ExternalContext externalContext = context.getExternalContext();
        String sUser = externalContext.getRemoteUser();
        workitemItemCollection.replaceItemValue("namCreator", sUser);
        workitemItemCollection.replaceItemValue("$WriteAccess", sUser);
        workitemItemCollection.replaceItemValue("type", "team");
        return action;
    }

    public String saveAction(String action) throws AccessDeniedException {
        // save workitem now...
        workitemItemCollection = entityService.save(workitemItemCollection);
        return action;
    }
....
}

Note that in this example the property ‘type’ is set to the value ‘team’ to categorize the dataobjects. Additional the property ‘$writeAccess’ is set to the current users name to grant access to the new data object.
To get a list of all cWith help from the Imixs Workflow project you can implement a simple data access object with a minimal effort. The open source project provides a EJB service class named EntityService to store data based on a generic data object.reated ItemCollections the EnityService also provides a simple load and find method:

    public List<ItemCollection> getTeams() {
        teams = new ArrayList<ItemCollection>();
        Collection<ItemCollecti/imixsworkflow/on> col = null;
        String sQuery = "SELECT wi FROM Entity AS wi " + " WHERE wi.type= '"
                + getType() + "' ORDER BY wi.modified desc";
        col = entityService.findAllEntities(sQuery, row, maxSearchResult);
        endOfList = col.size() < maxSearchResult;
        for (ItemCollection aworkitem : col) {
            teams.add((aworkitem));
        }
        return teams;
    }

To access the dataobject from a JSF page is also very simple as all property are accessible through a Map interface.

....
<h:outputLabel for="subject_id" value="#{global.name}:">
    <h:message style="color: red" for="subject_id" showSummary="true" showDetail="true" />
</h:outputLabel>
<h:inputText  style="width: 20em;" required="true"
value="#{teamController.workitem.item['txtname']}" id="subject_id"/>
.....
<h:commandButton action="#{teamController.saveAction('teamlist')}" 
    value="#{global.save}" />

This is an example to display all data objects in a JSF table element:

<h:dataTable id="datatable_id" columns="7" value="#{teamController.teams}" var="record">
                    <h:column>
                        <f:facet name="header">
                            <h:outputText value="Name" />
                        </f:facet>
                        <h:outputText value="#{record.item['txtName']} " />
                    </h:column>
                    <h:column>
                        <f:facet name="header">
                            <h:outputText value="#{global.modified}" />
                        </f:facet>
                        <h:outputText value="#{record.item['$modified']}">
                            <f:convertDateTime type="both" dateStyle="short"
                                pattern="dd.MM.yy HH:mm:ss" />
                        </h:outputText>
                    </h:column>
                    <!-- edit -->
                    <h:column>
                        <h:commandLink action="#{teamController.editAction(record,'team')}">
                            <h:outputText value="#{global.edit}" />
                        </h:commandLink>
                    </h:column>
                    <!-- delete -->
                    <h:column>
                        <h:commandLink value="#{global.delete}" action="#{teamController.deleteAction(record,null)}"/>
                    </h:column>
 </h:dataTable>

You can find more examples on the Imixs Workflow project site. You can also checkout the complete sample application from the source code repository

http://java.net/projects/imixs-workflow/sources/svn/show/imixs-workflow-samples/imixs-workflow-web-sample/trunk 

 

Comments are closed.