Skip to content

Commit

Permalink
Change transactions to not use @transactional.
Browse files Browse the repository at this point in the history
Only use the absolute number of required transactions.

Fix rebuild index on windows.

Fix sort templates.
  • Loading branch information
Jesse Eichar committed Mar 12, 2014
1 parent 7c2bc1f commit 9f1741b
Show file tree
Hide file tree
Showing 226 changed files with 9,833 additions and 2,960 deletions.
4 changes: 4 additions & 0 deletions code_quality/findbugs-excludes.xml
Expand Up @@ -141,6 +141,10 @@
<Match>
<Class name="org.fao.geonet.services.ownership.OwnershipUtils$1" />
</Match>
<Match>
<Class name="org.fao.geonet.wro4j.TemplatesUriLocator" />
<Bug pattern="OS_OPEN_STREAM" />
</Match>


<!-- The following bugs are hard to fix should be fixed because they can cause problems in servers with multiple geonetworks running -->
Expand Down
7 changes: 7 additions & 0 deletions common/src/main/java/org/fao/geonet/utils/BinaryFile.java
Expand Up @@ -563,6 +563,13 @@ public static void copy(File srcFile, File destFile) throws IOException {

}

public static void moveTo(File inFile, File outFile, String operationDescription) throws IOException {
IO.mkdirs(outFile.getParentFile(), "Error creating Parent File for operation: "+operationDescription);
if (!inFile.renameTo(outFile)) {
copy(inFile, outFile);
IO.delete(inFile, false, "org.fao.geonet");
}
}
}

//=============================================================================
Expand Down
112 changes: 103 additions & 9 deletions common/src/main/java/org/fao/geonet/utils/Xml.java
Expand Up @@ -35,11 +35,7 @@
import org.apache.fop.apps.FopFactory;
import org.apache.fop.apps.MimeConstants;
import org.apache.xml.resolver.tools.CatalogResolver;
import org.jdom.DocType;
import org.jdom.Document;
import org.jdom.Element;
import org.jdom.JDOMException;
import org.jdom.Namespace;
import org.jdom.*;
import org.jdom.input.SAXBuilder;
import org.jdom.output.Format;
import org.jdom.output.SAXOutputter;
Expand Down Expand Up @@ -666,8 +662,6 @@ public static String getString(Element data)
* @param xml the XML element
* @return the JSON response
*
* @throws JsonParseException
* @throws JsonMappingException
* @throws IOException
*/
public static String getJSON (Element xml) throws IOException {
Expand All @@ -679,8 +673,6 @@ public static String getJSON (Element xml) throws IOException {
* @param xml the XML element
* @return the JSON response
*
* @throws JsonParseException
* @throws JsonMappingException
* @throws IOException
*/
public static String getJSON (String xml) throws IOException {
Expand Down Expand Up @@ -1223,4 +1215,106 @@ private static SchemaFactory factory() {
}


/**
* Create and XPath expression for identified Element.
*
* @param element element within the xml to find an XPath for.
*
* return xpath or null if there was an error.
*/
public static String getXPathExpr(Content element) {
StringBuilder builder = new StringBuilder();
if (!doCreateXpathExpr(element, builder)) {
return null;
} else {
return builder.toString();
}
}

/**
* Create and XPath expression for identified Element.
*
* @param attribute element within the xml to find an XPath for.
*
* return xpath or null if there was an error.
*/
public static String getXPathExpr(Attribute attribute) {
StringBuilder builder = new StringBuilder();
if (!doCreateXpathExpr(attribute, builder)) {
return null;
} else {
return builder.toString();
}
}

private static boolean doCreateXpathExpr(Object content, StringBuilder builder) {
if (builder.length() > 0) {
builder.insert(0, "/");
}

Element parentElement;
if (content instanceof Element) {
Element element = (Element) content;
final List<Attribute> attributes = element.getAttributes();
doCreateAttributesXpathExpr(builder, attributes);
final String textTrim = element.getTextTrim();
if (!textTrim.isEmpty()) {
boolean addToCondition = builder.length() > 0 && builder.charAt(0) == '[';

if (!addToCondition) {
builder.insert(0, "']");
} else {
builder.deleteCharAt(0);
builder.insert(0, "' and ");
}

builder.insert(0, textTrim).insert(0, "[normalize-space(text()) = '");

}
builder.insert(0, element.getName());
if (element.getNamespacePrefix() != null && !element.getNamespacePrefix().trim().isEmpty()) {
builder.insert(0, ':').insert(0, element.getNamespacePrefix());
}
parentElement = element.getParentElement();
} else if (content instanceof Text) {
final Text text = (Text) content;
builder.insert(0, "text()");
parentElement = text.getParentElement();
} else if (content instanceof Attribute) {
Attribute attribute = (Attribute) content;
builder.insert(0, attribute.getName());
if (attribute.getNamespacePrefix() != null && !attribute.getNamespacePrefix().trim().isEmpty()) {
builder.insert(0, ':').insert(0, attribute.getNamespacePrefix());
}
builder.insert(0, '@');
parentElement = attribute.getParent();
} else {
parentElement = null;
}

if (parentElement != null && parentElement.getParentElement() != null) {
return doCreateXpathExpr(parentElement, builder);
}
return true;
}

private static void doCreateAttributesXpathExpr(StringBuilder builder, List<Attribute> attributes) {
if (!attributes.isEmpty()) {
StringBuilder attBuilder = new StringBuilder("[");
for (Attribute attribute : attributes) {
if (attBuilder.length() > 1) {
attBuilder.append(" and ");
}
attBuilder.append('@');
if (attribute.getNamespacePrefix() != null && !attribute.getNamespacePrefix().trim().isEmpty()) {
attBuilder.append(attribute.getNamespacePrefix()).append(':');
}
attBuilder.append(attribute.getName()).append(" = '").append(attribute.getValue()).append('\'');
}
attBuilder.append("]");

builder.insert(0, attBuilder);
}
}

}
64 changes: 64 additions & 0 deletions common/src/test/java/org/fao/geonet/utils/XmlTest.java
@@ -0,0 +1,64 @@
package org.fao.geonet.utils;

import org.jdom.Attribute;
import org.jdom.Element;
import org.jdom.Namespace;
import org.jdom.Text;
import org.junit.BeforeClass;
import org.junit.Test;

import java.util.Arrays;
import java.util.List;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertSame;

/**
* Test methods in the {@link org.fao.geonet.utils.Xml} utility class
* Created by Jesse on 2/6/14.
*/
public class XmlTest {
public static final Namespace GMD = Namespace.getNamespace("gmd", "http://www.isotc211.org/2005/gmd");
public static final Namespace GCO = Namespace.getNamespace("gco", "http://www.isotc211.org/2005/gco");
private static final List<Namespace> NAMESPACES = Arrays.asList(GMD, GCO);
public static Element TEST_METADATA;

@BeforeClass
public static void setUp() throws Exception {
TEST_METADATA = Xml.loadFile(XmlTest.class.getClassLoader().getResource("sampleXml.xml"));
}

@Test
public void testGetXPathExpr() throws Exception {
final Element charString = TEST_METADATA.getChild("fileIdentifier", GMD).getChild("CharacterString", GCO);
String xpath = Xml.getXPathExpr(charString);

assertEquals("gmd:fileIdentifier/gco:CharacterString[normalize-space(text())='fileIdentifier']", xpath.replaceAll("\\s+", ""));
assertSame(charString, Xml.selectElement(TEST_METADATA, xpath, NAMESPACES));
}

@Test
public void testGetXPathExprString() throws Exception {
final Text charString = (Text) TEST_METADATA.getChild("fileIdentifier", GMD).getChild("CharacterString", GCO).getContent().get(0);
String xpath = Xml.getXPathExpr(charString);

assertEquals("gmd:fileIdentifier/gco:CharacterString[normalize-space(text())='fileIdentifier']/text()", xpath.replaceAll("\\s+", ""));

final List<?> actual = Xml.selectNodes(TEST_METADATA, xpath, NAMESPACES);
assertSame(1, actual.size());
assertSame(charString, actual.get(0));
}
@Test
public void testGetXPathExprAttribute() throws Exception {
final Attribute attribute = TEST_METADATA.getChild("characterSet", GMD)
.getChild("MD_CharacterSetCode", GMD)
.getAttribute("codeListValue");
String xpath = Xml.getXPathExpr(attribute);

final List<?> actual = Xml.selectNodes(TEST_METADATA, xpath, NAMESPACES);
assertSame(1, actual.size());
assertSame(attribute, actual.get(0));
}


}

0 comments on commit 9f1741b

Please sign in to comment.