2 * Copyright (C) 2007-2012 Argeo GmbH
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
16 package org
.argeo
.slc
.jcr
;
18 import javax
.jcr
.Node
;
19 import javax
.jcr
.RepositoryException
;
20 import javax
.jcr
.Session
;
21 import javax
.jcr
.nodetype
.NodeType
;
23 import org
.argeo
.jcr
.JcrUtils
;
24 import org
.argeo
.jcr
.UserJcrUtils
;
25 import org
.argeo
.slc
.SlcException
;
28 * Utilities around the SLC JCR Result model. Note that it relies on fixed base
29 * paths (convention over configuration) for optimization purposes.
31 public class SlcJcrResultUtils
{
34 * Returns the path to the current slc:result node
36 public static String
getSlcResultsBasePath(Session session
) {
38 Node userHome
= UserJcrUtils
.getUserHome(session
);
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
);
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.
58 public static Node
getSlcResultsParentNode(Session session
) {
60 String absPath
= getSlcResultsBasePath(session
);
61 if (session
.nodeExists(absPath
)) {
62 Node currNode
= session
.getNode(absPath
);
63 if (currNode
.isNodeType(NodeType
.NT_UNSTRUCTURED
))
66 throw new SlcException(
67 "A node already exists at this path : " + absPath
68 + " that has the wrong type. ");
70 Node slcResParNode
= JcrUtils
.mkdirs(session
, absPath
);
71 slcResParNode
.setPrimaryType(NodeType
.NT_UNSTRUCTURED
);
75 } catch (RepositoryException re
) {
76 throw new SlcException(
77 "Unexpected error while creating slcResult root parent node.",
83 * Returns the path to the current Result UI specific node, depending the
86 public static String
getMyResultsBasePath(Session session
) {
88 Node userHome
= UserJcrUtils
.getUserHome(session
);
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
);
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.
108 public static Node
getMyResultParentNode(Session session
) {
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
))
116 throw new SlcException(
117 "A node already exists at this path : " + absPath
118 + " that has the wrong type. ");
120 Node myResParNode
= JcrUtils
.mkdirs(session
, absPath
);
121 myResParNode
.setPrimaryType(SlcTypes
.SLC_MY_RESULT_ROOT_FOLDER
);
125 } catch (RepositoryException re
) {
126 throw new SlcException(
127 "Unexpected error while creating user MyResult base node.",
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.
141 public static synchronized Node
createResultFolderNode(Session session
,
144 if (session
.nodeExists(absPath
)) {
146 Node currNode
= session
.getNode(absPath
);
147 if (currNode
.isNodeType(SlcTypes
.SLC_RESULT_FOLDER
))
150 throw new SlcException(
151 "A node already exists at this path : " + absPath
152 + " that has the wrong type. ");
154 Node rfNode
= JcrUtils
.mkdirs(session
, absPath
);
155 rfNode
.setPrimaryType(SlcTypes
.SLC_RESULT_FOLDER
);
156 Node statusNode
= rfNode
.addNode(SlcNames
.SLC_AGGREGATED_STATUS
,
158 statusNode
.setProperty(SlcNames
.SLC_SUCCESS
, true);
161 } catch (RepositoryException re
) {
162 throw new SlcException(
163 "Unexpected error while creating Result Folder node.", re
);