Search in sources :

Example 6 with DublinCoreCatalog

use of org.opencastproject.metadata.dublincore.DublinCoreCatalog in project opencast by opencast.

the class SchedulerMessageReceiverImplTest method testUpdateCreator.

@Test
public void testUpdateCreator() throws Exception {
    DublinCoreCatalog catalog = DublinCores.read(getClass().getResourceAsStream("/dublincore.xml"));
    SchedulerItem schedulerItem = SchedulerItem.updateCatalog("uuid", catalog);
    // Test initial set of creator
    scheduler.execute(schedulerItem);
    Event event = index.getEventResult();
    assertNotNull(event);
    assertEquals("Current user is expected to be creator as no other creator has been set explicitly", "Creator", event.getCreator());
    // Test updating creator
    event.setCreator("Hans");
    index.setInitialEvent(event);
    scheduler.execute(schedulerItem);
    event = index.getEventResult();
    assertNotNull(event);
    assertEquals("Creator has been updated", "Hans", event.getCreator());
}
Also used : SchedulerItem(org.opencastproject.message.broker.api.scheduler.SchedulerItem) Event(org.opencastproject.index.service.impl.index.event.Event) DublinCoreCatalog(org.opencastproject.metadata.dublincore.DublinCoreCatalog) Test(org.junit.Test)

Example 7 with DublinCoreCatalog

use of org.opencastproject.metadata.dublincore.DublinCoreCatalog in project opencast by opencast.

the class DublinCoreCatalogUIAdapterTest method testUpdateDublincoreCatalog.

@Test
public void testUpdateDublincoreCatalog() throws IOException, URISyntaxException {
    FileInputStream fis = new FileInputStream(new File(getClass().getResource("/catalog-adapter/start-date-time-duration.xml").toURI()));
    DublinCoreCatalog catalog = DublinCores.read(fis);
    MetadataField<String> startDate = MetadataField.createTemporalStartDateMetadata(TEMPORAL_DUBLIN_CORE_KEY, Opt.some("startDate"), "START_DATE_LABEL", false, false, "yyyy-MM-dd", Opt.<Integer>none(), Opt.<String>none());
    DublinCoreMetadataCollection metadata = new DublinCoreMetadataCollection();
    metadata.addField(startDate, "2014-11-01", listProvidersService);
    DublinCoreMetadataUtil.updateDublincoreCatalog(catalog, metadata);
    logger.info("Catalog:" + catalog.toXmlString());
}
Also used : DublinCoreCatalog(org.opencastproject.metadata.dublincore.DublinCoreCatalog) File(java.io.File) FileInputStream(java.io.FileInputStream) Test(org.junit.Test)

Example 8 with DublinCoreCatalog

use of org.opencastproject.metadata.dublincore.DublinCoreCatalog in project opencast by opencast.

the class SeriesWorkflowOperationHandlerTest method testExtraMetadata.

@Test
public void testExtraMetadata() throws WorkflowOperationException {
    final EName otherProperty = new EName(DublinCore.TERMS_NS_URI, "my-custom-property");
    final String otherValue = "foobar";
    // Add extra metadata to the series catalog.
    seriesCatalog.set(DublinCore.PROPERTY_LANGUAGE, "Opencastian");
    seriesCatalog.set(otherProperty, otherValue);
    seriesCatalog.set(DublinCore.PROPERTY_CONTRIBUTOR, Arrays.asList(new DublinCoreValue[] { DublinCoreValue.mk("Mr. Contry Bute"), DublinCoreValue.mk("Mrs. Jane Doe") }));
    // Prepare "copy metadata" property
    String[] extraMetadata = { // Append a full metadata field, with NS
    DublinCore.PROPERTY_LANGUAGE.toString(), // Field without namespace
    DublinCore.PROPERTY_CONTRIBUTOR.getLocalName(), // Field with a namespace different than the default
    otherProperty.toString(), // Field that does not exist in the series catalog
    "does-not-exist" };
    WorkflowInstanceImpl instance = new WorkflowInstanceImpl();
    List<WorkflowOperationInstance> ops = new ArrayList<WorkflowOperationInstance>();
    WorkflowOperationInstanceImpl operation = new WorkflowOperationInstanceImpl("test", OperationState.INSTANTIATED);
    ops.add(operation);
    instance.setOperations(ops);
    instance.setMediaPackage(mp);
    MediaPackage clone = (MediaPackage) mp.clone();
    operation.setConfiguration(SeriesWorkflowOperationHandler.SERIES_PROPERTY, "series1");
    operation.setConfiguration(SeriesWorkflowOperationHandler.ATTACH_PROPERTY, "*");
    operation.setConfiguration(SeriesWorkflowOperationHandler.APPLY_ACL_PROPERTY, "false");
    operation.setConfiguration(SeriesWorkflowOperationHandler.COPY_METADATA_PROPERTY, StringUtils.join(extraMetadata, ", "));
    WorkflowOperationResult result = operationHandler.start(instance, null);
    Assert.assertEquals(Action.CONTINUE, result.getAction());
    MediaPackage resultingMediapackage = result.getMediaPackage();
    // Get episode DublinCore
    DublinCoreCatalog episodeCatalog = DublinCores.read(capturedStream.getValue());
    Assert.assertEquals("series1", resultingMediapackage.getSeries());
    Assert.assertEquals("Series 1", resultingMediapackage.getSeriesTitle());
    Assert.assertEquals(clone.getElements().length + 1, resultingMediapackage.getElements().length);
    // Check the extra metadata were copied into the dublincore (only those present in the series catalog)
    Assert.assertTrue(episodeCatalog.hasValue(DublinCore.PROPERTY_CONTRIBUTOR));
    Assert.assertEquals(seriesCatalog.get(DublinCore.PROPERTY_CONTRIBUTOR), episodeCatalog.get(DublinCore.PROPERTY_CONTRIBUTOR));
    Assert.assertTrue(episodeCatalog.hasValue(DublinCore.PROPERTY_LANGUAGE));
    Assert.assertEquals(seriesCatalog.get(DublinCore.PROPERTY_LANGUAGE), episodeCatalog.get(DublinCore.PROPERTY_LANGUAGE));
    Assert.assertTrue(episodeCatalog.hasValue(otherProperty));
    Assert.assertEquals(seriesCatalog.get(otherProperty), episodeCatalog.get(otherProperty));
    Assert.assertFalse(episodeCatalog.hasValue(new EName(DublinCore.TERMS_NS_URI, "does-not-exist")));
}
Also used : EName(org.opencastproject.mediapackage.EName) DublinCoreValue(org.opencastproject.metadata.dublincore.DublinCoreValue) WorkflowInstanceImpl(org.opencastproject.workflow.api.WorkflowInstanceImpl) WorkflowOperationInstance(org.opencastproject.workflow.api.WorkflowOperationInstance) ArrayList(java.util.ArrayList) MediaPackage(org.opencastproject.mediapackage.MediaPackage) WorkflowOperationInstanceImpl(org.opencastproject.workflow.api.WorkflowOperationInstanceImpl) DublinCoreCatalog(org.opencastproject.metadata.dublincore.DublinCoreCatalog) WorkflowOperationResult(org.opencastproject.workflow.api.WorkflowOperationResult) Test(org.junit.Test)

Example 9 with DublinCoreCatalog

use of org.opencastproject.metadata.dublincore.DublinCoreCatalog in project opencast by opencast.

the class IndexServiceImpl method createSeries.

@Override
public String createSeries(String metadata) throws IllegalArgumentException, IndexServiceException, UnauthorizedException {
    JSONObject metadataJson = null;
    try {
        metadataJson = (JSONObject) new JSONParser().parse(metadata);
    } catch (Exception e) {
        logger.warn("Unable to parse metadata {}", metadata);
        throw new IllegalArgumentException("Unable to parse metadata" + metadata);
    }
    if (metadataJson == null)
        throw new IllegalArgumentException("No metadata set to create series");
    JSONArray seriesMetadataJson = (JSONArray) metadataJson.get("metadata");
    if (seriesMetadataJson == null)
        throw new IllegalArgumentException("No metadata field in metadata");
    JSONObject options = (JSONObject) metadataJson.get("options");
    if (options == null)
        throw new IllegalArgumentException("No options field in metadata");
    Opt<Long> themeId = Opt.none();
    Long theme = (Long) metadataJson.get("theme");
    if (theme != null) {
        themeId = Opt.some(theme);
    }
    Map<String, String> optionsMap;
    try {
        optionsMap = JSONUtils.toMap(new org.codehaus.jettison.json.JSONObject(options.toJSONString()));
    } catch (JSONException e) {
        logger.warn("Unable to parse options to map: {}", getStackTrace(e));
        throw new IllegalArgumentException("Unable to parse options to map");
    }
    DublinCoreCatalog dc = DublinCores.mkOpencastSeries().getCatalog();
    dc.set(PROPERTY_IDENTIFIER, UUID.randomUUID().toString());
    dc.set(DublinCore.PROPERTY_CREATED, EncodingSchemeUtils.encodeDate(new Date(), Precision.Second));
    for (Entry<String, String> entry : optionsMap.entrySet()) {
        dc.set(new EName(DublinCores.OC_PROPERTY_NS_URI, entry.getKey()), entry.getValue());
    }
    MetadataList metadataList;
    try {
        metadataList = getMetadataListWithAllSeriesCatalogUIAdapters();
        metadataList.fromJSON(seriesMetadataJson.toJSONString());
    } catch (Exception e) {
        logger.warn("Not able to parse the series metadata {}: {}", seriesMetadataJson, getStackTrace(e));
        throw new IllegalArgumentException("Not able to parse the series metadata");
    }
    Opt<MetadataCollection> seriesMetadata = metadataList.getMetadataByFlavor(MediaPackageElements.SERIES.toString());
    if (seriesMetadata.isSome()) {
        DublinCoreMetadataUtil.updateDublincoreCatalog(dc, seriesMetadata.get());
    }
    AccessControlList acl = getAccessControlList(metadataJson);
    String seriesId;
    try {
        DublinCoreCatalog createdSeries = seriesService.updateSeries(dc);
        seriesId = createdSeries.getFirst(PROPERTY_IDENTIFIER);
        seriesService.updateAccessControl(seriesId, acl);
        for (Long id : themeId) seriesService.updateSeriesProperty(seriesId, THEME_PROPERTY_NAME, Long.toString(id));
    } catch (Exception e) {
        logger.error("Unable to create new series: {}", getStackTrace(e));
        throw new IndexServiceException("Unable to create new series");
    }
    updateSeriesMetadata(seriesId, metadataList);
    return seriesId;
}
Also used : AccessControlList(org.opencastproject.security.api.AccessControlList) EName(org.opencastproject.mediapackage.EName) JSONArray(org.json.simple.JSONArray) JSONException(org.codehaus.jettison.json.JSONException) SchedulerException(org.opencastproject.scheduler.api.SchedulerException) IngestException(org.opencastproject.ingest.api.IngestException) WebApplicationException(javax.ws.rs.WebApplicationException) MetadataParsingException(org.opencastproject.metadata.dublincore.MetadataParsingException) EventCommentException(org.opencastproject.event.comment.EventCommentException) IOException(java.io.IOException) JSONException(org.codehaus.jettison.json.JSONException) SearchIndexException(org.opencastproject.matterhorn.search.SearchIndexException) ParseException(java.text.ParseException) SeriesException(org.opencastproject.series.api.SeriesException) WorkflowException(org.opencastproject.workflow.api.WorkflowException) MediaPackageException(org.opencastproject.mediapackage.MediaPackageException) IndexServiceException(org.opencastproject.index.service.exception.IndexServiceException) UnauthorizedException(org.opencastproject.security.api.UnauthorizedException) NotFoundException(org.opencastproject.util.NotFoundException) WorkflowDatabaseException(org.opencastproject.workflow.api.WorkflowDatabaseException) AssetManagerException(org.opencastproject.assetmanager.api.AssetManagerException) Date(java.util.Date) MetadataList(org.opencastproject.index.service.catalog.adapter.MetadataList) JSONObject(org.json.simple.JSONObject) JSONParser(org.json.simple.parser.JSONParser) MetadataCollection(org.opencastproject.metadata.dublincore.MetadataCollection) DublinCoreCatalog(org.opencastproject.metadata.dublincore.DublinCoreCatalog) IndexServiceException(org.opencastproject.index.service.exception.IndexServiceException)

Example 10 with DublinCoreCatalog

use of org.opencastproject.metadata.dublincore.DublinCoreCatalog in project opencast by opencast.

the class IndexServiceImpl method updateMediaPackageMetadata.

private void updateMediaPackageMetadata(MediaPackage mp, MetadataList metadataList) {
    String oldSeriesId = mp.getSeries();
    for (EventCatalogUIAdapter catalogUIAdapter : getEventCatalogUIAdapters()) {
        Opt<MetadataCollection> metadata = metadataList.getMetadataByAdapter(catalogUIAdapter);
        if (metadata.isSome() && metadata.get().isUpdated()) {
            catalogUIAdapter.storeFields(mp, metadata.get());
        }
    }
    // update series catalogs
    if (!StringUtils.equals(oldSeriesId, mp.getSeries())) {
        List<String> seriesDcTags = new ArrayList<>();
        List<String> seriesAclTags = new ArrayList<>();
        Map<String, List<String>> seriesExtDcTags = new HashMap<>();
        if (StringUtils.isNotBlank(oldSeriesId)) {
            // remove series dublincore from the media package
            for (MediaPackageElement mpe : mp.getElementsByFlavor(MediaPackageElements.SERIES)) {
                mp.remove(mpe);
                for (String tag : mpe.getTags()) {
                    seriesDcTags.add(tag);
                }
            }
            // remove series ACL from the media package
            for (MediaPackageElement mpe : mp.getElementsByFlavor(MediaPackageElements.XACML_POLICY_SERIES)) {
                mp.remove(mpe);
                for (String tag : mpe.getTags()) {
                    seriesAclTags.add(tag);
                }
            }
            // remove series extended metadata from the media package
            try {
                Opt<Map<String, byte[]>> oldSeriesElementsOpt = seriesService.getSeriesElements(oldSeriesId);
                for (Map<String, byte[]> oldSeriesElements : oldSeriesElementsOpt) {
                    for (String oldSeriesElementType : oldSeriesElements.keySet()) {
                        for (MediaPackageElement mpe : mp.getElementsByFlavor(MediaPackageElementFlavor.flavor(oldSeriesElementType, "series"))) {
                            mp.remove(mpe);
                            String elementType = mpe.getFlavor().getType();
                            if (StringUtils.isNotBlank(elementType)) {
                                // remember the tags for this type of element
                                if (!seriesExtDcTags.containsKey(elementType)) {
                                    // initialize the tags list on the first occurrence of this element type
                                    seriesExtDcTags.put(elementType, new ArrayList<>());
                                }
                                for (String tag : mpe.getTags()) {
                                    seriesExtDcTags.get(elementType).add(tag);
                                }
                            }
                        }
                    }
                }
            } catch (SeriesException e) {
                logger.info("Unable to retrieve series element types from series service for the series {}", oldSeriesId, e);
            }
        }
        if (StringUtils.isNotBlank(mp.getSeries())) {
            // add updated series dublincore to the media package
            try {
                DublinCoreCatalog seriesDC = seriesService.getSeries(mp.getSeries());
                if (seriesDC != null) {
                    mp.setSeriesTitle(seriesDC.getFirst(DublinCore.PROPERTY_TITLE));
                    try (InputStream in = IOUtils.toInputStream(seriesDC.toXmlString(), "UTF-8")) {
                        String elementId = UUID.randomUUID().toString();
                        URI catalogUrl = workspace.put(mp.getIdentifier().compact(), elementId, "dublincore.xml", in);
                        MediaPackageElement mpe = mp.add(catalogUrl, MediaPackageElement.Type.Catalog, MediaPackageElements.SERIES);
                        mpe.setIdentifier(elementId);
                        mpe.setChecksum(Checksum.create(ChecksumType.DEFAULT_TYPE, workspace.read(catalogUrl)));
                        if (StringUtils.isNotBlank(oldSeriesId)) {
                            for (String tag : seriesDcTags) {
                                mpe.addTag(tag);
                            }
                        } else {
                            // add archive tag to the element if the media package had no series set before
                            mpe.addTag("archive");
                        }
                    } catch (IOException e) {
                        throw new IllegalStateException("Unable to add the series dublincore to the media package " + mp.getIdentifier(), e);
                    }
                }
            } catch (SeriesException e) {
                throw new IllegalStateException("Unable to retrieve series dublincore catalog for the series " + mp.getSeries(), e);
            } catch (NotFoundException | UnauthorizedException e) {
                throw new IllegalArgumentException("Unable to retrieve series dublincore catalog for the series " + mp.getSeries(), e);
            }
            // add updated series ACL to the media package
            try {
                AccessControlList seriesAccessControl = seriesService.getSeriesAccessControl(mp.getSeries());
                if (seriesAccessControl != null) {
                    mp = authorizationService.setAcl(mp, AclScope.Series, seriesAccessControl).getA();
                    for (MediaPackageElement seriesAclMpe : mp.getElementsByFlavor(MediaPackageElements.XACML_POLICY_SERIES)) {
                        if (StringUtils.isNotBlank(oldSeriesId)) {
                            for (String tag : seriesAclTags) {
                                seriesAclMpe.addTag(tag);
                            }
                        } else {
                            // add archive tag to the element if the media package had no series set before
                            seriesAclMpe.addTag("archive");
                        }
                    }
                }
            } catch (SeriesException e) {
                throw new IllegalStateException("Unable to retrieve series ACL for series " + oldSeriesId, e);
            } catch (NotFoundException e) {
                logger.debug("There is no ACL set for the series {}", mp.getSeries());
            }
            // add updated series extended metadata to the media package
            try {
                Opt<Map<String, byte[]>> seriesElementsOpt = seriesService.getSeriesElements(mp.getSeries());
                for (Map<String, byte[]> seriesElements : seriesElementsOpt) {
                    for (String seriesElementType : seriesElements.keySet()) {
                        try (InputStream in = new ByteArrayInputStream(seriesElements.get(seriesElementType))) {
                            String elementId = UUID.randomUUID().toString();
                            URI catalogUrl = workspace.put(mp.getIdentifier().compact(), elementId, "dublincore.xml", in);
                            MediaPackageElement mpe = mp.add(catalogUrl, MediaPackageElement.Type.Catalog, MediaPackageElementFlavor.flavor(seriesElementType, "series"));
                            mpe.setIdentifier(elementId);
                            mpe.setChecksum(Checksum.create(ChecksumType.DEFAULT_TYPE, workspace.read(catalogUrl)));
                            if (StringUtils.isNotBlank(oldSeriesId)) {
                                if (seriesExtDcTags.containsKey(seriesElementType)) {
                                    for (String tag : seriesExtDcTags.get(seriesElementType)) {
                                        mpe.addTag(tag);
                                    }
                                }
                            } else {
                                // add archive tag to the element if the media package had no series set before
                                mpe.addTag("archive");
                            }
                        } catch (IOException e) {
                            throw new IllegalStateException(String.format("Unable to serialize series element %s for the series %s", seriesElementType, mp.getSeries()), e);
                        } catch (NotFoundException e) {
                            throw new IllegalArgumentException("Unable to retrieve series element dublincore catalog for the series " + mp.getSeries(), e);
                        }
                    }
                }
            } catch (SeriesException e) {
                throw new IllegalStateException("Unable to retrieve series elements for the series " + mp.getSeries(), e);
            }
        }
    }
}
Also used : AccessControlList(org.opencastproject.security.api.AccessControlList) LinkedHashMap(java.util.LinkedHashMap) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) NotFoundException(org.opencastproject.util.NotFoundException) URI(java.net.URI) MediaPackageElement(org.opencastproject.mediapackage.MediaPackageElement) CommonEventCatalogUIAdapter(org.opencastproject.index.service.catalog.adapter.events.CommonEventCatalogUIAdapter) EventCatalogUIAdapter(org.opencastproject.metadata.dublincore.EventCatalogUIAdapter) UnauthorizedException(org.opencastproject.security.api.UnauthorizedException) MetadataCollection(org.opencastproject.metadata.dublincore.MetadataCollection) MetadataList(org.opencastproject.index.service.catalog.adapter.MetadataList) ArrayList(java.util.ArrayList) AccessControlList(org.opencastproject.security.api.AccessControlList) List(java.util.List) LinkedList(java.util.LinkedList) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) SeriesException(org.opencastproject.series.api.SeriesException) IOException(java.io.IOException) ByteArrayInputStream(java.io.ByteArrayInputStream) DublinCoreCatalog(org.opencastproject.metadata.dublincore.DublinCoreCatalog) Map(java.util.Map) LinkedHashMap(java.util.LinkedHashMap) HashMap(java.util.HashMap)

Aggregations

DublinCoreCatalog (org.opencastproject.metadata.dublincore.DublinCoreCatalog)117 MediaPackage (org.opencastproject.mediapackage.MediaPackage)51 NotFoundException (org.opencastproject.util.NotFoundException)46 Test (org.junit.Test)44 Date (java.util.Date)40 IOException (java.io.IOException)30 UnauthorizedException (org.opencastproject.security.api.UnauthorizedException)29 URI (java.net.URI)23 AccessControlList (org.opencastproject.security.api.AccessControlList)22 SeriesException (org.opencastproject.series.api.SeriesException)20 ArrayList (java.util.ArrayList)18 Catalog (org.opencastproject.mediapackage.Catalog)18 SchedulerException (org.opencastproject.scheduler.api.SchedulerException)18 EName (org.opencastproject.mediapackage.EName)17 MediaPackageException (org.opencastproject.mediapackage.MediaPackageException)17 HashMap (java.util.HashMap)16 HashSet (java.util.HashSet)15 SchedulerConflictException (org.opencastproject.scheduler.api.SchedulerConflictException)15 InputStream (java.io.InputStream)14 AQueryBuilder (org.opencastproject.assetmanager.api.query.AQueryBuilder)14