Search in sources :

Example 11 with UpdateAction

use of org.codice.ddf.spatial.ogc.csw.catalog.actions.UpdateAction in project ddf by codice.

the class TransactionRequestConverterTest method testUnmarshalByProperty.

@Test
public void testUnmarshalByProperty() throws Exception {
    String updateRequest = IOUtils.toString(TransactionRequestConverterTest.class.getResourceAsStream("/updateByPropertyRequest.xml"));
    CswTransactionRequest request = (CswTransactionRequest) xStream.fromXML(updateRequest);
    assertThat(request.getDeleteActions(), emptyCollectionOf(DeleteAction.class));
    assertThat(request.getUpdateActions(), hasSize(1));
    assertThat(request.getInsertActions(), emptyCollectionOf(InsertAction.class));
    UpdateAction action = request.getUpdateActions().get(0);
    assertThat(action.getTypeName(), is(CswConstants.CSW_RECORD));
    assertThat(action.getConstraint(), notNullValue());
    assertThat(action.getRecordProperties().size(), is(5));
    assertThat(action.getRecordProperties().get("title"), is("Updated Title"));
    assertThat(action.getRecordProperties().get("created"), is(CswUnmarshallHelper.convertToDate("2015-08-25")));
    assertThat(action.getRecordProperties().get("datatype"), is(Arrays.asList("ABC", "DEF", "GHI")));
    assertThat(action.getRecordProperties().get("language"), is(""));
    assertThat(action.getRecordProperties().get("language"), is(""));
}
Also used : UpdateAction(org.codice.ddf.spatial.ogc.csw.catalog.actions.UpdateAction) CswTransactionRequest(org.codice.ddf.spatial.ogc.csw.catalog.common.transaction.CswTransactionRequest) InsertAction(org.codice.ddf.spatial.ogc.csw.catalog.actions.InsertAction) DeleteAction(org.codice.ddf.spatial.ogc.csw.catalog.actions.DeleteAction) Test(org.junit.Test)

Example 12 with UpdateAction

use of org.codice.ddf.spatial.ogc.csw.catalog.actions.UpdateAction in project ddf by codice.

the class TransactionRequestConverterTest method testMultipleOperations.

@Test
public void testMultipleOperations() throws Exception {
    CswTransactionRequest transactionRequest = new CswTransactionRequest();
    MetacardImpl metacard = new MetacardImpl();
    metacard.setId(METACARD_ID);
    transactionRequest.setService(CswConstants.CSW);
    transactionRequest.setVerbose(true);
    transactionRequest.setVersion(CswConstants.VERSION_2_0_2);
    InsertAction insertAction = new InsertActionImpl(CswConstants.CSW_METACARD_TYPE_NAME, null, Arrays.asList(metacard));
    transactionRequest.getInsertActions().add(insertAction);
    UpdateAction updateAction = new UpdateActionImpl(metacard, CswConstants.CSW_METACARD_TYPE_NAME, null);
    transactionRequest.getUpdateActions().add(updateAction);
    DeleteType deleteType = new DeleteType();
    QueryConstraintType queryConstraintType = new QueryConstraintType();
    queryConstraintType.setCqlText("identifier = " + METACARD_ID);
    deleteType.setConstraint(queryConstraintType);
    DeleteAction deleteAction = new DeleteActionImpl(deleteType, null);
    transactionRequest.getDeleteActions().add(deleteAction);
    String xml = xStream.toXML(transactionRequest);
    Diff diff = XMLUnit.compareXML(xml, EXPECTED_MULTI_OP_XML);
    assertThat(diff.similar(), is(true));
}
Also used : InsertActionImpl(org.codice.ddf.spatial.ogc.csw.catalog.common.transaction.InsertActionImpl) UpdateActionImpl(org.codice.ddf.spatial.ogc.csw.catalog.common.transaction.UpdateActionImpl) UpdateAction(org.codice.ddf.spatial.ogc.csw.catalog.actions.UpdateAction) Diff(org.custommonkey.xmlunit.Diff) CswTransactionRequest(org.codice.ddf.spatial.ogc.csw.catalog.common.transaction.CswTransactionRequest) InsertAction(org.codice.ddf.spatial.ogc.csw.catalog.actions.InsertAction) DeleteAction(org.codice.ddf.spatial.ogc.csw.catalog.actions.DeleteAction) DeleteType(net.opengis.cat.csw.v_2_0_2.DeleteType) DeleteActionImpl(org.codice.ddf.spatial.ogc.csw.catalog.common.transaction.DeleteActionImpl) MetacardImpl(ddf.catalog.data.impl.MetacardImpl) QueryConstraintType(net.opengis.cat.csw.v_2_0_2.QueryConstraintType) Test(org.junit.Test)

Example 13 with UpdateAction

use of org.codice.ddf.spatial.ogc.csw.catalog.actions.UpdateAction in project ddf by codice.

the class TransactionRequestConverter method unmarshal.

@Override
public Object unmarshal(HierarchicalStreamReader reader, UnmarshallingContext context) {
    CswTransactionRequest cswTransactionRequest = new CswTransactionRequest();
    cswTransactionRequest.setVersion(reader.getAttribute(CswConstants.VERSION));
    cswTransactionRequest.setService(reader.getAttribute(CswConstants.SERVICE));
    cswTransactionRequest.setVerbose(Boolean.valueOf(reader.getAttribute(CswConstants.VERBOSE_RESPONSE)));
    XStreamAttributeCopier.copyXmlNamespaceDeclarationsIntoContext(reader, context);
    while (reader.hasMoreChildren()) {
        reader.moveDown();
        if (reader.getNodeName().contains("Insert")) {
            String typeName = StringUtils.defaultIfEmpty(reader.getAttribute(CswConstants.TYPE_NAME_PARAMETER), CswConstants.CSW_RECORD);
            String handle = StringUtils.defaultIfEmpty(reader.getAttribute(CswConstants.HANDLE_PARAMETER), "");
            context.put(CswConstants.TRANSFORMER_LOOKUP_KEY, TransformerManager.ID);
            context.put(CswConstants.TRANSFORMER_LOOKUP_VALUE, typeName);
            List<Metacard> metacards = new ArrayList<>();
            // Loop through the individual records to be inserted, converting each into a Metacard
            while (reader.hasMoreChildren()) {
                // move down to the record's tag
                reader.moveDown();
                Metacard metacard = (Metacard) context.convertAnother(null, MetacardImpl.class, delegatingTransformer);
                if (metacard != null) {
                    metacards.add(metacard);
                }
                // move back up to the <SearchResults> parent of the <csw:Record> tags
                reader.moveUp();
            }
            cswTransactionRequest.getInsertActions().add(new InsertActionImpl(typeName, handle, metacards));
        } else if (reader.getNodeName().contains("Delete")) {
            XStreamAttributeCopier.copyXmlNamespaceDeclarationsIntoContext(reader, context);
            Map<String, String> xmlnsAttributeToUriMappings = getXmlnsAttributeToUriMappingsFromContext(context);
            Map<String, String> prefixToUriMappings = getPrefixToUriMappingsFromXmlnsAttributes(xmlnsAttributeToUriMappings);
            StringWriter writer = new StringWriter();
            XStreamAttributeCopier.copyXml(reader, writer, xmlnsAttributeToUriMappings);
            DeleteType deleteType = getElementFromXml(writer.toString(), DeleteType.class);
            cswTransactionRequest.getDeleteActions().add(new DeleteActionImpl(deleteType, prefixToUriMappings));
        } else if (reader.getNodeName().contains("Update")) {
            XStreamAttributeCopier.copyXmlNamespaceDeclarationsIntoContext(reader, context);
            UpdateAction updateAction = parseUpdateAction(reader, context);
            cswTransactionRequest.getUpdateActions().add(updateAction);
        }
        reader.moveUp();
    }
    return cswTransactionRequest;
}
Also used : Metacard(ddf.catalog.data.Metacard) InsertActionImpl(org.codice.ddf.spatial.ogc.csw.catalog.common.transaction.InsertActionImpl) StringWriter(java.io.StringWriter) UpdateAction(org.codice.ddf.spatial.ogc.csw.catalog.actions.UpdateAction) CswTransactionRequest(org.codice.ddf.spatial.ogc.csw.catalog.common.transaction.CswTransactionRequest) ArrayList(java.util.ArrayList) DeleteType(net.opengis.cat.csw.v_2_0_2.DeleteType) DeleteActionImpl(org.codice.ddf.spatial.ogc.csw.catalog.common.transaction.DeleteActionImpl) DefaultCswRecordMap(org.codice.ddf.spatial.ogc.csw.catalog.common.converter.DefaultCswRecordMap) HashMap(java.util.HashMap) Map(java.util.Map) MetacardImpl(ddf.catalog.data.impl.MetacardImpl)

Example 14 with UpdateAction

use of org.codice.ddf.spatial.ogc.csw.catalog.actions.UpdateAction in project ddf by codice.

the class TransactionRequestConverter method parseUpdateAction.

private UpdateAction parseUpdateAction(HierarchicalStreamReader reader, UnmarshallingContext context) {
    Map<String, String> xmlnsAttributeToUriMappings = getXmlnsAttributeToUriMappingsFromContext(context);
    Map<String, String> prefixToUriMappings = getPrefixToUriMappingsFromXmlnsAttributes(xmlnsAttributeToUriMappings);
    String typeName = StringUtils.defaultIfEmpty(reader.getAttribute(CswConstants.TYPE_NAME_PARAMETER), CswConstants.CSW_RECORD);
    String handle = StringUtils.defaultIfEmpty(reader.getAttribute(CswConstants.HANDLE_PARAMETER), "");
    // Move down to the content of the <Update>.
    reader.moveDown();
    UpdateAction updateAction;
    // Do we have a list of <RecordProperty> elements or a new <csw:Record>?
    if (reader.getNodeName().contains("RecordProperty")) {
        Map<String, Serializable> cswRecordProperties = new HashMap<>();
        while (reader.getNodeName().contains("RecordProperty")) {
            String cswField;
            Serializable newValue = null;
            // Move down to the <Name>.
            reader.moveDown();
            if (reader.getNodeName().contains("Name")) {
                String attribute = reader.getValue();
                cswField = CswRecordConverter.getCswAttributeFromAttributeName(attribute);
            } else {
                throw new ConversionException("Missing Parameter Value: missing a Name in a RecordProperty.");
            }
            // Move back up to the <RecordProperty>.
            reader.moveUp();
            String attrName = DefaultCswRecordMap.getDefaultMetacardFieldForPrefixedString(cswField);
            cswRecordProperties.put(attrName, null);
            // Is there a <Value>?
            while (reader.hasMoreChildren()) {
                // Move down to the <Value>.
                reader.moveDown();
                if (reader.getNodeName().contains("Value")) {
                    newValue = getRecordPropertyValue(reader, attrName);
                } else {
                    throw new ConversionException("Invalid Parameter Value: invalid element in a RecordProperty.");
                }
                Serializable currentValue = cswRecordProperties.get(attrName);
                if (currentValue != null) {
                    if (currentValue instanceof List) {
                        ((List) currentValue).add(newValue);
                    } else {
                        LinkedList<Serializable> list = new LinkedList<>();
                        list.add(currentValue);
                        list.add(newValue);
                        cswRecordProperties.put(attrName, list);
                    }
                } else {
                    cswRecordProperties.put(attrName, newValue);
                }
                // Back to the <RecordProperty>.
                reader.moveUp();
            }
            // Back to the <Update>, look for the next <RecordProperty>.
            reader.moveUp();
            if (!reader.hasMoreChildren()) {
                // Constraint, which is required.
                throw new ConversionException("Missing Parameter Value: missing a Constraint.");
            }
            // What's the next element in the <Update>?
            reader.moveDown();
        }
        // Now there should be a <Constraint> element.
        if (reader.getNodeName().contains("Constraint")) {
            StringWriter writer = new StringWriter();
            XStreamAttributeCopier.copyXml(reader, writer, xmlnsAttributeToUriMappings);
            QueryConstraintType constraint = getElementFromXml(writer.toString(), QueryConstraintType.class);
            // For any CSW attributes that map to basic metacard attributes (e.g. title,
            // modified date, etc.), update the basic metacard attributes as well.
            Map<String, String> cswToMetacardAttributeNames = DefaultCswRecordMap.getDefaultCswRecordMap().getCswToMetacardAttributeNames();
            Map<String, Serializable> cswRecordPropertiesWithMetacardAttributes = new HashMap<>(cswRecordProperties);
            for (Entry<String, Serializable> recordProperty : cswRecordProperties.entrySet()) {
                String cswAttributeName = recordProperty.getKey();
                // basic metacard attribute.
                if (cswToMetacardAttributeNames.containsKey(cswAttributeName)) {
                    String metacardAttrName = cswToMetacardAttributeNames.get(cswAttributeName);
                    // If this basic metacard attribute hasn't already been set, set it.
                    if (!cswRecordPropertiesWithMetacardAttributes.containsKey(metacardAttrName)) {
                        Attribute metacardAttr = cswRecordConverter.getMetacardAttributeFromCswAttribute(cswAttributeName, recordProperty.getValue(), metacardAttrName);
                        cswRecordPropertiesWithMetacardAttributes.put(metacardAttrName, metacardAttr.getValue());
                    }
                }
            }
            updateAction = new UpdateActionImpl(cswRecordPropertiesWithMetacardAttributes, typeName, handle, constraint, prefixToUriMappings);
        } else {
            throw new ConversionException("Missing Parameter Value: missing a Constraint.");
        }
    } else {
        context.put(CswConstants.TRANSFORMER_LOOKUP_KEY, TransformerManager.ID);
        context.put(CswConstants.TRANSFORMER_LOOKUP_VALUE, typeName);
        Metacard metacard = (Metacard) context.convertAnother(null, MetacardImpl.class, delegatingTransformer);
        updateAction = new UpdateActionImpl(metacard, typeName, handle);
        // Move back to the <Update>.
        reader.moveUp();
    }
    return updateAction;
}
Also used : ConversionException(com.thoughtworks.xstream.converters.ConversionException) Serializable(java.io.Serializable) UpdateActionImpl(org.codice.ddf.spatial.ogc.csw.catalog.common.transaction.UpdateActionImpl) UpdateAction(org.codice.ddf.spatial.ogc.csw.catalog.actions.UpdateAction) HashMap(java.util.HashMap) Attribute(ddf.catalog.data.Attribute) LinkedList(java.util.LinkedList) QueryConstraintType(net.opengis.cat.csw.v_2_0_2.QueryConstraintType) MetacardImpl(ddf.catalog.data.impl.MetacardImpl) Metacard(ddf.catalog.data.Metacard) StringWriter(java.io.StringWriter) ArrayList(java.util.ArrayList) LinkedList(java.util.LinkedList) List(java.util.List)

Aggregations

UpdateAction (org.codice.ddf.spatial.ogc.csw.catalog.actions.UpdateAction)14 CswTransactionRequest (org.codice.ddf.spatial.ogc.csw.catalog.common.transaction.CswTransactionRequest)11 Test (org.junit.Test)9 Metacard (ddf.catalog.data.Metacard)8 MetacardImpl (ddf.catalog.data.impl.MetacardImpl)6 QueryConstraintType (net.opengis.cat.csw.v_2_0_2.QueryConstraintType)6 ArrayList (java.util.ArrayList)5 DeleteAction (org.codice.ddf.spatial.ogc.csw.catalog.actions.DeleteAction)5 InsertAction (org.codice.ddf.spatial.ogc.csw.catalog.actions.InsertAction)5 UpdateActionImpl (org.codice.ddf.spatial.ogc.csw.catalog.common.transaction.UpdateActionImpl)5 Serializable (java.io.Serializable)4 UpdateRequest (ddf.catalog.operation.UpdateRequest)3 UpdateResponse (ddf.catalog.operation.UpdateResponse)3 SimpleDateFormat (java.text.SimpleDateFormat)3 Date (java.util.Date)3 HashMap (java.util.HashMap)3 TransactionResponseType (net.opengis.cat.csw.v_2_0_2.TransactionResponseType)3 TransactionSummaryType (net.opengis.cat.csw.v_2_0_2.TransactionSummaryType)3 CswQueryFactoryTest (org.codice.ddf.spatial.ogc.csw.catalog.endpoint.CswQueryFactoryTest)3 Result (ddf.catalog.data.Result)2