]> git.argeo.org Git - gpl/argeo-slc.git/blob - runtime/org.argeo.slc.support.jcr/src/main/java/org/argeo/slc/jcr/SlcJcrResultUtils.java
Refactor JCR utils and home usage
[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
22 import org.argeo.ArgeoException;
23 import org.argeo.jcr.ArgeoJcrUtils;
24 import org.argeo.jcr.JcrUtils;
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
39 return ArgeoJcrUtils.getUserHome(session).getPath() + "/"
40 + SlcNames.SLC_RESULTS;
41 } catch (RepositoryException re) {
42 throw new ArgeoException(
43 "Unexpected error while getting Slc Results Base Path.", re);
44 }
45 }
46
47 /**
48 * Returns the path to the current Result UI specific node, depending the
49 * current user
50 */
51 public static String getMyResultsBasePath(Session session) {
52 try {
53 return ArgeoJcrUtils.getUserHome(session).getPath() + "/"
54 + SlcJcrConstants.SLC_MYRESULT_BASEPATH;
55 } catch (RepositoryException re) {
56 throw new ArgeoException(
57 "Unexpected error while getting Slc Results Base Path.", re);
58 }
59 }
60
61 public static Node getMyResultParentNode(Session session) {
62 try {
63 if (session.nodeExists(SlcJcrResultUtils
64 .getMyResultsBasePath(session)))
65 return session.getNode(getMyResultsBasePath(session));
66 else
67 return createResultFolderNode(session,
68 getMyResultsBasePath(session));
69 } catch (RepositoryException re) {
70 throw new ArgeoException(
71 "Unexpected error while creating user MyResult base node.",
72 re);
73 }
74
75 }
76
77 /**
78 * Creates a new node with type SlcTypes.SLC_RESULT_FOLDER at the given
79 * absolute path. If a node already exists at the given path, returns that
80 * node if it has the correct type and throws an exception otherwise.
81 *
82 * @param session
83 * @param absPath
84 * @return
85 */
86 public static synchronized Node createResultFolderNode(Session session,
87 String absPath) {
88 try {
89 if (session.nodeExists(absPath)) {
90 // Sanity check
91 Node currNode = session.getNode(absPath);
92 if (currNode.isNodeType(SlcTypes.SLC_RESULT_FOLDER))
93 return currNode;
94 else
95 throw new SlcException(
96 "A node already exists at this path : " + absPath
97 + " that has the wrong type. ");
98 }
99 Node rfNode = JcrUtils.mkdirs(session, absPath);
100 rfNode.setPrimaryType(SlcTypes.SLC_RESULT_FOLDER);
101 Node statusNode = rfNode.addNode(SlcNames.SLC_STATUS,
102 SlcTypes.SLC_CHECK);
103 statusNode.setProperty(SlcNames.SLC_SUCCESS, true);
104 session.save();
105 return rfNode;
106 } catch (RepositoryException re) {
107 throw new ArgeoException(
108 "Unexpected error while creating Result Folder node.", re);
109 }
110 }
111 }