X-Git-Url: http://git.argeo.org/?a=blobdiff_plain;f=runtime%2Forg.argeo.slc.support.jcr%2Fsrc%2Fmain%2Fjava%2Forg%2Fargeo%2Fslc%2Fjcr%2Fdao%2FTreeTestResultDaoJcr.java;h=b0e0d5997f13f1e9dcfc1a225ce14901c11e16fb;hb=4663b42433ba4a16c81d90927aecff2a923cd7a4;hp=61a5b757369d99da03de5db8f6245f66cac9d269;hpb=dfe0c5ca8925056d9483a180d72ab7607e28b03a;p=gpl%2Fargeo-slc.git diff --git a/runtime/org.argeo.slc.support.jcr/src/main/java/org/argeo/slc/jcr/dao/TreeTestResultDaoJcr.java b/runtime/org.argeo.slc.support.jcr/src/main/java/org/argeo/slc/jcr/dao/TreeTestResultDaoJcr.java index 61a5b7573..b0e0d5997 100644 --- a/runtime/org.argeo.slc.support.jcr/src/main/java/org/argeo/slc/jcr/dao/TreeTestResultDaoJcr.java +++ b/runtime/org.argeo.slc.support.jcr/src/main/java/org/argeo/slc/jcr/dao/TreeTestResultDaoJcr.java @@ -1,3 +1,19 @@ +/* + * Copyright (C) 2010 Mathieu Baudier + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + package org.argeo.slc.jcr.dao; import java.util.ArrayList; @@ -6,12 +22,12 @@ import java.util.Date; import java.util.GregorianCalendar; import java.util.List; import java.util.Map; +import java.util.SortedMap; import javax.jcr.Node; import javax.jcr.NodeIterator; import javax.jcr.RepositoryException; import javax.jcr.Session; -import javax.jcr.Workspace; import javax.jcr.query.Query; import javax.jcr.query.QueryManager; import javax.jcr.query.QueryResult; @@ -19,7 +35,6 @@ import javax.jcr.query.QueryResult; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; import org.argeo.jcr.JcrUtils; -import org.argeo.jcr.NodeMapper; import org.argeo.slc.SlcException; import org.argeo.slc.core.attachment.SimpleAttachment; import org.argeo.slc.core.structure.tree.TreeSPath; @@ -41,35 +56,16 @@ public class TreeTestResultDaoJcr extends AbstractSlcJcrDao implements private final static Log log = LogFactory .getLog(TreeTestResultDaoJcr.class); - private Workspace workspace; - private QueryManager queryManager; - private NodeMapper nodeMapper; - - - public TreeTestResultDaoJcr() { - } - - public void init() { - try { - workspace = getSession().getWorkspace(); - queryManager = workspace.getQueryManager(); - nodeMapper = getNodeMapperProvider().findNodeMapper(null); - } catch (RepositoryException e) { - throw new SlcException("Cannot initialize DAO", e); - } - } - - public void create(TestResult testResult) { + public synchronized void create(TestResult testResult) { try { nodeMapper.save(getSession(), basePath(testResult), testResult); getSession().save(); - //JcrUtils.debug(getSession().getRootNode()); } catch (Exception e) { throw new SlcException("Cannot create testResult " + testResult, e); } } - public void update(TestResult testResult) { + public synchronized void update(TestResult testResult) { try { nodeMapper.save(getSession(), basePath(testResult), testResult); getSession().save(); @@ -79,18 +75,12 @@ public class TreeTestResultDaoJcr extends AbstractSlcJcrDao implements } public TreeTestResult getTestResult(String uuid) { - - try { - String queryString = "//testresult[@uuid='" + uuid + "']"; - Query query = queryManager.createQuery(queryString, Query.XPATH); - Node node = JcrUtils.querySingleNode(query); - if (node == null) - return null; - return (TreeTestResult) nodeMapper.load(node); - - } catch (Exception e) { - throw new SlcException("Cannot load TestResult with ID " + uuid, e); - } + String queryString = "//testresult[@uuid='" + uuid + "']"; + Query query = createQuery(queryString, Query.XPATH); + Node node = JcrUtils.querySingleNode(query); + if (node == null) + return null; + return (TreeTestResult) nodeMapper.load(node); } @@ -98,7 +88,7 @@ public class TreeTestResultDaoJcr extends AbstractSlcJcrDao implements try { // TODO: optimize query String queryString = "//testresult"; - Query query = queryManager.createQuery(queryString, Query.XPATH); + Query query = createQuery(queryString, Query.XPATH); QueryResult queryResult = query.execute(); NodeIterator nodeIterator = queryResult.getNodes(); if (nodeIterator.hasNext()) { @@ -116,7 +106,7 @@ public class TreeTestResultDaoJcr extends AbstractSlcJcrDao implements } else return null; - } catch (Exception e) { + } catch (RepositoryException e) { throw new SlcException("Cannot load list of TestResult ", e); } } @@ -125,7 +115,7 @@ public class TreeTestResultDaoJcr extends AbstractSlcJcrDao implements try { // TODO: optimize query String queryString = "//testresult" + path.getAsUniqueString(); - Query query = queryManager.createQuery(queryString, Query.XPATH); + Query query = createQuery(queryString, Query.XPATH); QueryResult queryResult = query.execute(); NodeIterator nodeIterator = queryResult.getNodes(); if (nodeIterator.hasNext()) { @@ -138,22 +128,27 @@ public class TreeTestResultDaoJcr extends AbstractSlcJcrDao implements } else return null; - } catch (Exception e) { + } catch (RepositoryException e) { throw new SlcException("Cannot load list of TestResult ", e); } } - public void close(final String testResultId, final Date closeDate) { + public synchronized void close(final String testResultId, + final Date closeDate) { try { // TODO: optimize query - String queryString = "//testresult[@uiid='" + testResultId + "']"; - Query query = queryManager.createQuery(queryString, Query.XPATH); + String queryString = "//testresult[@uuid='" + testResultId + "']"; + Query query = createQuery(queryString, Query.XPATH); Node resNode = JcrUtils.querySingleNode(query); Calendar cal = new GregorianCalendar(); cal.setTime(closeDate); - resNode.setProperty("closeDate", cal); + if (resNode != null) + resNode.setProperty("closeDate", cal); + else if (log.isDebugEnabled()) + log.debug("Cannot close because a node for test result # " + + testResultId + " was not found"); getSession().save(); - } catch (Exception e) { + } catch (RepositoryException e) { throw new SlcException("Cannot close TestResult " + testResultId, e); } @@ -168,14 +163,14 @@ public class TreeTestResultDaoJcr extends AbstractSlcJcrDao implements */ // TODO do we load objects, do treatment and persist them or do we work // directly in JCR - public void addResultPart(final String testResultId, final TreeSPath path, - final SimpleResultPart resultPart, + public synchronized void addResultPart(final String testResultId, + final TreeSPath path, final SimpleResultPart resultPart, final Map relatedElements) { try { // TODO: optimize query - String queryString = "//*[@uuid='" + testResultId + "']"; - Query query = queryManager.createQuery(queryString, Query.XPATH); + String queryString = "//testresult[@uuid='" + testResultId + "']"; + Query query = createQuery(queryString, Query.XPATH); Node resNode = JcrUtils.querySingleNode(query); Node curNode; @@ -225,9 +220,41 @@ public class TreeTestResultDaoJcr extends AbstractSlcJcrDao implements // We add the tags Map tags = relatedElements.get(key) .getTags(); + for (String tag : tags.keySet()) { - curNode.setProperty(tag, tags.get(tag)); + NodeIterator tagIt = curNode.getNodes("tag"); + Node tagNode = null; + while (tagIt.hasNext()) { + Node n = tagIt.nextNode(); + if (n.getProperty("name").getString().equals(tag)) { + tagNode = n; + } + } + + if (tagNode == null) { + tagNode = curNode.addNode("tag"); + tagNode.setProperty("name", tag); + } + + tagNode.setProperty("value", tags.get(tag)); + + // remove forbidden characters + // String cleanTag = + // JcrUtils.removeForbiddenCharacters(tag); + // if (!cleanTag.equals(tag)) + // log.warn("Tag '" + tag + "' persisted as '" + + // cleanTag + // + "'"); + // childNode.setProperty(cleanTag, tags.get(tag)); } + // for (String tag : tags.keySet()) { + // String cleanTag = JcrUtils + // .removeForbiddenCharacters(tag); + // if (!cleanTag.equals(tag)) + // log.warn("Tag '" + tag + "' persisted as '" + // + cleanTag + "'"); + // curNode.setProperty(cleanTag, tags.get(tag)); + // } // We set the class in order to be able to retrieve curNode.setProperty("class", StructureElement.class @@ -236,12 +263,12 @@ public class TreeTestResultDaoJcr extends AbstractSlcJcrDao implements } getSession().save(); - } catch (Exception e) { + } catch (RepositoryException e) { throw new SlcException("Cannot add resultPart", e); } } - public void addAttachment(final String testResultId, + public synchronized void addAttachment(final String testResultId, final SimpleAttachment attachment) { try { @@ -252,11 +279,11 @@ public class TreeTestResultDaoJcr extends AbstractSlcJcrDao implements // Check if attachment already exists String queryString = "//testresult[@uuid='" + testResultId + "']"; - Query query = queryManager.createQuery(queryString, Query.XPATH); + Query query = createQuery(queryString, Query.XPATH); Node resNode = JcrUtils.querySingleNode(query); queryString = ".//*[@uuid='" + attachment.getUuid() + "']"; - query = queryManager.createQuery(queryString, Query.XPATH); + query = createQuery(queryString, Query.XPATH); Node atNode = JcrUtils.querySingleNode(query); if (atNode != null) { @@ -276,7 +303,7 @@ public class TreeTestResultDaoJcr extends AbstractSlcJcrDao implements getSession().save(); } - } catch (Exception e) { + } catch (RepositoryException e) { throw new SlcException("Cannot Add Attachment to " + testResultId, e); } @@ -293,30 +320,28 @@ public class TreeTestResultDaoJcr extends AbstractSlcJcrDao implements return null; return (TreeTestResult) nodeMapper.load(node); - } catch (Exception e) { + } catch (RepositoryException e) { throw new SlcException("Cannot load TestResult with ID " + testResultId + " For Session " + session, e); } } - public void updateAttributes(final String testResultId, - final Map attributes) { + public synchronized void updateAttributes(final String testResultId, + final SortedMap attributes) { try { String queryString = "//testresult[@uuid='" + testResultId + "']"; - Query query = queryManager.createQuery(queryString, Query.XPATH); + Query query = createQuery(queryString, Query.XPATH); Node node = JcrUtils.querySingleNode(query); - for (String key: attributes.keySet()){ - node.setProperty(key, attributes.get(key)); + for (String key : attributes.keySet()) { + node.setProperty(key, attributes.get(key)); } getSession().save(); - } catch (Exception e) { - throw new SlcException("Cannot update Attributes on TestResult with ID " - + testResultId , e); + } catch (RepositoryException e) { + throw new SlcException( + "Cannot update Attributes on TestResult with ID " + + testResultId, e); } } - - - }