2 * Copyright (C) 2007-2012 Mathieu Baudier
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
.dao
;
18 import java
.util
.ArrayList
;
19 import java
.util
.List
;
21 import javax
.jcr
.Node
;
22 import javax
.jcr
.NodeIterator
;
23 import javax
.jcr
.RepositoryException
;
24 import javax
.jcr
.query
.Query
;
26 import org
.apache
.commons
.logging
.Log
;
27 import org
.apache
.commons
.logging
.LogFactory
;
28 import org
.argeo
.jcr
.JcrUtils
;
29 import org
.argeo
.slc
.SlcException
;
30 import org
.argeo
.slc
.dao
.runtime
.SlcAgentDescriptorDao
;
31 import org
.argeo
.slc
.runtime
.SlcAgentDescriptor
;
33 public class SlcAgentDescriptorDaoJcr
extends AbstractSlcJcrDao
implements
34 SlcAgentDescriptorDao
{
35 private final static Log log
= LogFactory
36 .getLog(SlcAgentDescriptorDaoJcr
.class);
38 public void create(SlcAgentDescriptor slcAgentDescriptor
) {
40 nodeMapper
.save(getSession(), basePath(slcAgentDescriptor
),
43 } catch (RepositoryException e
) {
44 throw new SlcException("Cannot import " + slcAgentDescriptor
, e
);
48 public SlcAgentDescriptor
getAgentDescriptor(String agentId
) {
49 // TODO: optimize query
50 String queryString
= "//agent[@uuid='" + agentId
+ "']";
51 Query query
= createQuery(queryString
, Query
.XPATH
);
52 Node node
= JcrUtils
.querySingleNode(query
);
55 return (SlcAgentDescriptor
) nodeMapper
.load(node
);
58 public void delete(SlcAgentDescriptor slcAgentDescriptor
) {
60 String queryString
= "//agent[@uuid='"
61 + slcAgentDescriptor
.getUuid() + "']";
62 Query query
= createQuery(queryString
, Query
.XPATH
);
63 Node node
= JcrUtils
.querySingleNode(query
);
68 log
.warn("No node found for agent descriptor: "
69 + slcAgentDescriptor
);
70 } catch (RepositoryException e
) {
71 throw new SlcException("Cannot delete " + slcAgentDescriptor
, e
);
76 public void delete(String agentId
) {
78 // TODO: optimize query
79 String queryString
= "//agent[@uuid='" + agentId
+ "']";
80 Query query
= createQuery(queryString
, Query
.XPATH
);
81 Node node
= JcrUtils
.querySingleNode(query
);
84 } catch (RepositoryException e
) {
85 throw new SlcException("Cannot find AgentDescriptor" + agentId
, e
);
90 public List
<SlcAgentDescriptor
> listSlcAgentDescriptors() {
92 String queryString
= "//agent";
93 Query query
= createQuery(queryString
, Query
.XPATH
);
95 List
<SlcAgentDescriptor
> listSad
= new ArrayList
<SlcAgentDescriptor
>();
97 NodeIterator ni
= query
.execute().getNodes();
98 while (ni
.hasNext()) {
99 Node curNode
= (Node
) ni
.next();
100 // JcrUtils.debug(curNode);
101 listSad
.add((SlcAgentDescriptor
) nodeMapper
.load(curNode
));
105 } catch (Exception e
) {
106 throw new SlcException("Cannot load AgentDescriptorList", e
);