Skip to content

Commit 1cdc11b

Browse files
committed
Merge pull request #1 from nttuyen/master
Add JCR implement for todo-app
2 parents b92ac87 + 32298fa commit 1cdc11b

File tree

21 files changed

+1547
-217
lines changed

21 files changed

+1547
-217
lines changed

todo-app/todomvc-core/pom.xml

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -9,32 +9,32 @@
99
<finalName>todomvc-core</finalName>
1010
</build>
1111
<dependencies>
12-
<dependency>
13-
<groupId>org.exoplatform.kernel</groupId>
14-
<artifactId>exo.kernel.container</artifactId>
15-
<version>2.3.8-GA</version>
16-
<scope>test</scope>
17-
</dependency>
1812

19-
<dependency>
20-
<groupId>org.exoplatform.kernel</groupId>
21-
<artifactId>exo.kernel.commons</artifactId>
22-
<version>2.3.8-GA</version>
23-
<scope>test</scope>
24-
</dependency>
13+
<dependency>
14+
<groupId>org.exoplatform.jcr</groupId>
15+
<artifactId>exo.jcr.component.core</artifactId>
16+
<version>1.15.3-GA</version>
17+
<scope>provided</scope>
18+
</dependency>
19+
<dependency>
20+
<groupId>org.exoplatform.jcr</groupId>
21+
<artifactId>exo.jcr.component.ext</artifactId>
22+
<version>1.15.3-GA</version>
23+
<scope>provided</scope>
24+
</dependency>
2525

2626
<dependency>
2727
<groupId>junit</groupId>
2828
<artifactId>junit</artifactId>
29-
<version>3.8.1</version>
29+
<version>4.10</version>
3030
<scope>test</scope>
3131
</dependency>
3232

3333
<dependency>
3434
<groupId>org.slf4j</groupId>
3535
<artifactId>slf4j-api</artifactId>
3636
<version>1.7.1</version>
37-
<scope>compile</scope>
37+
<scope>provided</scope>
3838
</dependency>
3939

4040
<dependency>
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
/*
2+
* Copyright (C) 2012 eXo Platform SAS.
3+
*
4+
* This is free software; you can redistribute it and/or modify it
5+
* under the terms of the GNU Lesser General Public License as
6+
* published by the Free Software Foundation; either version 2.1 of
7+
* the License, or (at your option) any later version.
8+
*
9+
* This software is distributed in the hope that it will be useful,
10+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
11+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12+
* Lesser General Public License for more details.
13+
*
14+
* You should have received a copy of the GNU Lesser General Public
15+
* License along with this software; if not, write to the Free
16+
* Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
17+
* 02110-1301 USA, or see the FSF site: http://www.fsf.org.
18+
*/
19+
package org.gatein.portlet.todomvc.model.jcr;
20+
21+
import javax.jcr.Node;
22+
import javax.jcr.Property;
23+
24+
public class TodoNode {
25+
private final Node node;
26+
27+
public TodoNode(Node node) {
28+
this.node = node;
29+
}
30+
31+
public String getName() throws Exception {
32+
return node.getName();
33+
}
34+
35+
public void setJob(String job) throws Exception {
36+
node.setProperty("todo:job", job);
37+
}
38+
public String getJob() throws Exception {
39+
return node.getProperty("todo:job").getString();
40+
}
41+
42+
public void setPriority(int priority) throws Exception {
43+
node.setProperty("todo:priority", priority);
44+
}
45+
public int getPriority() throws Exception {
46+
return (int)node.getProperty("todo:priority").getLong();
47+
}
48+
49+
public void setCompleted(boolean isCompleted) throws Exception {
50+
node.setProperty("todo:isCompleted", isCompleted);
51+
}
52+
public boolean isCompleted() throws Exception {
53+
return node.getProperty("todo:isCompleted").getBoolean();
54+
}
55+
56+
public void save() throws Exception {
57+
node.save();
58+
}
59+
public void remove() throws Exception {
60+
node.remove();
61+
}
62+
}
Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
/*
2+
* Copyright (C) 2012 eXo Platform SAS.
3+
*
4+
* This is free software; you can redistribute it and/or modify it
5+
* under the terms of the GNU Lesser General Public License as
6+
* published by the Free Software Foundation; either version 2.1 of
7+
* the License, or (at your option) any later version.
8+
*
9+
* This software is distributed in the hope that it will be useful,
10+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
11+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12+
* Lesser General Public License for more details.
13+
*
14+
* You should have received a copy of the GNU Lesser General Public
15+
* License along with this software; if not, write to the Free
16+
* Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
17+
* 02110-1301 USA, or see the FSF site: http://www.fsf.org.
18+
*/
19+
package org.gatein.portlet.todomvc.model.jcr;
20+
21+
import java.util.LinkedList;
22+
import java.util.List;
23+
24+
import javax.jcr.Node;
25+
import javax.jcr.NodeIterator;
26+
27+
public class TodoNodeList {
28+
private final Node node;
29+
private long currentId;
30+
31+
public TodoNodeList(Node node) {
32+
this.node = node;
33+
34+
try {
35+
if(!node.hasProperty("todo:maxChildId")) {
36+
currentId = 0;
37+
this.node.setProperty("todo:maxChildId", currentId);
38+
this.node.save();
39+
}
40+
41+
this.currentId = node.getProperty("todo:maxChildId").getLong();
42+
} catch (Exception e) {
43+
currentId = 0;
44+
}
45+
}
46+
47+
public String getName() throws Exception {
48+
return node.getName();
49+
}
50+
51+
public TodoNode newTodo() throws Exception {
52+
String nodeName = String.valueOf(++this.currentId);
53+
54+
return this.newTodo(nodeName);
55+
}
56+
57+
public TodoNode newTodo(String name) throws Exception {
58+
Node todo = this.node.addNode(name, "todo:todo");
59+
return new TodoNode(todo);
60+
}
61+
public TodoNode getTodo(String name) throws Exception {
62+
Node todo = node.getNode(name);
63+
return new TodoNode(todo);
64+
}
65+
66+
public List<TodoNode> getTodos() throws Exception {
67+
NodeIterator iterator = node.getNodes();
68+
List<TodoNode> list = new LinkedList<TodoNode>();
69+
70+
while (iterator.hasNext()) {
71+
Node todo = iterator.nextNode();
72+
if(!todo.getName().equals("*")) {
73+
list.add(new TodoNode(todo));
74+
}
75+
}
76+
return list;
77+
}
78+
79+
public void save() throws Exception {
80+
node.setProperty("todo:maxChildId", this.currentId);
81+
node.save();
82+
}
83+
}
Lines changed: 162 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,162 @@
1+
/*
2+
* Copyright (C) 2012 eXo Platform SAS.
3+
*
4+
* This is free software; you can redistribute it and/or modify it
5+
* under the terms of the GNU Lesser General Public License as
6+
* published by the Free Software Foundation; either version 2.1 of
7+
* the License, or (at your option) any later version.
8+
*
9+
* This software is distributed in the hope that it will be useful,
10+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
11+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12+
* Lesser General Public License for more details.
13+
*
14+
* You should have received a copy of the GNU Lesser General Public
15+
* License along with this software; if not, write to the Free
16+
* Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
17+
* 02110-1301 USA, or see the FSF site: http://www.fsf.org.
18+
*/
19+
package org.gatein.portlet.todomvc.service.impl;
20+
21+
import java.util.List;
22+
import javax.jcr.Node;
23+
import org.gatein.portlet.todomvc.model.jcr.TodoNode;
24+
import org.gatein.portlet.todomvc.model.jcr.TodoNodeList;
25+
import org.gatein.portlet.todomvc.service.TodoService;
26+
import org.gatein.portlet.todomvc.service.impl.jcr.JCRHelper;
27+
import org.json.JSONArray;
28+
import org.json.JSONObject;
29+
30+
public class TodoServiceJCRImpl implements TodoService {
31+
public static final String JCR_PROPERTY_JOB = "job";
32+
public static final String JCR_PROPERTY_PRIORITY = "priority";
33+
public static final String JCR_PROPERTY_COMPLETED = "completed";
34+
35+
public static final String JSON_KEY_ID = "id";
36+
public static final String JSON_KEY_COMPLETED = "completed";
37+
public static final String JSON_KEY_DISPLAY = "display";
38+
public static final String JSON_KEY_EDITING = "editing";
39+
public static final String JSON_KEY_JOB = "job";
40+
public static final String JSON_KEY_PRIORITY = "priority";
41+
42+
private JCRHelper jcrHelper;
43+
44+
public TodoServiceJCRImpl(JCRHelper jcrHelper) {
45+
this.jcrHelper = jcrHelper;
46+
}
47+
48+
public TodoNodeList getTodoNodeList(String todoListId) throws Exception {
49+
return this.jcrHelper.getTodoNodeList(todoListId);
50+
}
51+
52+
@Override
53+
public void saveListTodos(String todoListId, JSONArray jsonArray) throws Exception {
54+
int size = jsonArray.length();
55+
for(int i = 0; i < size; i++) {
56+
JSONObject jsonObject = jsonArray.getJSONObject(i);
57+
this.saveTodo(todoListId, jsonObject);
58+
}
59+
}
60+
61+
@Override
62+
public void saveTodo(String todoListId, JSONObject jsonObject) throws Exception {
63+
TodoNodeList todoList = this.getTodoNodeList(todoListId);
64+
65+
66+
String job = jsonObject.getString(JSON_KEY_JOB);
67+
int priority = jsonObject.getInt(JSON_KEY_PRIORITY);
68+
boolean completed = jsonObject.getBoolean(JSON_KEY_COMPLETED);
69+
70+
TodoNode todo = todoList.newTodo();
71+
todo.setCompleted(completed);
72+
todo.setPriority(priority);
73+
todo.setJob(job);
74+
75+
todoList.save();
76+
todo.save();
77+
}
78+
79+
@Override
80+
public JSONArray getListTodos(String todoListId) throws Exception {
81+
TodoNodeList todoNodeList = this.getTodoNodeList(todoListId);
82+
83+
JSONArray array = new JSONArray();
84+
85+
List<TodoNode> todoNodes = todoNodeList.getTodos();
86+
for(TodoNode todo : todoNodes) {
87+
JSONObject object = new JSONObject();
88+
89+
object.put(JSON_KEY_ID, todo.getName());
90+
object.put(JSON_KEY_PRIORITY, todo.getPriority());
91+
object.put(JSON_KEY_COMPLETED, todo.isCompleted());
92+
object.put(JSON_KEY_JOB, todo.getJob());
93+
94+
array.put(object);
95+
}
96+
97+
return array;
98+
}
99+
100+
@Override
101+
public JSONObject getTodo(String todoListId, int todoId) throws Exception {
102+
TodoNodeList todoNodeList = this.getTodoNodeList(todoListId);
103+
104+
TodoNode todo = todoNodeList.getTodo(String.valueOf(todoId));
105+
106+
JSONObject object = new JSONObject();
107+
object.put(JSON_KEY_ID, Integer.valueOf(todo.getName()));
108+
object.put(JSON_KEY_PRIORITY, todo.getPriority());
109+
object.put(JSON_KEY_COMPLETED, todo.isCompleted());
110+
object.put(JSON_KEY_JOB, todo.getJob());
111+
112+
return object;
113+
}
114+
115+
@Override
116+
public void updateListTodos(String todoListId, JSONArray jsonArray) throws Exception {
117+
int size = jsonArray.length();
118+
for(int i = 0; i < size; i++) {
119+
JSONObject jsonObject = jsonArray.getJSONObject(i);
120+
this.updateTodo(todoListId, jsonObject);
121+
}
122+
}
123+
124+
@Override
125+
public void updateTodo(String todoListId, JSONObject jsonObject) throws Exception {
126+
127+
int id = jsonObject.getInt(JSON_KEY_ID);
128+
129+
TodoNodeList todoNodeList = this.getTodoNodeList(todoListId);
130+
TodoNode todo = todoNodeList.getTodo(String.valueOf(id));
131+
132+
133+
String job = jsonObject.getString(JSON_KEY_JOB);
134+
int priority = jsonObject.getInt(JSON_KEY_PRIORITY);
135+
boolean completed = jsonObject.getBoolean(JSON_KEY_COMPLETED);
136+
todo.setPriority(priority);
137+
todo.setCompleted(completed);
138+
todo.setJob(job);
139+
140+
todoNodeList.save();
141+
}
142+
143+
@Override
144+
public void deleteListTodos(String todoListId, JSONArray jsonArray) throws Exception {
145+
int size = jsonArray.length();
146+
for(int i = 0; i < size; i++) {
147+
JSONObject jsonObject = jsonArray.getJSONObject(i);
148+
this.deleteTodo(todoListId, jsonObject);
149+
}
150+
}
151+
152+
@Override
153+
public void deleteTodo(String todoListId, JSONObject jsonObject) throws Exception {
154+
TodoNodeList todoNodeList = this.getTodoNodeList(todoListId);
155+
156+
int id = jsonObject.getInt(JSON_KEY_ID);
157+
TodoNode todo = todoNodeList.getTodo(String.valueOf(id));
158+
todo.remove();
159+
160+
todoNodeList.save();
161+
}
162+
}

0 commit comments

Comments
 (0)