Search in sources :

Example 1 with MetacardType

use of ddf.catalog.data.MetacardType in project ddf by codice.

the class MetacardImpl method writeObject.

/**
     * Serializes this {@link MetacardImpl} instance.
     *
     * @param stream the {@link ObjectOutputStream} that contains the object to be serialized
     * @throws IOException
     * @serialData First, all non-transient fields are written out by the default Java serialization
     * implementation ( {@link ObjectOutputStream#defaultWriteObject()}) . Next, the
     * {@link MetacardType} is written out as a {@link MetacardTypeImpl}. Then the
     * <i>number</i> of {@code Attribute} objects is written as an {@code int}. After
     * the number of objects, each {@code Attribute} object is written out.
     * <p>
     * <p>
     * The MetacardType object is written out as a {@link MetacardTypeImpl} object
     * because {@link MetacardTypeImpl} is a class that is part of the DDF API and is
     * guaranteed to be on the classpath when this object is deserialized. Secondly, the
     * {@link MetacardTypeImpl} has a trusted serialization implementation where the
     * object's logical representation is serialized.
     * </p>
     */
private void writeObject(ObjectOutputStream stream) throws IOException {
    /*
         * defaultWriteObject() is invoked for greater flexibility and compatibility. See the
         * *Serialization Note* in MetacardImpl's class Javadoc.
         */
    stream.defaultWriteObject();
    /*
         * Cannot allow unknown implementations of MetacardType to be serialized. Must convert them
         * to our implementation to guarantee it is serializing the logical representation and not
         * the physical representation.
         */
    if (type instanceof MetacardTypeImpl) {
        stream.writeObject(type);
    } else {
        MetacardTypeImpl mt = new MetacardTypeImpl(type.getName(), type.getAttributeDescriptors());
        stream.writeObject(mt);
    }
    if (map != null) {
        stream.writeInt(map.size());
        for (Attribute attribute : this.map.values()) {
            stream.writeObject(attribute);
        }
    } else {
        if (wrappedMetacard != null && wrappedMetacard.getMetacardType() != null) {
            MetacardType metacardType = wrappedMetacard.getMetacardType();
            List<Attribute> attributes = new ArrayList<Attribute>();
            if (metacardType.getAttributeDescriptors() == null) {
                // no descriptors, means no attributes can be defined.
                // no attributes defined, means no attributes written to
                // disk
                stream.writeInt(0);
            } else {
                for (AttributeDescriptor ad : metacardType.getAttributeDescriptors()) {
                    Attribute attribute = wrappedMetacard.getAttribute(ad.getName());
                    if (attribute != null) {
                        attributes.add(attribute);
                    }
                }
                // Must loop again because the size of the attributes list
                // is not known until list has been fully populated.
                stream.writeInt(attributes.size());
                for (Attribute attribute : attributes) {
                    stream.writeObject(attribute);
                }
            }
        }
    }
}
Also used : Attribute(ddf.catalog.data.Attribute) ArrayList(java.util.ArrayList) AttributeDescriptor(ddf.catalog.data.AttributeDescriptor) MetacardType(ddf.catalog.data.MetacardType)

Example 2 with MetacardType

use of ddf.catalog.data.MetacardType in project ddf by codice.

the class MetacardImplTest method testSerializationSingle.

@Test
public void testSerializationSingle() throws IOException, ClassNotFoundException, URISyntaxException {
    MetacardImpl metacard = new MetacardImpl();
    Date now = new Date();
    metacard.setTitle("Flagstaff");
    metacard.setContentTypeName("nitf");
    metacard.setContentTypeVersion("DDF_20");
    metacard.setLocation("POINT (1 0)");
    metacard.setMetadata("<something/>");
    metacard.setCreatedDate(now);
    metacard.setResourceURI(new URI("http://ddf.com"));
    byte[] buffer = { -86 };
    metacard.setThumbnail(buffer);
    metacard.setSourceId("mySourceId");
    metacard.setDescription("Northern Arizona City");
    metacard.setPointOfContact("poc");
    Serializer<Metacard> serializer = new Serializer<Metacard>();
    /* WRITE */
    serializer.serialize(metacard, DEFAULT_SERIALIZATION_FILE_LOCATION);
    /* READ */
    Metacard readMetacard = serializer.deserialize(DEFAULT_SERIALIZATION_FILE_LOCATION);
    assertEquals(metacard.getTitle(), readMetacard.getTitle());
    assertEquals(metacard.getContentTypeName(), readMetacard.getContentTypeName());
    assertEquals(metacard.getContentTypeVersion(), readMetacard.getContentTypeVersion());
    assertEquals(metacard.getLocation(), readMetacard.getLocation());
    assertEquals(metacard.getMetadata(), readMetacard.getMetadata());
    assertEquals(metacard.getCreatedDate(), readMetacard.getCreatedDate());
    assertEquals(metacard.getExpirationDate(), readMetacard.getExpirationDate());
    assertEquals(metacard.getResourceURI(), readMetacard.getResourceURI());
    assertEquals(metacard.getResourceSize(), readMetacard.getResourceSize());
    assertTrue(Arrays.equals(metacard.getThumbnail(), readMetacard.getThumbnail()));
    assertEquals(metacard.getSourceId(), readMetacard.getSourceId());
    assertEquals(metacard.getDescription(), readMetacard.getAttribute("description").getValue());
    assertEquals(metacard.getPointOfContact(), readMetacard.getAttribute("point-of-contact").getValue());
    MetacardType metacardType = metacard.getMetacardType();
    MetacardType readMetacardType = readMetacard.getMetacardType();
    assertEquals(metacardType.getName(), readMetacardType.getName());
    Set<AttributeDescriptor> oldAd = metacardType.getAttributeDescriptors();
    Set<AttributeDescriptor> newAd = readMetacardType.getAttributeDescriptors();
    assertEquals(oldAd.size(), newAd.size());
    for (int i = 0; i < oldAd.size(); i++) {
        AttributeDescriptor oldDescriptor = oldAd.iterator().next();
        boolean match = false;
        for (AttributeDescriptor newDescriptor : newAd) {
            if (oldDescriptor.equals(newDescriptor)) {
                match = true;
                break;
            }
        }
        assertTrue(match);
    }
}
Also used : Metacard(ddf.catalog.data.Metacard) AttributeDescriptor(ddf.catalog.data.AttributeDescriptor) URI(java.net.URI) Date(java.util.Date) MetacardType(ddf.catalog.data.MetacardType) Test(org.junit.Test)

Example 3 with MetacardType

use of ddf.catalog.data.MetacardType in project ddf by codice.

the class MetacardTypeImplTest method testDateTimeType.

@Test
public void testDateTimeType() {
    List<MetacardType> metacardTypeList = new ArrayList<>();
    metacardTypeList.add(DATE_TIME_ATTRIBUTES);
    MetacardType metacardType = new MetacardTypeImpl(TEST_NAME, metacardTypeList);
    assertMetacardAttributes(metacardType, CORE_ATTRIBUTES.getAttributeDescriptors());
    assertMetacardAttributes(metacardType, DATE_TIME_ATTRIBUTES.getAttributeDescriptors());
}
Also used : ArrayList(java.util.ArrayList) MetacardType(ddf.catalog.data.MetacardType) Test(org.junit.Test)

Example 4 with MetacardType

use of ddf.catalog.data.MetacardType in project ddf by codice.

the class MetacardTypeImplTest method testSerializationSingle.

@Test
public void testSerializationSingle() throws IOException, ClassNotFoundException {
    HashSet<AttributeDescriptor> descriptors = new HashSet<>();
    descriptors.add(new AttributeDescriptorImpl(ID, true, true, false, false, BasicTypes.STRING_TYPE));
    MetacardTypeImpl metacardType = new MetacardTypeImpl("basic", descriptors);
    String fileLocation = "target/metacardType.ser";
    Serializer<MetacardType> serializer = new Serializer<>();
    serializer.serialize(metacardType, fileLocation);
    MetacardType readMetacardType = serializer.deserialize(fileLocation);
    assertThat(metacardType.getName(), is(readMetacardType.getName()));
    assertThat(metacardType.getAttributeDescriptor(ID).getName(), is(readMetacardType.getAttributeDescriptor(ID).getName()));
    assertEquals(metacardType.getAttributeDescriptor(ID).getType().getBinding(), readMetacardType.getAttributeDescriptor(ID).getType().getBinding());
    assertThat(metacardType.getAttributeDescriptor(ID).getType().getAttributeFormat(), is(readMetacardType.getAttributeDescriptor(ID).getType().getAttributeFormat()));
    Set<AttributeDescriptor> oldAd = metacardType.getAttributeDescriptors();
    Set<AttributeDescriptor> newAd = readMetacardType.getAttributeDescriptors();
    assertThat(oldAd.iterator().next(), is(newAd.iterator().next()));
}
Also used : AttributeDescriptor(ddf.catalog.data.AttributeDescriptor) MetacardType(ddf.catalog.data.MetacardType) HashSet(java.util.HashSet) Test(org.junit.Test)

Example 5 with MetacardType

use of ddf.catalog.data.MetacardType in project ddf by codice.

the class MetacardImplTest method testSerializationSingleWrapped.

@Test
public void testSerializationSingleWrapped() throws IOException, ClassNotFoundException, URISyntaxException {
    MetacardImpl innerMetacard = new MetacardImpl();
    Date now = new Date();
    innerMetacard.setTitle("Flagstaff");
    innerMetacard.setContentTypeName("nitf");
    innerMetacard.setContentTypeVersion("DDF_20");
    innerMetacard.setLocation("POINT (1 0)");
    innerMetacard.setMetadata("<something/>");
    innerMetacard.setCreatedDate(now);
    innerMetacard.setResourceURI(new URI("http://ddf.com"));
    byte[] buffer = { -86 };
    innerMetacard.setThumbnail(buffer);
    innerMetacard.setDescription("Northern Arizona City");
    innerMetacard.setPointOfContact("poc");
    Metacard metacard = new MetacardImpl(innerMetacard);
    Serializer<Metacard> serializer = new Serializer<Metacard>();
    serializer.serialize(metacard, DEFAULT_SERIALIZATION_FILE_LOCATION);
    Metacard readMetacard = serializer.deserialize(DEFAULT_SERIALIZATION_FILE_LOCATION);
    assertEquals(metacard.getTitle(), readMetacard.getTitle());
    assertEquals(metacard.getContentTypeName(), readMetacard.getContentTypeName());
    assertEquals(metacard.getContentTypeVersion(), readMetacard.getContentTypeVersion());
    assertEquals(metacard.getLocation(), readMetacard.getLocation());
    assertEquals(metacard.getMetadata(), readMetacard.getMetadata());
    assertEquals(metacard.getCreatedDate(), readMetacard.getCreatedDate());
    assertEquals(metacard.getExpirationDate(), readMetacard.getExpirationDate());
    assertEquals(metacard.getResourceURI(), readMetacard.getResourceURI());
    assertEquals(metacard.getResourceSize(), readMetacard.getResourceSize());
    assertEquals(metacard.getAttribute("description").getValue(), readMetacard.getAttribute("description").getValue());
    assertEquals(metacard.getAttribute("point-of-contact").getValue(), readMetacard.getAttribute("point-of-contact").getValue());
    assertTrue(Arrays.equals(metacard.getThumbnail(), readMetacard.getThumbnail()));
    MetacardType metacardType = metacard.getMetacardType();
    MetacardType readMetacardType = readMetacard.getMetacardType();
    assertEquals(metacardType.getName(), readMetacardType.getName());
    Set<AttributeDescriptor> oldAd = metacardType.getAttributeDescriptors();
    Set<AttributeDescriptor> newAd = readMetacardType.getAttributeDescriptors();
    assertEquals(oldAd.size(), newAd.size());
    assertEquals(oldAd.size(), newAd.size());
    for (int i = 0; i < oldAd.size(); i++) {
        AttributeDescriptor oldDescriptor = oldAd.iterator().next();
        boolean match = false;
        for (AttributeDescriptor newDescriptor : newAd) {
            if (oldDescriptor.equals(newDescriptor)) {
                match = true;
                break;
            }
        }
        assertTrue(match);
    }
}
Also used : Metacard(ddf.catalog.data.Metacard) AttributeDescriptor(ddf.catalog.data.AttributeDescriptor) URI(java.net.URI) Date(java.util.Date) MetacardType(ddf.catalog.data.MetacardType) Test(org.junit.Test)

Aggregations

MetacardType (ddf.catalog.data.MetacardType)88 Test (org.junit.Test)57 AttributeDescriptor (ddf.catalog.data.AttributeDescriptor)41 Metacard (ddf.catalog.data.Metacard)35 ArrayList (java.util.ArrayList)29 MetacardImpl (ddf.catalog.data.impl.MetacardImpl)20 MetacardTypeImpl (ddf.catalog.data.impl.MetacardTypeImpl)17 HashSet (java.util.HashSet)15 Attribute (ddf.catalog.data.Attribute)12 Serializable (java.io.Serializable)10 IOException (java.io.IOException)8 Date (java.util.Date)8 HashMap (java.util.HashMap)8 List (java.util.List)8 Map (java.util.Map)8 AttributeDescriptorImpl (ddf.catalog.data.impl.AttributeDescriptorImpl)7 InputStream (java.io.InputStream)7 Set (java.util.Set)7 ContentItem (ddf.catalog.content.data.ContentItem)6 ContentItemImpl (ddf.catalog.content.data.impl.ContentItemImpl)6