]> git.argeo.org Git - gpl/argeo-slc.git/blob - runtime/org.argeo.slc.support.jcr/src/main/java/org/argeo/slc/jcr/SlcJcrResultUtils.java
support.jcr : extend SlcName with addtional Diff Result properties
[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.ArgeoException;
24 import org.argeo.jcr.JcrUtils;
25 import org.argeo.jcr.UserJcrUtils;
26 import org.argeo.slc.SlcException;
27
28 /**
29 * Utilities around the SLC JCR Result model. Note that it relies on fixed base
30 * paths (convention over configuration) for optimization purposes.
31 */
32 public class SlcJcrResultUtils {
33
34 /**
35 * Returns the path to the current slc:result node
36 */
37 public static String getSlcResultsBasePath(Session session) {
38 try {
39
40 return UserJcrUtils.getUserHome(session).getPath() + "/"
41 + SlcNames.SLC_RESULTS;
42 } catch (RepositoryException re) {
43 throw new ArgeoException(
44 "Unexpected error while getting Slc Results Base Path.", re);
45 }
46 }
47
48 /**
49 * Returns the path to the current Result UI specific node, depending the
50 * current user
51 */
52 public static String getMyResultsBasePath(Session session) {
53 try {
54 return UserJcrUtils.getUserHome(session).getPath() + "/"
55 + SlcJcrConstants.SLC_MYRESULT_BASEPATH;
56 } catch (RepositoryException re) {
57 throw new ArgeoException(
58 "Unexpected error while getting Slc Results Base Path.", re);
59 }
60 }
61
62 /**
63 * Creates a new node with type NodeType.NT_UNSTRUCTURED at the given
64 * absolute path. If a node already exists at the given path, returns that
65 * node if it has the correct type and throws an exception otherwise.
66 *
67 * @param session
68 * @return
69 */
70 public static Node getMyResultParentNode(Session session) {
71 try {
72 String absPath = getMyResultsBasePath(session);
73 if (session.nodeExists(absPath)) {
74 Node currNode = session.getNode(absPath);
75 if (currNode.isNodeType(NodeType.NT_UNSTRUCTURED))
76 return currNode;
77 else
78 throw new SlcException(
79 "A node already exists at this path : " + absPath
80 + " that has the wrong type. ");
81 } else {
82 Node myResParNode = JcrUtils.mkdirs(session, absPath);
83 myResParNode.setPrimaryType(NodeType.NT_UNSTRUCTURED);
84 session.save();
85 return myResParNode;
86 }
87 } catch (RepositoryException re) {
88 throw new ArgeoException(
89 "Unexpected error while creating user MyResult base node.",
90 re);
91 }
92 }
93
94 /**
95 * Creates a new node with type SlcTypes.SLC_RESULT_FOLDER at the given
96 * absolute path. If a node already exists at the given path, returns that
97 * node if it has the correct type and throws an exception otherwise.
98 *
99 * @param session
100 * @param absPath
101 * @return
102 */
103 public static synchronized Node createResultFolderNode(Session session,
104 String absPath) {
105 try {
106 if (session.nodeExists(absPath)) {
107 // Sanity check
108 Node currNode = session.getNode(absPath);
109 if (currNode.isNodeType(SlcTypes.SLC_RESULT_FOLDER))
110 return currNode;
111 else
112 throw new SlcException(
113 "A node already exists at this path : " + absPath
114 + " that has the wrong type. ");
115 }
116 Node rfNode = JcrUtils.mkdirs(session, absPath);
117 rfNode.setPrimaryType(SlcTypes.SLC_RESULT_FOLDER);
118 Node statusNode = rfNode.addNode(SlcNames.SLC_STATUS,
119 SlcTypes.SLC_CHECK);
120 statusNode.setProperty(SlcNames.SLC_SUCCESS, true);
121 session.save();
122 return rfNode;
123 } catch (RepositoryException re) {
124 throw new ArgeoException(
125 "Unexpected error while creating Result Folder node.", re);
126 }
127 }
128 }