Using Makai Persistence aka Getting Started with Makai/JAMP part II

From Resin 4.0 Wiki

Revision as of 00:34, 19 June 2013 by Rick (Talk | contribs)
Jump to: navigation, search


Files

├── makaiExample.iml  (InteliJ project file)
├── pom.xml                (Maven project file)
├── taskListModel        (Model project, clean Java no Makai or Java EE depedencies)
│   ├── model.iml
│   ├── pom.xml
│   ├── src
│   │   ├── main
│   │   │   ├── java
│   │   │   │   └── example                        Simple Object Model. Just Pojos
│   │   │   │       ├── Group.java
│   │   │   │       ├── OwnerRepo.java
│   │   │   │       ├── Person.java
│   │   │   │       ├── Task.java
│   │   │   │       ├── TaskManager.java
│   │   │   │       ├── TaskOwner.java
│   │   │   │       └── Utils.java
│   │   │   └── resources
│   │   └── test
│   │       └── java
│   │           └── io
│   │               └── jamp
│   │                   └── example
│   │                       └── model
│   │                           └── TaskManagerTest.java      //Simple Test Class
│   └── taskListModel.iml
└── taskListService
    ├── pom.xml
    ├── src
    │   └── main
    │       ├── java
    │       │   ├── example
    │       │   │   ├── TaskData.java          DTO object for sending TaskData without all of the parameters
    │       │   │   └── TaskService.java      The actual Makai Service
    │       │   └── io
    │       │       └── makai
    │       │           └── example
    │       ├── resources
    │       │   └── META-INF
    │       │       └── beans.xml
    │       └── webapp
    │           ├── META-INF
    │           │   └── MANIFEST.MF
    │           ├── WEB-INF
    │           │   └── resin-web.xml
    │           └── todo.html
    └── taskListService.iml


TaskManager (POJO)

package example;

import java.util.*;

public class TaskManager {

    private Set<Task> tasks = new HashSet<Task>();
    private transient Map<TaskOwner, Set<Task>> ownerToTaskMap = new HashMap<>();
    private transient Map<String, Task> idToTask = new HashMap<>();

    public void addTask(TaskOwner owner, Task task) {
        task.setOwner(owner);
        tasks.add(task);

        Set<Task> ownerTasks = ownerToTaskMap.get(owner);
        if (ownerTasks == null) {
            ownerTasks = new HashSet<>();
        }
        ownerTasks.add(task);
        idToTask.put(task.id(), task);

    }

    public void removeTask(Task task) {
        tasks.remove(task);
        idToTask.remove(task.id());

        Set<Task> ownerTasks = ownerToTaskMap.get(task.owner());
        if (ownerTasks == null) {
            ownerTasks = new HashSet<>();
        }
        ownerTasks.remove(task);

    }


    public Set<Task> listTasksForOwner(TaskOwner doer) {
        return ownerToTaskMap.get(doer);
    }

    public Task getTaskBy(String id) {
        return idToTask.get(id);
    }


    //These two are needed for persistence
    public Set<Task> tasks() {
        return tasks;
    }

    public Map<TaskOwner, Set<Task>> owners() {
        return ownerToTaskMap;
    }

    //This gets called as starup once owners and tasks are read from async store
    public void init(Map<TaskOwner, Set<Task>> owners, Set<Task> tasks) {
        if (owners!=null) {
            this.ownerToTaskMap = owners;
        }

        if (tasks!=null) {
            this.tasks = tasks;
        }

        /* We can rebuild taskIdMap from tasks so we don't store it */
        for (Task task : this.tasks) {
            this.idToTask.put(task.id(), task);
        }

    }
}

Personal tools
TOOLBOX
LANGUAGES