]> git.argeo.org Git - gpl/argeo-slc.git/blob - runtime/org.argeo.slc.support.jcr/src/main/java/org/argeo/slc/jcr/SlcJcrResultUtils.java
ba54c1378c26e601457a34dafa13a54d7e5f077d
[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 Mathieu Baudier
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 path to the current Result UI specific node, depending the
52 * current user
53 */
54 public static String getMyResultsBasePath(Session session) {
55 try {
56 Node userHome = UserJcrUtils.getUserHome(session);
57 if (userHome == null)
58 throw new SlcException("No user home available for "
59 + session.getUserID());
60 return userHome.getPath() + '/' + SlcNames.SLC_SYSTEM + '/'
61 + SlcNames.SLC_MY_RESULTS;
62 } catch (RepositoryException re) {
63 throw new SlcException(
64 "Unexpected error while getting Slc Results Base Path.", re);
65 }
66 }
67
68 /**
69 * Creates a new node with type NodeType.NT_UNSTRUCTURED at the given
70 * absolute path. If a node already exists at the given path, returns that
71 * node if it has the correct type and throws an exception otherwise.
72 *
73 * @param session
74 * @return
75 */
76 public static Node getMyResultParentNode(Session session) {
77 try {
78 String absPath = getMyResultsBasePath(session);
79 if (session.nodeExists(absPath)) {
80 Node currNode = session.getNode(absPath);
81 if (currNode.isNodeType(NodeType.NT_UNSTRUCTURED))
82 return currNode;
83 else
84 throw new SlcException(
85 "A node already exists at this path : " + absPath
86 + " that has the wrong type. ");
87 } else {
88 Node myResParNode = JcrUtils.mkdirs(session, absPath);
89 myResParNode.setPrimaryType(NodeType.NT_UNSTRUCTURED);
90 session.save();
91 return myResParNode;
92 }
93 } catch (RepositoryException re) {
94 throw new SlcException(
95 "Unexpected error while creating user MyResult base node.",
96 re);
97 }
98 }
99
100 /**
101 * Creates a new node with type SlcTypes.SLC_RESULT_FOLDER at the given
102 * absolute path. If a node already exists at the given path, returns that
103 * node if it has the correct type and throws an exception otherwise.
104 *
105 * @param session
106 * @param absPath
107 * @return
108 */
109 public static synchronized Node createResultFolderNode(Session session,
110 String absPath) {
111 try {
112 if (session.nodeExists(absPath)) {
113 // Sanity check
114 Node currNode = session.getNode(absPath);
115 if (currNode.isNodeType(SlcTypes.SLC_RESULT_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 }
122 Node rfNode = JcrUtils.mkdirs(session, absPath);
123 rfNode.setPrimaryType(SlcTypes.SLC_RESULT_FOLDER);
124 Node statusNode = rfNode.addNode(SlcNames.SLC_STATUS,
125 SlcTypes.SLC_CHECK);
126 statusNode.setProperty(SlcNames.SLC_SUCCESS, true);
127 session.save();
128 return rfNode;
129 } catch (RepositoryException re) {
130 throw new SlcException(
131 "Unexpected error while creating Result Folder node.", re);
132 }
133 }
134 }