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
.api
.NodeUtils
;
24 import org
.argeo
.jcr
.JcrUtils
;
25 import org
.argeo
.slc
.SlcException
;
26 import org
.argeo
.slc
.SlcNames
;
27 import org
.argeo
.slc
.SlcTypes
;
30 * Utilities around the SLC JCR Result model. Note that it relies on fixed base
31 * paths (convention over configuration) for optimization purposes.
33 public class SlcJcrResultUtils
{
36 * Returns the path to the current slc:result node
38 public static String
getSlcResultsBasePath(Session session
) {
40 Node userHome
= NodeUtils
.getUserHome(session
);
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
);
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.
60 public static Node
getSlcResultsParentNode(Session session
) {
62 String absPath
= getSlcResultsBasePath(session
);
63 if (session
.nodeExists(absPath
)) {
64 Node currNode
= session
.getNode(absPath
);
65 if (currNode
.isNodeType(NodeType
.NT_UNSTRUCTURED
))
68 throw new SlcException(
69 "A node already exists at this path : " + absPath
70 + " that has the wrong type. ");
72 Node slcResParNode
= JcrUtils
.mkdirs(session
, absPath
);
73 slcResParNode
.setPrimaryType(NodeType
.NT_UNSTRUCTURED
);
77 } catch (RepositoryException re
) {
78 throw new SlcException(
79 "Unexpected error while creating slcResult root parent node.",
85 * Returns the path to the current Result UI specific node, depending the
88 public static String
getMyResultsBasePath(Session session
) {
90 Node userHome
= NodeUtils
.getUserHome(session
);
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
);
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.
110 public static Node
getMyResultParentNode(Session session
) {
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
))
118 throw new SlcException(
119 "A node already exists at this path : " + absPath
120 + " that has the wrong type. ");
122 Node myResParNode
= JcrUtils
.mkdirs(session
, absPath
);
123 myResParNode
.setPrimaryType(SlcTypes
.SLC_MY_RESULT_ROOT_FOLDER
);
127 } catch (RepositoryException re
) {
128 throw new SlcException(
129 "Unexpected error while creating user MyResult base node.",
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.
143 public static synchronized Node
createResultFolderNode(Session session
,
146 if (session
.nodeExists(absPath
)) {
148 Node currNode
= session
.getNode(absPath
);
149 if (currNode
.isNodeType(SlcTypes
.SLC_RESULT_FOLDER
))
152 throw new SlcException(
153 "A node already exists at this path : " + absPath
154 + " that has the wrong type. ");
156 Node rfNode
= JcrUtils
.mkdirs(session
, absPath
);
157 rfNode
.setPrimaryType(SlcTypes
.SLC_RESULT_FOLDER
);
158 Node statusNode
= rfNode
.addNode(SlcNames
.SLC_AGGREGATED_STATUS
,
160 statusNode
.setProperty(SlcNames
.SLC_SUCCESS
, true);
163 } catch (RepositoryException re
) {
164 throw new SlcException(
165 "Unexpected error while creating Result Folder node.", re
);