]> git.argeo.org Git - gpl/argeo-slc.git/blob - org.argeo.slc.core/src/org/argeo/slc/jcr/SlcJcrResultUtils.java
Make processes persistence switchable
[gpl/argeo-slc.git] / org.argeo.slc.core / src / org / argeo / slc / jcr / SlcJcrResultUtils.java
1 /*
2 * Copyright (C) 2007-2012 Argeo GmbH
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16 package org.argeo.slc.jcr;
17
18 import javax.jcr.Node;
19 import javax.jcr.RepositoryException;
20 import javax.jcr.Session;
21 import javax.jcr.nodetype.NodeType;
22
23 import org.argeo.jcr.JcrUtils;
24 import org.argeo.node.NodeUtils;
25 import org.argeo.slc.SlcException;
26 import org.argeo.slc.SlcNames;
27 import org.argeo.slc.SlcTypes;
28
29 /**
30 * Utilities around the SLC JCR Result model. Note that it relies on fixed base
31 * paths (convention over configuration) for optimization purposes.
32 */
33 public class SlcJcrResultUtils {
34
35 /**
36 * Returns the path to the current slc:result node
37 */
38 public static String getSlcResultsBasePath(Session session) {
39 try {
40 Node userHome = NodeUtils.getUserHome(session);
41 if (userHome == null)
42 throw new SlcException("No user home available for "
43 + session.getUserID());
44 return userHome.getPath() + '/' + SlcNames.SLC_SYSTEM + '/'
45 + SlcNames.SLC_RESULTS;
46 } catch (RepositoryException re) {
47 throw new SlcException(
48 "Unexpected error while getting Slc Results Base Path.", re);
49 }
50 }
51
52 /**
53 * Returns the base node to store SlcResults. If it does not exists, it is
54 * created. If a node already exists at the given path with the wrong type,
55 * it throws an exception.
56 *
57 * @param session
58 * @return
59 */
60 public static Node getSlcResultsParentNode(Session session) {
61 try {
62 String absPath = getSlcResultsBasePath(session);
63 if (session.nodeExists(absPath)) {
64 Node currNode = session.getNode(absPath);
65 if (currNode.isNodeType(NodeType.NT_UNSTRUCTURED))
66 return currNode;
67 else
68 throw new SlcException(
69 "A node already exists at this path : " + absPath
70 + " that has the wrong type. ");
71 } else {
72 Node slcResParNode = JcrUtils.mkdirs(session, absPath);
73 slcResParNode.setPrimaryType(NodeType.NT_UNSTRUCTURED);
74 session.save();
75 return slcResParNode;
76 }
77 } catch (RepositoryException re) {
78 throw new SlcException(
79 "Unexpected error while creating slcResult root parent node.",
80 re);
81 }
82 }
83
84 /**
85 * Returns the path to the current Result UI specific node, depending the
86 * current user
87 */
88 public static String getMyResultsBasePath(Session session) {
89 try {
90 Node userHome = NodeUtils.getUserHome(session);
91 if (userHome == null)
92 throw new SlcException("No user home available for "
93 + session.getUserID());
94 return userHome.getPath() + '/' + SlcNames.SLC_SYSTEM + '/'
95 + SlcNames.SLC_MY_RESULTS;
96 } catch (RepositoryException re) {
97 throw new SlcException(
98 "Unexpected error while getting Slc Results Base Path.", re);
99 }
100 }
101
102 /**
103 * Creates a new node with type SlcTypes.SLC_MY_RESULT_ROOT_FOLDER at the
104 * given absolute path. If a node already exists at the given path, returns
105 * that node if it has the correct type and throws an exception otherwise.
106 *
107 * @param session
108 * @return
109 */
110 public static Node getMyResultParentNode(Session session) {
111 try {
112 String absPath = getMyResultsBasePath(session);
113 if (session.nodeExists(absPath)) {
114 Node currNode = session.getNode(absPath);
115 if (currNode.isNodeType(SlcTypes.SLC_MY_RESULT_ROOT_FOLDER))
116 return currNode;
117 else
118 throw new SlcException(
119 "A node already exists at this path : " + absPath
120 + " that has the wrong type. ");
121 } else {
122 Node myResParNode = JcrUtils.mkdirs(session, absPath);
123 myResParNode.setPrimaryType(SlcTypes.SLC_MY_RESULT_ROOT_FOLDER);
124 session.save();
125 return myResParNode;
126 }
127 } catch (RepositoryException re) {
128 throw new SlcException(
129 "Unexpected error while creating user MyResult base node.",
130 re);
131 }
132 }
133
134 /**
135 * Creates a new node with type SlcTypes.SLC_RESULT_FOLDER at the given
136 * absolute path. If a node already exists at the given path, returns that
137 * node if it has the correct type and throws an exception otherwise.
138 *
139 * @param session
140 * @param absPath
141 * @return
142 */
143 public static synchronized Node createResultFolderNode(Session session,
144 String absPath) {
145 try {
146 if (session.nodeExists(absPath)) {
147 // Sanity check
148 Node currNode = session.getNode(absPath);
149 if (currNode.isNodeType(SlcTypes.SLC_RESULT_FOLDER))
150 return currNode;
151 else
152 throw new SlcException(
153 "A node already exists at this path : " + absPath
154 + " that has the wrong type. ");
155 }
156 Node rfNode = JcrUtils.mkdirs(session, absPath);
157 rfNode.setPrimaryType(SlcTypes.SLC_RESULT_FOLDER);
158 Node statusNode = rfNode.addNode(SlcNames.SLC_AGGREGATED_STATUS,
159 SlcTypes.SLC_CHECK);
160 statusNode.setProperty(SlcNames.SLC_SUCCESS, true);
161 session.save();
162 return rfNode;
163 } catch (RepositoryException re) {
164 throw new SlcException(
165 "Unexpected error while creating Result Folder node.", re);
166 }
167 }
168 }