1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package org.exoplatform.services.cms.relations.impl;
18
19 import java.util.ArrayList;
20 import java.util.List;
21
22 import javax.jcr.ItemNotFoundException;
23 import javax.jcr.Node;
24 import javax.jcr.Property;
25 import javax.jcr.Session;
26 import javax.jcr.Value;
27
28 import org.exoplatform.container.xml.InitParams;
29 import org.exoplatform.services.cms.relations.RelationsService;
30 import org.exoplatform.services.jcr.RepositoryService;
31 import org.exoplatform.services.jcr.core.ManageableRepository;
32 import org.exoplatform.services.jcr.ext.common.SessionProvider;
33 import org.exoplatform.services.jcr.ext.hierarchy.NodeHierarchyCreator;
34 import org.exoplatform.services.log.ExoLogger;
35 import org.exoplatform.services.log.Log;
36 import org.picocontainer.Startable;
37
38
39
40
41
42 public class RelationsServiceImpl implements RelationsService, Startable {
43 private static final String RELATION_MIXIN = "exo:relationable";
44 private static final String RELATION_PROP = "exo:relation";
45
46 private RepositoryService repositoryService_;
47 private static final Log LOG = ExoLogger.getLogger(RelationsServiceImpl.class.getName());
48 public RelationsServiceImpl(RepositoryService repositoryService,
49 NodeHierarchyCreator nodeHierarchyCreator, InitParams params) {
50 repositoryService_ = repositoryService;
51 }
52
53
54
55
56 public boolean hasRelations(Node node) throws Exception {
57 if (node.isNodeType(RELATION_MIXIN)) return true;
58 return false;
59
60 }
61
62
63
64
65
66
67
68
69
70
71 private Node getNodeByUUID(String uuid, SessionProvider provider) throws Exception {
72 ManageableRepository manageRepo = repositoryService_.getCurrentRepository();
73 String[] workspaces = manageRepo.getWorkspaceNames() ;
74 for(String ws : workspaces) {
75 try{
76 return provider.getSession(ws,manageRepo).getNodeByUUID(uuid) ;
77 } catch(Exception e) {
78 continue ;
79 }
80 }
81 return null;
82 }
83
84
85
86
87 public List<Node> getRelations(Node node, SessionProvider provider) {
88 List<Node> rels = new ArrayList<Node>();
89 try {
90 if(node.hasProperty(RELATION_PROP)) {
91 Value[] values = node.getProperty(RELATION_PROP).getValues();
92 for (int i = 0; i < values.length; i++) {
93 if(getNodeByUUID(values[i].getString(), provider) != null) {
94 rels.add(getNodeByUUID(values[i].getString(), provider));
95 }
96 }
97 }
98 } catch(Exception e) {
99 if (LOG.isWarnEnabled()) {
100 LOG.warn(e.getMessage());
101 }
102 }
103 return rels ;
104 }
105
106
107
108
109 public void removeRelation(Node node, String relationPath) throws Exception {
110 List<Value> vals = new ArrayList<Value>();
111 if (!"*".equals(relationPath) && node.hasProperty(RELATION_PROP)) {
112 SessionProvider provider = SessionProvider.createSystemProvider() ;
113 Property relations = node.getProperty(RELATION_PROP);
114 if (relations != null) {
115 Value[] values = relations.getValues();
116 String uuid2Remove = null;
117 for (int i = 0; i < values.length; i++) {
118 String uuid = values[i].getString();
119 Node refNode = getNodeByUUID(uuid, provider);
120 if(refNode == null) continue ;
121 if (refNode.getPath().equals(relationPath)) uuid2Remove = uuid;
122 else vals.add(values[i]);
123 }
124 if (uuid2Remove == null) return;
125 }
126 provider.close();
127 if(vals.size() == 0) node.removeMixin(RELATION_MIXIN);
128 else node.setProperty(RELATION_PROP, vals.toArray(new Value[vals.size()]));
129 node.save() ;
130 }
131 }
132
133
134
135
136 public void addRelation(Node node, String relationPath,String workspace) throws Exception {
137 SessionProvider provider = SessionProvider.createSystemProvider() ;
138 Session session = getSession(workspace,provider) ;
139 Node catNode = (Node) session.getItem(relationPath);
140 if(!catNode.isNodeType("mix:referenceable")) {
141 catNode.addMixin("mix:referenceable") ;
142 catNode.save() ;
143 session.save() ;
144 }
145 Value value2add = session.getValueFactory().createValue(catNode);
146 if (!node.isNodeType(RELATION_MIXIN)) {
147 node.addMixin(RELATION_MIXIN);
148 node.setProperty(RELATION_PROP, new Value[] {value2add});
149 node.save() ;
150 session.save() ;
151 } else {
152 List<Value> vals = new ArrayList<Value>();
153 Value[] values = node.getProperty(RELATION_PROP).getValues();
154 for (int i = 0; i < values.length; i++) {
155 Value value = values[i];
156 String uuid = value.getString();
157 Node refNode = null ;
158 try {
159 refNode = getNodeByUUID(uuid, provider) ;
160 } catch(ItemNotFoundException ie) {
161 removeRelation(node, relationPath) ;
162 continue ;
163 }
164 if(refNode.getPath().equals(relationPath)) return;
165 vals.add(value);
166 }
167 vals.add(value2add);
168 node.setProperty(RELATION_PROP, vals.toArray(new Value[vals.size()]));
169 node.save() ;
170 session.save() ;
171 session.logout();
172 provider.close();
173 }
174 }
175
176
177
178
179 public void start() {
180 }
181
182
183
184
185 public void stop() {
186 }
187
188
189
190
191 public void init() throws Exception {
192 }
193
194
195
196
197
198
199
200 protected Session getSession() throws Exception {
201 ManageableRepository manageableRepository = repositoryService_.getCurrentRepository();
202 String workspaceName = manageableRepository.getConfiguration().getSystemWorkspaceName();
203 return manageableRepository.getSystemSession(workspaceName);
204 }
205
206
207
208
209
210
211
212
213
214 private Session getSession(String workspace, SessionProvider provider) throws Exception {
215 ManageableRepository manageableRepository = repositoryService_.getCurrentRepository();
216 return provider.getSession(workspace, manageableRepository);
217 }
218 }