Advertisement
Guest User

Untitled

a guest
Nov 30th, 2011
72
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 19.05 KB | None | 0 0
  1. package fr.univNantes.geodoc.app.portailmel.services.inner;
  2.  
  3. import java.awt.geom.Rectangle2D;
  4. import java.net.URI;
  5. import java.net.URISyntaxException;
  6. import java.util.ArrayList;
  7. import java.util.Collection;
  8. import java.util.Date;
  9. import java.util.HashMap;
  10. import java.util.HashSet;
  11. import java.util.List;
  12. import java.util.Locale;
  13. import java.util.Map;
  14. import java.util.Set;
  15.  
  16. import org.geotoolkit.internal.simple.SimpleReferenceIdentifier;
  17. import org.geotoolkit.metadata.iso.DefaultIdentifier;
  18. import org.geotoolkit.metadata.iso.DefaultMetadata;
  19. import org.geotoolkit.metadata.iso.citation.DefaultCitationDate;
  20. import org.geotoolkit.metadata.iso.citation.DefaultContact;
  21. import org.geotoolkit.metadata.iso.citation.DefaultOnlineResource;
  22. import org.geotoolkit.metadata.iso.citation.DefaultResponsibleParty;
  23. import org.geotoolkit.metadata.iso.distribution.DefaultDigitalTransferOptions;
  24. import org.geotoolkit.metadata.iso.distribution.DefaultDistribution;
  25. import org.geotoolkit.metadata.iso.extent.DefaultExtent;
  26. import org.geotoolkit.metadata.iso.extent.DefaultGeographicBoundingBox;
  27. import org.geotoolkit.metadata.iso.extent.DefaultGeographicDescription;
  28. import org.geotoolkit.metadata.iso.identification.DefaultBrowseGraphic;
  29. import org.geotoolkit.metadata.iso.identification.DefaultRepresentativeFraction;
  30. import org.geotoolkit.util.DefaultInternationalString;
  31. import org.opengis.metadata.Identifier;
  32. import org.opengis.metadata.Metadata;
  33. import org.opengis.metadata.citation.Citation;
  34. import org.opengis.metadata.citation.CitationDate;
  35. import org.opengis.metadata.citation.Contact;
  36. import org.opengis.metadata.citation.DateType;
  37. import org.opengis.metadata.citation.OnLineFunction;
  38. import org.opengis.metadata.citation.OnlineResource;
  39. import org.opengis.metadata.citation.PresentationForm;
  40. import org.opengis.metadata.citation.ResponsibleParty;
  41. import org.opengis.metadata.citation.Role;
  42. import org.opengis.metadata.citation.Series;
  43. import org.opengis.metadata.constraint.Constraints;
  44. import org.opengis.metadata.distribution.Format;
  45. import org.opengis.metadata.extent.Extent;
  46. import org.opengis.metadata.extent.GeographicExtent;
  47. import org.opengis.metadata.identification.AggregateInformation;
  48. import org.opengis.metadata.identification.BrowseGraphic;
  49. import org.opengis.metadata.identification.CharacterSet;
  50. import org.opengis.metadata.identification.DataIdentification;
  51. import org.opengis.metadata.identification.KeywordType;
  52. import org.opengis.metadata.identification.Keywords;
  53. import org.opengis.metadata.identification.Progress;
  54. import org.opengis.metadata.identification.RepresentativeFraction;
  55. import org.opengis.metadata.identification.Resolution;
  56. import org.opengis.metadata.identification.TopicCategory;
  57. import org.opengis.metadata.identification.Usage;
  58. import org.opengis.metadata.maintenance.MaintenanceInformation;
  59. import org.opengis.metadata.maintenance.ScopeCode;
  60. import org.opengis.metadata.spatial.SpatialRepresentationType;
  61. import org.opengis.util.InternationalString;
  62. import org.springframework.stereotype.Component;
  63.  
  64. import fr.univNantes.geodoc.app.portailmel.model.entities.RecordPortailMEL;
  65. import fr.univNantes.geodoc.model.entities.MotCle;
  66. import fr.univNantes.geodoc.model.entities.Place;
  67. import fr.univNantes.geodoc.services.inner.MetadataConverter;
  68.  
  69. @Component
  70. public class MetadataConverterPortailMEL implements MetadataConverter<RecordPortailMEL> {
  71.     private String host = "localhost:8080";
  72.  
  73.     private String thumbnailUrlStringFormat
  74.         = "http://%s/rest/fiches/%s/document/thumbnail";
  75.  
  76.     private String documentUrlStringFormat
  77.         = "http://%s/rest/fiches/%s/document/data";
  78.  
  79.     @Override
  80.     @SuppressWarnings("serial")
  81.     public Metadata recordToMetadata(final RecordPortailMEL record) {
  82.         // XXX assert(record.getPlaces().size() > 0);
  83.  
  84.         DefaultMetadata metadata = new DefaultMetadata();
  85.  
  86.         metadata.setFileIdentifier(record.getUUIDMetadata());
  87.         metadata.setLanguage(Locale.ENGLISH);
  88.         metadata.setCharacterSet(CharacterSet.UTF_8);
  89.         metadata.setDateStamp(new Date());
  90.  
  91.         metadata.setHierarchyLevels(
  92.             new ArrayList<ScopeCode>() {{ this.add(ScopeCode.DATASET); }}
  93.         );
  94.  
  95.         metadata.getContacts().add(new ResponsibleParty() {
  96.             @Override
  97.             public Role getRole() {
  98.                 return null;
  99.             }
  100.  
  101.             @Override
  102.             public InternationalString getPositionName() {
  103.                 return null;
  104.             }
  105.  
  106.             @Override
  107.             public InternationalString getOrganisationName() {
  108.                 return buildI18nString(Locale.FRENCH, record.getOrganismes());
  109.             }
  110.  
  111.             @Override
  112.             public String getIndividualName() {
  113.                 return record.getAuteurs();
  114.             }
  115.  
  116.             @Override
  117.             public Contact getContactInfo() {
  118.                 try {
  119.                     if (record.getLien() != null) {
  120.                         OnlineResource onlineResource
  121.                             = new DefaultOnlineResource(new URI(record.getLien()));
  122.  
  123.                         Contact contact = new DefaultContact(onlineResource);
  124.  
  125.                         return contact;
  126.                     }
  127.                 } catch (URISyntaxException e) { }
  128.  
  129.                 return null;
  130.             }
  131.         });
  132.  
  133.         metadata.getIdentificationInfo().add(
  134.             new DataIdentification() {
  135.                 @Override
  136.                 public Collection<Progress> getStatus() {
  137.                     return null;
  138.                 }
  139.  
  140.                 @Override
  141.                 public Collection<? extends Usage> getResourceSpecificUsages() {
  142.                     return null;
  143.                 }
  144.  
  145.                 @Override
  146.                 public Collection<? extends MaintenanceInformation> getResourceMaintenances() {
  147.                     return null;
  148.                 }
  149.  
  150.                 @Override
  151.                 public Collection<? extends Format> getResourceFormats() {
  152.                     return null;
  153.                 }
  154.  
  155.                 @Override
  156.                 public Collection<? extends Constraints> getResourceConstraints() {
  157.                     return null;
  158.                 }
  159.  
  160.                 @Override
  161.                 public InternationalString getPurpose() {
  162.                     return null;
  163.                 }
  164.  
  165.                 @Override
  166.                 public Collection<? extends ResponsibleParty> getPointOfContacts() {
  167.                     List<ResponsibleParty> parties = new ArrayList<ResponsibleParty>();
  168.  
  169.                     parties.add(new DefaultResponsibleParty(Role.PUBLISHER));
  170.  
  171.                     return parties;
  172.                 }
  173.  
  174.                 @Override
  175.                 public Collection<? extends BrowseGraphic> getGraphicOverviews() {
  176.                     List<BrowseGraphic> graphics = new ArrayList<BrowseGraphic>();
  177.  
  178.                     DefaultBrowseGraphic graphic = new DefaultBrowseGraphic();
  179.  
  180.                     Map<Locale, String> description = new HashMap<Locale, String>();
  181.                     description.put(Locale.FRENCH, "vignette");
  182.                     description.put(Locale.ENGLISH, "thumbnail");
  183.  
  184.                     graphic.setFileDescription(new DefaultInternationalString(description));
  185.                     try {
  186.                         URI uri = new URI(String.format(thumbnailUrlStringFormat, host, record.getId().toString()));
  187.                         graphic.setFileName(uri);
  188.                     } catch (URISyntaxException e) {
  189.                         // TODO
  190.                     }
  191.  
  192.                     graphics.add(graphic);
  193.  
  194.                     return graphics;
  195.                 }
  196.  
  197.                 @Override
  198.                 public Collection<? extends Keywords> getDescriptiveKeywords() {
  199.                     ArrayList<Keywords> keywords = new ArrayList<Keywords>();
  200.  
  201.                     keywords.add(new Keywords() {
  202.                         @Override
  203.                         public KeywordType getType() {
  204.                             return KeywordType.THEME;
  205.                         }
  206.  
  207.                         @Override
  208.                         public Citation getThesaurusName() {
  209.                             return null;
  210.                         }
  211.  
  212.                         @Override
  213.                         public Collection<? extends InternationalString> getKeywords() {
  214.                             ArrayList<InternationalString> keywords =
  215.                                 new ArrayList<InternationalString>();
  216.  
  217.                             for (final MotCle motCle : record.getMotsCles()) {
  218.                                 Map<Locale, String> keyword = new HashMap<Locale, String>();
  219.                                 keyword.put(Locale.FRENCH, motCle.getLibelle() + " (fr)");
  220.                                 keyword.put(Locale.ENGLISH, motCle.getLibelle() + " (en)");
  221.  
  222.                                 keywords.add(new DefaultInternationalString(keyword));
  223.                             }
  224.  
  225.                             return keywords;
  226.                         }
  227.                     });
  228.  
  229.                     return keywords;
  230.                 }
  231.  
  232.                 @Override
  233.                 public Collection<String> getCredits() {
  234.                     return null;
  235.                 }
  236.  
  237.                 @Override
  238.                 public Citation getCitation() {
  239.                     return new Citation() {
  240.                         @Override
  241.                         public InternationalString getTitle() {
  242.                             return buildI18nString(Locale.FRENCH, record.getTitre());
  243.                         }
  244.  
  245.                         @Override
  246.                         public Series getSeries() {
  247.                             return null;
  248.                         }
  249.  
  250.                         @Override
  251.                         public Collection<PresentationForm> getPresentationForms() {
  252.                             List<PresentationForm> presentationForms
  253.                                 = new ArrayList<PresentationForm>();
  254.  
  255.                             // XXX
  256.                             presentationForms.add(PresentationForm.DOCUMENT_DIGITAL);
  257.  
  258.                             return presentationForms;
  259.                         }
  260.  
  261.                         @Override
  262.                         public InternationalString getOtherCitationDetails() {
  263.                             return null;
  264.                         }
  265.  
  266.                         @Override
  267.                         public Collection<? extends Identifier> getIdentifiers() {
  268.                             List<Identifier> identifiers = new ArrayList<Identifier>();
  269.  
  270.                             identifiers.add(new DefaultIdentifier(record.getUUIDMetadata()));
  271.  
  272.                             return identifiers;
  273.                         }
  274.  
  275.                         @Override
  276.                         public String getISSN() {
  277.                             return null;
  278.                         }
  279.  
  280.                         @Override
  281.                         public String getISBN() {
  282.                             return null;
  283.                         }
  284.  
  285.                         @Override
  286.                         public Date getEditionDate() {
  287.                             return null;
  288.                         }
  289.  
  290.                         @Override
  291.                         public InternationalString getEdition() {
  292.                             return null;
  293.                         }
  294.  
  295.                         @Override
  296.                         public Collection<? extends CitationDate> getDates() {
  297.                             List<CitationDate> dates = new ArrayList<CitationDate>();
  298.  
  299.                             dates.add(new DefaultCitationDate(
  300.                                 record.getDateDerniereModification(), DateType.PUBLICATION
  301.                             ));
  302.  
  303.                             /*
  304.                             dates.add(new DefaultCitationDate(
  305.                                 fiche.getDateOrigine(), DateType.CREATION
  306.                             ));
  307.                             */
  308.  
  309.                             return dates;
  310.                         }
  311.  
  312.                         @Override
  313.                         public InternationalString getCollectiveTitle() {
  314.                             return null;
  315.                         }
  316.  
  317.                         @Override
  318.                         public Collection<? extends ResponsibleParty> getCitedResponsibleParties() {
  319.                             return null;
  320.                         }
  321.  
  322.                         @Override
  323.                         public Collection<? extends InternationalString> getAlternateTitles() {
  324.                             return null;
  325.                         }
  326.                     };
  327.                 }
  328.  
  329.                 @Override
  330.                 public Collection<? extends AggregateInformation> getAggregationInfo() {
  331.                     return null;
  332.                 }
  333.  
  334.                 @Override
  335.                 public InternationalString getAbstract() {
  336.                     return buildI18nString(Locale.FRENCH, record.getDescription());
  337.                 }
  338.  
  339.                 @Override
  340.                 public Collection<CharacterSet> getCharacterSets() {
  341.                     return null;
  342.                 }
  343.  
  344.                 @Override
  345.                 public InternationalString getEnvironmentDescription() {
  346.                     return null;
  347.                 }
  348.  
  349.                 @Override
  350.                 public Collection<? extends Extent> getExtents() {
  351.                     List<GeographicExtent> geographicExtents =
  352.                         new ArrayList<GeographicExtent>();
  353.  
  354.                     Set<String> placesIds = new HashSet<String>();
  355.  
  356.                     Rectangle2D wholeBoundingBox = null;
  357.  
  358.                     for (Place place : record.getPlaces()) {
  359.                         Place currentPlace = place;
  360.                         do {
  361.                             placesIds.add(currentPlace.id.toString());
  362.                             currentPlace = currentPlace.parentPlace;
  363.                         } while (currentPlace != null);
  364.  
  365.                         if (place.bounds != null) {
  366.                             Rectangle2D placeBoundingBox = new Rectangle2D.Double(
  367.                                 place.bounds.minX,
  368.                                 place.bounds.minY,
  369.                                 place.bounds.maxX - place.bounds.minX,
  370.                                 place.bounds.maxY - place.bounds.minY
  371.                             );
  372.  
  373.                             if (wholeBoundingBox == null) {
  374.                                 wholeBoundingBox = placeBoundingBox;
  375.                             } else {
  376.                                 Rectangle2D.union(wholeBoundingBox, placeBoundingBox, wholeBoundingBox);
  377.                             }
  378.                         }
  379.                     }
  380.  
  381.                     if (wholeBoundingBox != null) {
  382.                         geographicExtents.add(
  383.                             new DefaultGeographicBoundingBox(wholeBoundingBox)
  384.                         );
  385.                     }
  386.  
  387.                     for (String placeId : placesIds) {
  388.                         Identifier identifier = new SimpleReferenceIdentifier(
  389.                             "http://portailmel.univ-nantes.fr/", placeId
  390.                         );
  391.  
  392.                         geographicExtents.add(new DefaultGeographicDescription(identifier));
  393.                     }
  394.  
  395.                     if (geographicExtents.size() > 0) {
  396.                         DefaultExtent extent = new DefaultExtent();
  397.  
  398.                         extent.setGeographicElements(geographicExtents);
  399.  
  400.                         List<Extent> extents = new ArrayList<Extent>();
  401.                         extents.add(extent);
  402.  
  403.                         return extents;
  404.                     }
  405.  
  406.                     return null;
  407.                 }
  408.  
  409.                 @Override
  410.                 public Collection<Locale> getLanguages() {
  411.                     return null;
  412.                 }
  413.  
  414.                 @Override
  415.                 public Collection<SpatialRepresentationType> getSpatialRepresentationTypes() {
  416.                     Collection<SpatialRepresentationType> types =
  417.                         new ArrayList<SpatialRepresentationType>();
  418.  
  419.                     types.add(SpatialRepresentationType.GRID);
  420.  
  421.                     return types;
  422.                 }
  423.  
  424.                 @Override
  425.                 public Collection<? extends Resolution> getSpatialResolutions() {
  426.                     List<Resolution> resolutions = new ArrayList<Resolution>();
  427.  
  428.                     if (record.getEchelle() != null) {
  429.                         Resolution resolution = new Resolution() {
  430.                             @Override
  431.                             public RepresentativeFraction getEquivalentScale() {
  432.                                 return new DefaultRepresentativeFraction(record.getEchelle());
  433.                             }
  434.  
  435.                             @Override
  436.                             public Double getDistance() {
  437.                                 return null;
  438.                             }
  439.                         };
  440.  
  441.                         resolutions.add(resolution);
  442.  
  443.                     }
  444.  
  445.                     return resolutions;
  446.                 }
  447.  
  448.                 @Override
  449.                 public InternationalString getSupplementalInformation() {
  450.                     return null;
  451.                 }
  452.  
  453.                 @Override
  454.                 public Collection<TopicCategory> getTopicCategories() {
  455.                     return null;
  456.                 }
  457.             }
  458.         );
  459.  
  460.         URI uri = null;
  461.  
  462.         try {
  463.             uri = new URI(String.format(documentUrlStringFormat, host, record.getId().toString()));
  464.         } catch (URISyntaxException e) {
  465.             // TODO
  466.         }
  467.  
  468.         DefaultDistribution distribution = new DefaultDistribution();
  469.  
  470.         DefaultDigitalTransferOptions transferOptions = new DefaultDigitalTransferOptions();
  471.         transferOptions.setTransferSize(this.round((double)record.getDocument().getTaille() / (1024 * 1024), 2));
  472.  
  473.         DefaultOnlineResource onlineResource = new DefaultOnlineResource();
  474.         onlineResource.setLinkage(uri);
  475.         onlineResource.setProtocol("WWW:LINK-1.0-http--link");
  476.         onlineResource.setName(record.getDocument().getNom());
  477.         onlineResource.setFunction(OnLineFunction.DOWNLOAD);
  478.  
  479.         transferOptions.getOnLines().add(onlineResource);
  480.         distribution.getTransferOptions().add(transferOptions);
  481.  
  482.         metadata.setDistributionInfo(distribution);
  483.  
  484.         // metadata.getReferenceSystemInfo().add(DefaultGeographicCRS.WGS84);
  485.  
  486.         return metadata;
  487.     }
  488.  
  489.     private InternationalString buildI18nString(Locale locale, String text) {
  490.         if (text == null) {
  491.             return null;
  492.         }
  493.  
  494.         Map<Locale, String> map = new HashMap<Locale, String>();
  495.         map.put(locale, text);
  496.  
  497.         return new DefaultInternationalString(map);
  498.     }
  499.  
  500.     private double round(double value, int n) {
  501.         return (Math.round(value * Math.pow(10, n))) / Math.pow(10, n);
  502.     }
  503. }
  504.  
  505.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement