]> git.argeo.org Git - gpl/argeo-slc.git/blob - runtime/org.argeo.slc.support.jcr/src/main/java/org/argeo/slc/jcr/SlcJcrResultUtils.java
Improve launcher
[gpl/argeo-slc.git] / runtime / org.argeo.slc.support.jcr / src / main / java / 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.jcr.UserJcrUtils;
25 import org.argeo.slc.SlcException;
26
27 /**
28 * Utilities around the SLC JCR Result model. Note that it relies on fixed base
29 * paths (convention over configuration) for optimization purposes.
30 */
31 public class SlcJcrResultUtils {
32
33 /**
34 * Returns the path to the current slc:result node
35 */
36 public static String getSlcResultsBasePath(Session session) {
37 try {
38 Node userHome = UserJcrUtils.getUserHome(session);
39 if (userHome == null)
40 throw new SlcException("No user home available for "
41 + session.getUserID());
42 return userHome.getPath() + '/' + SlcNames.SLC_SYSTEM + '/'
43 + SlcNames.SLC_RESULTS;
44 } catch (RepositoryException re) {
45 throw new SlcException(
46 "Unexpected error while getting Slc Results Base Path.", re);
47 }
48 }
49
50 /**
51 * Returns the base node to store SlcResults. If it does not exists, it is
52 * created. If a node already exists at the given path with the wrong type,
53 * it throws an exception.
54 *
55 * @param session
56 * @return
57 */
58 public static Node getSlcResultsParentNode(Session session) {
59 try {
60 String absPath = getSlcResultsBasePath(session);
61 if (session.nodeExists(absPath)) {
62 Node currNode = session.getNode(absPath);
63 if (currNode.isNodeType(NodeType.NT_UNSTRUCTURED))
64 return currNode;
65 else
66 throw new SlcException(
67 "A node already exists at this path : " + absPath
68 + " that has the wrong type. ");
69 } else {
70 Node slcResParNode = JcrUtils.mkdirs(session, absPath);
71 slcResParNode.setPrimaryType(NodeType.NT_UNSTRUCTURED);
72 session.save();
73 return slcResParNode;
74 }
75 } catch (RepositoryException re) {
76 throw new SlcException(
77 "Unexpected error while creating slcResult root parent node.",
78 re);
79 }
80 }
81
82 /**
83 * Returns the path to the current Result UI specific node, depending the
84 * current user
85 */
86 public static String getMyResultsBasePath(Session session) {
87 try {
88 Node userHome = UserJcrUtils.getUserHome(session);
89 if (userHome == null)
90 throw new SlcException("No user home available for "
91 + session.getUserID());
92 return userHome.getPath() + '/' + SlcNames.SLC_SYSTEM + '/'
93 + SlcNames.SLC_MY_RESULTS;
94 } catch (RepositoryException re) {
95 throw new SlcException(
96 "Unexpected error while getting Slc Results Base Path.", re);
97 }
98 }
99
100 /**
101 * Creates a new node with type SlcTypes.SLC_MY_RESULT_ROOT_FOLDER at the
102 * given absolute path. If a node already exists at the given path, returns
103 * that node if it has the correct type and throws an exception otherwise.
104 *
105 * @param session
106 * @return
107 */
108 public static Node getMyResultParentNode(Session session) {
109 try {
110 String absPath = getMyResultsBasePath(session);
111 if (session.nodeExists(absPath)) {
112 Node currNode = session.getNode(absPath);
113 if (currNode.isNodeType(SlcTypes.SLC_MY_RESULT_ROOT_FOLDER))
114 return currNode;
115 else
116 throw new SlcException(
117 "A node already exists at this path : " + absPath
118 + " that has the wrong type. ");
119 } else {
120 Node myResParNode = JcrUtils.mkdirs(session, absPath);
121 myResParNode.setPrimaryType(SlcTypes.SLC_MY_RESULT_ROOT_FOLDER);
122 session.save();
123 return myResParNode;
124 }
125 } catch (RepositoryException re) {
126 throw new SlcException(
127 "Unexpected error while creating user MyResult base node.",
128 re);
129 }
130 }
131
132 /**
133 * Creates a new node with type SlcTypes.SLC_RESULT_FOLDER at the given
134 * absolute path. If a node already exists at the given path, returns that
135 * node if it has the correct type and throws an exception otherwise.
136 *
137 * @param session
138 * @param absPath
139 * @return
140 */
141 public static synchronized Node createResultFolderNode(Session session,
142 String absPath) {
143 try {
144 if (session.nodeExists(absPath)) {
145 // Sanity check
146 Node currNode = session.getNode(absPath);
147 if (currNode.isNodeType(SlcTypes.SLC_RESULT_FOLDER))
148 return currNode;
149 else
150 throw new SlcException(
151 "A node already exists at this path : " + absPath
152 + " that has the wrong type. ");
153 }
154 Node rfNode = JcrUtils.mkdirs(session, absPath);
155 rfNode.setPrimaryType(SlcTypes.SLC_RESULT_FOLDER);
156 Node statusNode = rfNode.addNode(SlcNames.SLC_AGGREGATED_STATUS,
157 SlcTypes.SLC_CHECK);
158 statusNode.setProperty(SlcNames.SLC_SUCCESS, true);
159 session.save();
160 return rfNode;
161 } catch (RepositoryException re) {
162 throw new SlcException(
163 "Unexpected error while creating Result Folder node.", re);
164 }
165 }
166 }