Changeset 5492


Ignore:
Timestamp:
07/28/14 12:40:09 (10 years ago)
Author:
Twan Goosen
Message:

Labels and descriptions can now marshalled and unmarshalled.
Fixes #598

Also refactored the marshaller implementation a bit for readability

Location:
VirtualCollectionRegistry/trunk/VirtualCollectionRegistry/src
Files:
4 edited

Legend:

Unmodified
Added
Removed
  • VirtualCollectionRegistry/trunk/VirtualCollectionRegistry/src/main/java/eu/clarin/cmdi/virtualcollectionregistry/service/impl/VirtualCollectionMarshallerImpl.java

    r5417 r5492  
    219219                    vc.getPersistentIdentifier().getIdentifier());
    220220        }
    221         String s = null;
    222         switch (vc.getState()) {
    223         case PRIVATE:
    224             s = "private";
    225             break;
    226         case PUBLIC_PENDING:
    227             s = "public-pending";
    228             break;
    229         case PUBLIC:
    230             s = "public";
    231             break;
    232         case DELETED:
    233             s = "deleted";
    234             break;
    235         case DEAD:
    236             s = "dead";
    237             break;
    238         }
    239         writer.writeAttribute("state", s);
     221        writer.writeAttribute("state", getStateRepresentation(vc.getState()));
     222
    240223        writer.writeStartElement("Type");
    241224        switch (vc.getType()) {
     
    271254            writer.writeStartElement("Creators");
    272255            for (Creator creator : vc.getCreators()) {
    273                 writer.writeStartElement("Creator");
    274 
    275                 writer.writeStartElement("Person");
    276                 writer.writeCharacters(creator.getPerson());
    277                 writer.writeEndElement(); // "Person" element
    278 
    279                 if (creator.getAddress() != null) {
    280                     writer.writeStartElement("Address");
    281                     writer.writeCharacters(creator.getAddress());
    282                     writer.writeEndElement(); // "Address" element
    283                 }
    284 
    285                 if (creator.getEMail() != null) {
    286                     writer.writeStartElement("Email");
    287                     writer.writeCharacters(creator.getEMail());
    288                     writer.writeEndElement(); // "Email" element
    289                 }
    290 
    291                 if (creator.getOrganisation() != null) {
    292                     writer.writeStartElement("Organisation");
    293                     writer.writeCharacters(creator.getOrganisation());
    294                     writer.writeEndElement(); // "Organisation" element
    295                 }
    296 
    297                 if (creator.getTelephone() != null) {
    298                     writer.writeStartElement("Telephone");
    299                     writer.writeCharacters(creator.getTelephone());
    300                     writer.writeEndElement(); // "Telephone" element
    301                 }
    302 
    303                 if (creator.getWebsite() != null) {
    304                     writer.writeStartElement("Website");
    305                     writer.writeCharacters(creator.getWebsite());
    306                     writer.writeEndElement(); // "Website" element
    307                 }
    308 
    309                 if (creator.getRole() != null) {
    310                     writer.writeStartElement("Role");
    311                     writer.writeCharacters(creator.getRole());
    312                     writer.writeEndElement(); // "Role" element
    313                 }
    314 
    315                 writer.writeEndElement(); // "Creator" element
     256                writeCreator(writer, creator);
    316257            }
    317258            writer.writeEndElement(); // "Creators" element
     
    375316
    376317            for (final Resource resource : vc.getResources()) {
    377                 writer.writeStartElement("Resource");
    378 
    379                 writer.writeStartElement("ResourceType");
    380                 switch (resource.getType()) {
    381                 case METADATA:
    382                     writer.writeCharacters("Metadata");
    383                     break;
    384                 case RESOURCE:
    385                     writer.writeCharacters("Resource");
    386                     break;
    387                 } // switch
    388                 writer.writeEndElement(); // "ResourceType" element
    389 
    390                 writer.writeStartElement("ResourceRef");
    391                 writer.writeCharacters(resource.getRef());
    392                 writer.writeEndElement(); // "ResourceRef" element
    393 
    394                 writer.writeEndElement(); // "Resource" element
     318                writeResource(writer, resource);
    395319            }
    396320
     
    424348
    425349        writer.writeEndElement(); // "VirtualCollection" element
     350    }
     351
     352    private String getStateRepresentation(final VirtualCollection.State state) {
     353        String s = null;
     354        switch (state) {
     355            case PRIVATE:
     356                s = "private";
     357                break;
     358            case PUBLIC_PENDING:
     359                s = "public-pending";
     360                break;
     361            case PUBLIC:
     362                s = "public";
     363                break;
     364            case DELETED:
     365                s = "deleted";
     366                break;
     367            case DEAD:
     368                s = "dead";
     369                break;
     370        }
     371        return s;
     372    }
     373
     374    private void writeCreator(XMLStreamWriter writer, Creator creator) throws XMLStreamException {
     375        writer.writeStartElement("Creator");
     376       
     377        writer.writeStartElement("Person");
     378        writer.writeCharacters(creator.getPerson());
     379        writer.writeEndElement(); // "Person" element
     380       
     381        if (creator.getAddress() != null) {
     382            writer.writeStartElement("Address");
     383            writer.writeCharacters(creator.getAddress());
     384            writer.writeEndElement(); // "Address" element
     385        }
     386       
     387        if (creator.getEMail() != null) {
     388            writer.writeStartElement("Email");
     389            writer.writeCharacters(creator.getEMail());
     390            writer.writeEndElement(); // "Email" element
     391        }
     392       
     393        if (creator.getOrganisation() != null) {
     394            writer.writeStartElement("Organisation");
     395            writer.writeCharacters(creator.getOrganisation());
     396            writer.writeEndElement(); // "Organisation" element
     397        }
     398       
     399        if (creator.getTelephone() != null) {
     400            writer.writeStartElement("Telephone");
     401            writer.writeCharacters(creator.getTelephone());
     402            writer.writeEndElement(); // "Telephone" element
     403        }
     404       
     405        if (creator.getWebsite() != null) {
     406            writer.writeStartElement("Website");
     407            writer.writeCharacters(creator.getWebsite());
     408            writer.writeEndElement(); // "Website" element
     409        }
     410       
     411        if (creator.getRole() != null) {
     412            writer.writeStartElement("Role");
     413            writer.writeCharacters(creator.getRole());
     414            writer.writeEndElement(); // "Role" element
     415        }
     416       
     417        writer.writeEndElement(); // "Creator" element
     418    }
     419
     420    private void writeResource(XMLStreamWriter writer, final Resource resource) throws XMLStreamException {
     421        writer.writeStartElement("Resource");
     422       
     423        writer.writeStartElement("ResourceType");
     424        switch (resource.getType()) {
     425            case METADATA:
     426                writer.writeCharacters("Metadata");
     427                break;
     428            case RESOURCE:
     429                writer.writeCharacters("Resource");
     430                break;
     431        } // switch
     432        writer.writeEndElement(); // "ResourceType" element
     433       
     434        writer.writeStartElement("ResourceRef");
     435        writer.writeCharacters(resource.getRef());
     436        writer.writeEndElement(); // "ResourceRef" element
     437       
     438        if(resource.getLabel() != null) {
     439            writer.writeStartElement("Label");
     440            writer.writeCharacters(resource.getLabel());
     441            writer.writeEndElement(); // "Label" element
     442        }
     443       
     444        if(resource.getDescription() != null) {
     445            writer.writeStartElement("Description");
     446            writer.writeCharacters(resource.getDescription());
     447            writer.writeEndElement();
     448        }
     449       
     450        writer.writeEndElement(); // "Resource" element
    426451    }
    427452
     
    560585                }
    561586                readStart(reader, "ResourceRef", true, true);
    562                 vc.getResources().add(
    563                         new Resource(r_type, readString(reader, false)));
     587                final Resource resource = new Resource(r_type, readString(reader, false));
     588               
     589                if(readStart(reader, "Label", false, true)){
     590                    resource.setLabel(readString(reader, false));
     591                }
     592
     593                if(readStart(reader, "Description", false, true)){
     594                    resource.setDescription(readString(reader, false));
     595                }
     596
     597                vc.getResources().add(resource);
     598               
    564599            } while (readStart(reader, "Resource", false, true));
    565600        }
  • VirtualCollectionRegistry/trunk/VirtualCollectionRegistry/src/main/resources/META-INF/VirtualCollection.xsd

    r5416 r5492  
    101101                      </xs:simpleType>
    102102                    </xs:element>
     103                    <xs:element name="Label" minOccurs="0">
     104                      <xs:simpleType>
     105                        <xs:restriction base="xs:string"/>
     106                      </xs:simpleType>
     107                    </xs:element>
     108                    <xs:element name="Description" minOccurs="0">
     109                      <xs:simpleType>
     110                        <xs:restriction base="xs:string"/>
     111                      </xs:simpleType>
     112                    </xs:element>
    103113                  </xs:sequence>
    104114                </xs:complexType>
  • VirtualCollectionRegistry/trunk/VirtualCollectionRegistry/src/test/java/eu/clarin/cmdi/virtualcollectionregistry/service/impl/VirtualCollectionMarshallerTest.java

    r5416 r5492  
    11package eu.clarin.cmdi.virtualcollectionregistry.service.impl;
    22
    3 import static org.junit.Assert.assertEquals;
    4 import static org.junit.Assert.assertNotNull;
    5 import static org.junit.Assert.assertNull;
    6 
    7 import java.io.ByteArrayInputStream;
    8 import java.io.ByteArrayOutputStream;
    9 import java.io.InputStream;
    10 import java.text.DateFormat;
    11 import java.text.SimpleDateFormat;
    12 
    13 import org.junit.Test;
    14 
    15 import eu.clarin.cmdi.virtualcollectionregistry.service.VirtualCollectionMarshaller.Format;
    163import eu.clarin.cmdi.virtualcollectionregistry.model.Creator;
    174import eu.clarin.cmdi.virtualcollectionregistry.model.GeneratedBy;
     
    196import eu.clarin.cmdi.virtualcollectionregistry.model.Resource;
    207import eu.clarin.cmdi.virtualcollectionregistry.model.VirtualCollection;
     8import eu.clarin.cmdi.virtualcollectionregistry.service.VirtualCollectionMarshaller.Format;
     9import java.io.ByteArrayInputStream;
     10import java.io.ByteArrayOutputStream;
     11import java.io.InputStream;
     12import java.io.InputStreamReader;
     13import java.io.Reader;
     14import java.io.StringReader;
     15import java.text.DateFormat;
     16import java.text.SimpleDateFormat;
     17import org.custommonkey.xmlunit.Diff;
     18import org.custommonkey.xmlunit.XMLUnit;
     19import static org.junit.Assert.assertEquals;
     20import static org.junit.Assert.assertEquals;
     21import static org.junit.Assert.assertEquals;
     22import static org.junit.Assert.assertNotNull;
     23import static org.junit.Assert.assertNotNull;
     24import static org.junit.Assert.assertNotNull;
     25import static org.junit.Assert.assertNull;
     26import static org.junit.Assert.assertNull;
     27import static org.junit.Assert.assertNull;
     28
     29import static org.junit.Assert.assertTrue;
     30import org.junit.Test;
    2131
    2232public class VirtualCollectionMarshallerTest {
     
    95105        assertEquals("http://hdl.handle.net/1839/00-0000-0000-0005-671C-C",
    96106                r.getRef());
     107        assertNull(r.getLabel());
     108        assertNull(r.getDescription());
     109
    97110        r = vc.getResources().get(1);
    98111        assertNotNull(r);
     
    100113        assertEquals("http://hdl.handle.net/1839/00-0000-0000-0006-671C-C",
    101114                r.getRef());
     115        assertEquals("Second metadata resource", r.getLabel());
     116        assertNull(r.getDescription());
     117       
    102118        r = vc.getResources().get(2);
    103119        assertNotNull(r);
     
    105121        assertEquals("http://hdl.handle.net/1839/00-0000-0000-0007-671C-C",
    106122                r.getRef());
     123        assertNull(r.getLabel());
     124        assertEquals("Third metadata resource", r.getDescription());
    107125
    108126        assertNull(vc.getGeneratedBy());
     127
     128        // round-tripping
     129        ByteArrayOutputStream out = new ByteArrayOutputStream(65536);
     130        m.marshal(out, Format.XML, vc);
     131        VirtualCollection vc2 =
     132            m.unmarshal(new ByteArrayInputStream(out.toByteArray()),
     133                    Format.XML, "utf-8");
     134        assertEquals(vc.getOwner(), vc2.getOwner());
     135        assertEquals(vc.getPersistentIdentifier(),
     136                vc2.getPersistentIdentifier());
     137        assertEquals(vc.getState(), vc2.getState());
     138        assertEquals(vc.getType(), vc2.getType());
     139        assertEquals(vc.getName(), vc2.getName());
     140        assertEquals(vc.getDescription(), vc2.getDescription());
     141        assertEquals(vc.getCreationDate(), vc2.getCreationDate());
     142        assertEquals(vc.getCreators(), vc2.getCreators());
     143        assertEquals(vc.getPurpose(), vc2.getPurpose());
     144        assertEquals(vc.getReproducibility(), vc2.getReproducibility());
     145        assertEquals(vc.getReproducibilityNotice(),
     146                vc2.getReproducibilityNotice());
     147        assertEquals(vc.getKeywords(), vc2.getKeywords());
     148        assertEquals(vc.getResources(), vc2.getResources());
     149        assertEquals(vc.getGeneratedBy(), vc2.getGeneratedBy());
     150        assertEquals(vc, vc2);       
     151    }
     152
     153    @Test
     154    public void testUnmarshallIntensionalCollection() throws Exception {
     155        VirtualCollectionMarshallerImpl m =
     156            new VirtualCollectionMarshallerImpl();
     157        InputStream in = this.getClass().getClassLoader()
     158                .getResourceAsStream("VirtualCollection-intensional.xml");
     159        VirtualCollection vc = m.unmarshal(in, Format.XML, "utf-8");
     160
     161        final DateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
     162        assertNotNull(vc);
     163        assertEquals("Test Collection 2", vc.getName());
     164        assertEquals(VirtualCollection.Type.INTENSIONAL, vc.getType());
     165        assertEquals("Test Description", vc.getDescription());
     166        assertEquals(sdf.parse("2009-12-31"), vc.getCreationDate());
     167        assertEquals(VirtualCollection.Purpose.SAMPLE, vc.getPurpose());
     168        assertEquals(VirtualCollection.Reproducibility.UNTENDED,
     169                vc.getReproducibility());
     170        assertEquals("Unmaintaned test collection.",
     171                vc.getReproducibilityNotice());
     172
     173        assertNotNull(vc.getKeywords());
     174        assertEquals(2, vc.getKeywords().size());
     175        assertNotNull(vc.getKeywords());
     176        assertEquals("test", vc.getKeywords().get(0));
     177        assertEquals("unmaintained", vc.getKeywords().get(1));
     178
     179        assertNotNull(vc.getCreators());
     180        assertEquals(1, vc.getCreators().size());
     181        Creator c = vc.getCreators().get(0);
     182        assertNotNull(c);
     183        assertEquals("Joe User", c.getPerson());
     184        assertEquals("joe.user@example.com", c.getEMail());
     185        assertEquals("Example Organization", c.getOrganisation());
     186
     187
     188        assertEquals(0, vc.getResources().size());
     189
     190        GeneratedBy generatedBy = vc.getGeneratedBy();
     191        assertNotNull(generatedBy);
     192        assertEquals("GeneratedBy description", generatedBy.getDescription());
     193        assertEquals("urn:x-vcr:test-uri", generatedBy.getURI());
     194        GeneratedByQuery query = generatedBy.getQuery();
     195        assertNotNull(query);
     196        assertEquals("sql", query.getProfile());
     197        assertEquals("<SQL>Test-Query</SQL>", query.getValue());
    109198
    110199        // round-tripping
     
    133222    }
    134223
    135     @Test
    136     public void testUnmarshallIntensionalCollection() throws Exception {
    137         VirtualCollectionMarshallerImpl m =
    138             new VirtualCollectionMarshallerImpl();
    139         InputStream in = this.getClass().getClassLoader()
    140                 .getResourceAsStream("VirtualCollection-intensional.xml");
    141         VirtualCollection vc = m.unmarshal(in, Format.XML, "utf-8");
    142 
    143         final DateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
    144         assertNotNull(vc);
    145         assertEquals("Test Collection 2", vc.getName());
    146         assertEquals(VirtualCollection.Type.INTENSIONAL, vc.getType());
    147         assertEquals("Test Description", vc.getDescription());
    148         assertEquals(sdf.parse("2009-12-31"), vc.getCreationDate());
    149         assertEquals(VirtualCollection.Purpose.SAMPLE, vc.getPurpose());
    150         assertEquals(VirtualCollection.Reproducibility.UNTENDED,
    151                 vc.getReproducibility());
    152         assertEquals("Unmaintaned test collection.",
    153                 vc.getReproducibilityNotice());
    154 
    155         assertNotNull(vc.getKeywords());
    156         assertEquals(2, vc.getKeywords().size());
    157         assertNotNull(vc.getKeywords());
    158         assertEquals("test", vc.getKeywords().get(0));
    159         assertEquals("unmaintained", vc.getKeywords().get(1));
    160 
    161         assertNotNull(vc.getCreators());
    162         assertEquals(1, vc.getCreators().size());
    163         Creator c = vc.getCreators().get(0);
    164         assertNotNull(c);
    165         assertEquals("Joe User", c.getPerson());
    166         assertEquals("joe.user@example.com", c.getEMail());
    167         assertEquals("Example Organization", c.getOrganisation());
    168 
    169 
    170         assertEquals(0, vc.getResources().size());
    171 
    172         GeneratedBy generatedBy = vc.getGeneratedBy();
    173         assertNotNull(generatedBy);
    174         assertEquals("GeneratedBy description", generatedBy.getDescription());
    175         assertEquals("urn:x-vcr:test-uri", generatedBy.getURI());
    176         GeneratedByQuery query = generatedBy.getQuery();
    177         assertNotNull(query);
    178         assertEquals("sql", query.getProfile());
    179         assertEquals("<SQL>Test-Query</SQL>", query.getValue());
    180 
    181         // round-tripping
    182         ByteArrayOutputStream out = new ByteArrayOutputStream(65536);
    183         m.marshal(out, Format.XML, vc);
    184         VirtualCollection vc2 =
    185             m.unmarshal(new ByteArrayInputStream(out.toByteArray()),
    186                     Format.XML, "utf-8");
    187         assertEquals(vc.getOwner(), vc2.getOwner());
    188         assertEquals(vc.getPersistentIdentifier(),
    189                 vc2.getPersistentIdentifier());
    190         assertEquals(vc.getState(), vc2.getState());
    191         assertEquals(vc.getType(), vc2.getType());
    192         assertEquals(vc.getName(), vc2.getName());
    193         assertEquals(vc.getDescription(), vc2.getDescription());
    194         assertEquals(vc.getCreationDate(), vc2.getCreationDate());
    195         assertEquals(vc.getCreators(), vc2.getCreators());
    196         assertEquals(vc.getPurpose(), vc2.getPurpose());
    197         assertEquals(vc.getReproducibility(), vc2.getReproducibility());
    198         assertEquals(vc.getReproducibilityNotice(),
    199                 vc2.getReproducibilityNotice());
    200         assertEquals(vc.getKeywords(), vc2.getKeywords());
    201         assertEquals(vc.getResources(), vc2.getResources());
    202         assertEquals(vc.getGeneratedBy(), vc2.getGeneratedBy());
    203         assertEquals(vc, vc2);
    204     }
    205 
    206224} // class VirtualCollectionMarshallerTest
  • VirtualCollectionRegistry/trunk/VirtualCollectionRegistry/src/test/resources/VirtualCollection-extensional.xml

    r859 r5492  
    4141            <ResourceType>Metadata</ResourceType>
    4242            <ResourceRef>http://hdl.handle.net/1839/00-0000-0000-0006-671C-C</ResourceRef>
     43            <Label>Second metadata resource</Label>
    4344        </Resource>
    4445        <Resource>
    4546            <ResourceType>Metadata</ResourceType>
    4647            <ResourceRef>http://hdl.handle.net/1839/00-0000-0000-0007-671C-C</ResourceRef>
     48            <Description>Third metadata resource</Description>
    4749        </Resource>
    4850    </Resources>
Note: See TracChangeset for help on using the changeset viewer.