Author: varunr
Date: Tue Nov 11 23:23:34 2014
New Revision: 1638562
URL: http://svn.apache.org/r1638562
Log:
See: https://reviews.apache.org/r/14626/
Modified:
oodt/trunk/curator/src/main/java/org/apache/oodt/cas/curation/service/MetadataResource.java
oodt/trunk/filemgr/src/main/java/org/apache/oodt/cas/filemgr/util/XmlStructFactory.java
oodt/trunk/filemgr/src/main/java/org/apache/oodt/cas/filemgr/validation/XMLValidationLayer.java
Modified:
oodt/trunk/curator/src/main/java/org/apache/oodt/cas/curation/service/MetadataResource.java
URL:
http://svn.apache.org/viewvc/oodt/trunk/curator/src/main/java/org/apache/oodt/cas/curation/service/MetadataResource.java?rev=1638562&r1=1638561&r2=1638562&view=diff
==============================================================================
---
oodt/trunk/curator/src/main/java/org/apache/oodt/cas/curation/service/MetadataResource.java
(original)
+++
oodt/trunk/curator/src/main/java/org/apache/oodt/cas/curation/service/MetadataResource.java
Tue Nov 11 23:23:34 2014
@@ -26,6 +26,9 @@ import java.io.FileOutputStream;
import java.io.IOException;
import java.net.MalformedURLException;
import java.util.Arrays;
+import java.util.ArrayList;
+import java.util.Collections;
+import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Set;
@@ -36,11 +39,13 @@ import javax.servlet.http.HttpServletRes
//JAX-RS imports
import javax.ws.rs.Consumes;
+import javax.ws.rs.DELETE;
import javax.ws.rs.DefaultValue;
import javax.ws.rs.FormParam;
import javax.ws.rs.GET;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
+import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;
import javax.ws.rs.QueryParam;
import javax.ws.rs.WebApplicationException;
@@ -49,11 +54,15 @@ import javax.ws.rs.core.MultivaluedMap;
import javax.ws.rs.core.Response;
import javax.ws.rs.core.UriInfo;
+
+
//JSON imports
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import net.sf.json.JSONSerializer;
+
+
//OODT imports
import org.apache.oodt.cas.curation.service.CurationService;
import org.apache.oodt.cas.curation.structs.ExtractorConfig;
@@ -61,13 +70,16 @@ import org.apache.oodt.cas.curation.util
import org.apache.oodt.cas.curation.util.ExtractorConfigReader;
import org.apache.oodt.cas.filemgr.catalog.Catalog;
import org.apache.oodt.cas.filemgr.repository.XMLRepositoryManager;
+import org.apache.oodt.cas.filemgr.structs.Element;
import org.apache.oodt.cas.filemgr.structs.Product;
import org.apache.oodt.cas.filemgr.structs.ProductType;
import org.apache.oodt.cas.filemgr.structs.Reference;
import org.apache.oodt.cas.filemgr.structs.exceptions.CatalogException;
import
org.apache.oodt.cas.filemgr.structs.exceptions.RepositoryManagerException;
+import org.apache.oodt.cas.filemgr.structs.exceptions.ValidationLayerException;
import org.apache.oodt.cas.filemgr.system.XmlRpcFileManagerClient;
import org.apache.oodt.cas.filemgr.util.GenericFileManagerObjectFactory;
+import org.apache.oodt.cas.filemgr.validation.XMLValidationLayer;
import org.apache.oodt.cas.metadata.MetExtractor;
import org.apache.oodt.cas.metadata.Metadata;
import org.apache.oodt.cas.metadata.SerializableMetadata;
@@ -301,9 +313,10 @@ public class MetadataResource extends Cu
// this.sendRedirect("login.jsp", uriInfo, res);
Metadata metadata;
- String[] idParts = id.split("/");
+ String[] idParts = id.split("/", 3);
String policy = idParts[1];
String productType = idParts[2];
+ productType = productType.substring(0, productType.lastIndexOf("/"));
try {
metadata = getProductTypeMetadataForPolicy(policy, productType);
} catch (Exception e) {
@@ -724,4 +737,236 @@ public class MetadataResource extends Cu
return catalog;
}
+ @DELETE
+ @Path(PRODUCT_TYPE+"/remove")
+ @Produces("text/plain")
+ public boolean removeProductType(
+ @FormParam("policy") String policy,
+ @FormParam("id") String id) {
+ XMLRepositoryManager xmlRepo = getRepo(policy);
+ try {
+ ProductType type = xmlRepo.getProductTypeById(id);
+ xmlRepo.removeProductType(type);
+ return true;
+ } catch (RepositoryManagerException e) {
+ e.printStackTrace();
+ return false;
+ }
+ }
+
+ @GET
+ @Path(PRODUCT_TYPE+"/parentmap")
+ @Produces("text/plain")
+ public String getParentTypeMap(
+ @FormParam("policy") String policy) {
+ XMLValidationLayer vLayer = getValidationLayer(policy);
+ return JSONSerializer.toJSON(vLayer.getSubToSuperMap()).toString();
+ }
+
+ @POST
+ @Path(PRODUCT_TYPE+"/parent/add")
+ @Produces("text/plain")
+ public boolean addParentForProductType(
+ @FormParam("policy") String policy,
+ @FormParam("id") String id,
+ @FormParam("parentId") String parentId) {
+ XMLValidationLayer vLayer = getValidationLayer(policy);
+ XMLRepositoryManager xmlRepo = getRepo(policy);
+ try {
+ ProductType type = xmlRepo.getProductTypeById(id);
+ vLayer.addParentForProductType(type, parentId);
+ return true;
+ } catch (RepositoryManagerException e) {
+ e.printStackTrace();
+ } catch (ValidationLayerException e) {
+ e.printStackTrace();
+ }
+ return false;
+ }
+
+ @DELETE
+ @Path(PRODUCT_TYPE+"/parent/remove")
+ @Produces("text/plain")
+ public boolean removeParentForProductType(
+ @FormParam("policy") String policy,
+ @FormParam("id") String id)
+ throws ValidationLayerException {
+ XMLValidationLayer vLayer = getValidationLayer(policy);
+ XMLRepositoryManager xmlRepo = getRepo(policy);
+ try {
+ ProductType type = xmlRepo.getProductTypeById(id);
+ vLayer.removeParentForProductType(type);
+ return true;
+ } catch (RepositoryManagerException e) {
+ e.printStackTrace();
+ }
+ return false;
+ }
+
+ @POST
+ @Path(PRODUCT_TYPE+"/elements/add")
+ @Produces("text/plain")
+ public boolean addElementsForProductType(
+ @FormParam("policy") String policy,
+ @FormParam("id") String id,
+ @FormParam("elementIds") String elementIds) {
+ XMLValidationLayer vLayer = getValidationLayer(policy);
+ XMLRepositoryManager xmlRepo = getRepo(policy);
+ try {
+ ProductType type = xmlRepo.getProductTypeById(id);
+ for(String elementid: elementIds.split(",")) {
+ Element element = vLayer.getElementById(elementid);
+ if(element == null) {
+ element = new Element(elementid, elementid, "", "", "Automatically
added", "");
+ vLayer.addElement(element);
+ }
+ vLayer.addElementToProductType(type, element);
+ }
+ return true;
+ } catch (Exception e) {
+ e.printStackTrace();
+ }
+ return false;
+ }
+
+ @GET
+ @Path(PRODUCT_TYPE+"/elements")
+ @Produces("text/plain")
+ public String getElementsForProductType(
+ @FormParam("policy") String policy,
+ @FormParam("id") String id,
+ @FormParam("direct") boolean direct) {
+ XMLValidationLayer vLayer = getValidationLayer(policy);
+ XMLRepositoryManager xmlRepo = getRepo(policy);
+ try {
+ ProductType type = xmlRepo.getProductTypeById(id);
+ ArrayList<String> elementIds = new ArrayList<String>();
+ for(Element el : vLayer.getElements(type, direct)) {
+ elementIds.add(el.getElementId());
+ }
+ return JSONSerializer.toJSON(elementIds).toString();
+ } catch (Exception e) {
+ e.printStackTrace();
+ }
+ return null;
+ }
+
+ @DELETE
+ @Path(PRODUCT_TYPE+"/elements/remove/all")
+ @Produces("text/plain")
+ public boolean removeAllElementsForProductType(
+ @FormParam("policy") String policy,
+ @FormParam("id") String id) {
+ XMLValidationLayer vLayer = getValidationLayer(policy);
+ XMLRepositoryManager xmlRepo = getRepo(policy);
+ try {
+ ProductType type = xmlRepo.getProductTypeById(id);
+ List<Element> elementList = vLayer.getElements(type);
+ for(Element element: elementList) {
+ vLayer.removeElementFromProductType(type, element);
+ }
+ this.removeUnusedElements(elementList, xmlRepo, vLayer);
+ return true;
+ } catch (Exception e) {
+ e.printStackTrace();
+ }
+ return false;
+ }
+
+ @DELETE
+ @Path(PRODUCT_TYPE+"/elements/remove")
+ @Produces("text/plain")
+ public boolean removeElementsForProductType(
+ @FormParam("policy") String policy,
+ @FormParam("id") String id,
+ @FormParam("elementIds") String elementIds) {
+ XMLValidationLayer vLayer = getValidationLayer(policy);
+ XMLRepositoryManager xmlRepo = getRepo(policy);
+ try {
+ ProductType type = xmlRepo.getProductTypeById(id);
+ ArrayList<Element> elements = new ArrayList<Element>();
+ for(String elementId: elementIds.split(",")) {
+ Element element = vLayer.getElementById(elementId);
+ if(element != null) {
+ vLayer.removeElementFromProductType(type, element);
+ elements.add(element);
+ }
+ }
+ this.removeUnusedElements(elements, xmlRepo, vLayer);
+ return true;
+ } catch (Exception e) {
+ e.printStackTrace();
+ }
+ return false;
+ }
+
+ @GET
+ @Path(PRODUCT_TYPE+"/typeswithelement/{elementId}")
+ @Produces("text/plain")
+ public String getProductTypeIdsHavingElement(
+ @FormParam("policy") String policy,
+ @PathParam("elementId") String elementId) {
+ XMLValidationLayer vLayer = getValidationLayer(policy);
+ XMLRepositoryManager xmlRepo = getRepo(policy);
+ ArrayList<String> typeids = new ArrayList<String>();
+ try {
+ for(ProductType type : xmlRepo.getProductTypes()) {
+ for(Element el : vLayer.getElements(type)) {
+ if(el.getElementId().equals(elementId))
+ typeids.add(type.getProductTypeId());
+ }
+ }
+ } catch (Exception e) {
+ e.printStackTrace();
+ }
+ return JSONSerializer.toJSON(typeids).toString();
+ }
+
+
+ /*
+ * Private helper functions
+ */
+ private void removeUnusedElements(List<Element> elements,
+ XMLRepositoryManager xmlRepo, XMLValidationLayer vLayer)
+ throws ValidationLayerException,
RepositoryManagerException {
+ // Remove Elements that aren't used in any product type
+ List<ProductType> ptypelist = xmlRepo.getProductTypes();
+ HashMap<String, Boolean> usedElementIds = new HashMap<String, Boolean>();
+ for(ProductType ptype: ptypelist) {
+ List<Element> ptypeElements =
+ vLayer.getElements(ptype);
+ for(Element el: ptypeElements) {
+ usedElementIds.put(el.getElementId(), true);
+ }
+ }
+ for(Element el: elements) {
+ if(!usedElementIds.containsKey(el.getElementId()))
+ vLayer.removeElement(el);
+ }
+ }
+
+ private XMLRepositoryManager getRepo(String policy) {
+ XMLRepositoryManager xmlRepo = null;
+ String url = "file://" +
CurationService.config.getPolicyUploadPath() + "/" + policy;
+
+ try {
+ xmlRepo = new
XMLRepositoryManager(Collections.singletonList(url));
+ } catch (Exception e) {
+ e.printStackTrace();
+ }
+
+ return xmlRepo;
+ }
+
+ private XMLValidationLayer getValidationLayer(String policy) {
+ XMLValidationLayer vLayer = null;
+ String url = "file://" +
CurationService.config.getPolicyUploadPath() + "/" + policy;
+
+ try {
+ vLayer = new
XMLValidationLayer(Collections.singletonList(url));
+ } catch (Exception e) {
+ e.printStackTrace();
+ }
+ return vLayer;
+ }
}
Modified:
oodt/trunk/filemgr/src/main/java/org/apache/oodt/cas/filemgr/util/XmlStructFactory.java
URL:
http://svn.apache.org/viewvc/oodt/trunk/filemgr/src/main/java/org/apache/oodt/cas/filemgr/util/XmlStructFactory.java?rev=1638562&r1=1638561&r2=1638562&view=diff
==============================================================================
---
oodt/trunk/filemgr/src/main/java/org/apache/oodt/cas/filemgr/util/XmlStructFactory.java
(original)
+++
oodt/trunk/filemgr/src/main/java/org/apache/oodt/cas/filemgr/util/XmlStructFactory.java
Tue Nov 11 23:23:34 2014
@@ -127,9 +127,8 @@ public final class XmlStructFactory {
if (extractorRoot != null) {
NodeList extractorNodes = extractorRoot
.getElementsByTagName("extractor");
-
+ extractors = new Vector<ExtractorSpec>();
if (extractorNodes != null && extractorNodes.getLength() > 0) {
- extractors = new Vector<ExtractorSpec>();
for (int i = 0; i < extractorNodes.getLength(); i++) {
Element extractorElem = (Element) extractorNodes.item(i);
ExtractorSpec spec = new ExtractorSpec();
Modified:
oodt/trunk/filemgr/src/main/java/org/apache/oodt/cas/filemgr/validation/XMLValidationLayer.java
URL:
http://svn.apache.org/viewvc/oodt/trunk/filemgr/src/main/java/org/apache/oodt/cas/filemgr/validation/XMLValidationLayer.java?rev=1638562&r1=1638561&r2=1638562&view=diff
==============================================================================
---
oodt/trunk/filemgr/src/main/java/org/apache/oodt/cas/filemgr/validation/XMLValidationLayer.java
(original)
+++
oodt/trunk/filemgr/src/main/java/org/apache/oodt/cas/filemgr/validation/XMLValidationLayer.java
Tue Nov 11 23:23:34 2014
@@ -171,20 +171,7 @@ public class XMLValidationLayer implemen
*/
public List<Element> getElements(ProductType type)
throws ValidationLayerException {
- List<Element> elems = new Vector<Element>();
- String currType = type.getProductTypeId();
- if (productTypeElementMap.containsKey(currType)) {
- elems.addAll(productTypeElementMap.get(currType));
- }
-
- while (subToSuperMap.containsKey(currType)) {
- currType = subToSuperMap.get(currType);
- if (productTypeElementMap.containsKey(currType)) {
- elems.addAll(productTypeElementMap.get(currType));
- }
- }
-
- return elems;
+ return this.getElements(type, false);
}
/*
@@ -225,6 +212,69 @@ public class XMLValidationLayer implemen
return null;
}
+
+ /**
+ * Returns the declared elements for a {@link ProductType}
+ * @param type The {@link ProductType} to get the elements for
+ * @param direct If false, return elements of the parent product types as
well
+ * @return A list of all {@link Element} of the Product type
+ * @throws ValidationLayerException
+ * If any error occurs
+ */
+ public List<Element> getElements(ProductType type, boolean direct)
+ throws ValidationLayerException {
+ List<Element> elems = new Vector<Element>();
+ String currType = type.getProductTypeId();
+ if (productTypeElementMap.containsKey(currType)) {
+ elems.addAll(productTypeElementMap.get(currType));
+ }
+
+ if(!direct) {
+ while (subToSuperMap.containsKey(currType)) {
+ currType = subToSuperMap.get(currType);
+ if (productTypeElementMap.containsKey(currType)) {
+ elems.addAll(productTypeElementMap.get(currType));
+ }
+ }
+ }
+
+ return elems;
+ }
+
+ /**
+ * Gets the parent-child relationship between product types
+ *
+ * @return HashMap of {@link ProductType} ids mapped to their parent id
+ */
+ public HashMap<String, String> getSubToSuperMap() {
+ return subToSuperMap;
+ }
+
+ /**
+ * Sets a parentId for an existing {@link ProductType}
+ * @param type The {@link ProductType} to add a parent for
+ * @param parentId The id of the parent {@link ProductType}
+ * @throws ValidationLayerException
+ * If any error occurs
+ */
+ public void addParentForProductType(ProductType type, String parentId)
+ throws ValidationLayerException {
+ subToSuperMap.put(type.getProductTypeId(), parentId);
+ saveElementsAndMappings();
+ }
+
+ /**
+ * Removes the parent for a {@link ProductType}
+ * @param type The {@link ProductType} to remove the parent from
+ * @throws ValidationLayerException
+ * If any error occurs
+ */
+ public void removeParentForProductType(ProductType type)
+ throws ValidationLayerException {
+ subToSuperMap.remove(type.getProductTypeId());
+ saveElementsAndMappings();
+ }
+
private void saveElementsAndMappings() {
for (Iterator<String> i = xmlFileDirUris.iterator(); i.hasNext();) {