Search in sources :

Example 1 with MetadataList

use of org.opencastproject.index.service.catalog.adapter.MetadataList 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 2 with MetadataList

use of org.opencastproject.index.service.catalog.adapter.MetadataList 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)

Example 3 with MetadataList

use of org.opencastproject.index.service.catalog.adapter.MetadataList in project opencast by opencast.

the class IndexServiceImpl method createEvent.

@Override
public String createEvent(JSONObject metadataJson, MediaPackage mp) throws ParseException, IOException, MediaPackageException, IngestException, NotFoundException, SchedulerException, UnauthorizedException {
    if (metadataJson == null)
        throw new IllegalArgumentException("No metadata set");
    JSONObject source = (JSONObject) metadataJson.get("source");
    if (source == null)
        throw new IllegalArgumentException("No source field in metadata");
    JSONObject processing = (JSONObject) metadataJson.get("processing");
    if (processing == null)
        throw new IllegalArgumentException("No processing field in metadata");
    JSONArray allEventMetadataJson = (JSONArray) metadataJson.get("metadata");
    if (allEventMetadataJson == null)
        throw new IllegalArgumentException("No metadata field in metadata");
    AccessControlList acl = getAccessControlList(metadataJson);
    MetadataList metadataList = getMetadataListWithAllEventCatalogUIAdapters();
    try {
        metadataList.fromJSON(allEventMetadataJson.toJSONString());
    } catch (MetadataParsingException e) {
        logger.warn("Unable to parse event metadata {}", allEventMetadataJson.toJSONString());
        throw new IllegalArgumentException("Unable to parse metadata set");
    }
    EventHttpServletRequest eventHttpServletRequest = new EventHttpServletRequest();
    eventHttpServletRequest.setAcl(acl);
    eventHttpServletRequest.setMetadataList(metadataList);
    eventHttpServletRequest.setMediaPackage(mp);
    eventHttpServletRequest.setProcessing(processing);
    eventHttpServletRequest.setSource(source);
    return createEvent(eventHttpServletRequest);
}
Also used : AccessControlList(org.opencastproject.security.api.AccessControlList) MetadataList(org.opencastproject.index.service.catalog.adapter.MetadataList) EventHttpServletRequest(org.opencastproject.index.service.impl.index.event.EventHttpServletRequest) MetadataParsingException(org.opencastproject.metadata.dublincore.MetadataParsingException) JSONObject(org.json.simple.JSONObject) JSONArray(org.json.simple.JSONArray)

Example 4 with MetadataList

use of org.opencastproject.index.service.catalog.adapter.MetadataList in project opencast by opencast.

the class IndexServiceImpl method updateAllEventMetadata.

@Override
public MetadataList updateAllEventMetadata(String id, String metadataJSON, AbstractSearchIndex index) throws IllegalArgumentException, IndexServiceException, NotFoundException, SearchIndexException, UnauthorizedException {
    MetadataList metadataList;
    try {
        metadataList = getMetadataListWithAllEventCatalogUIAdapters();
        metadataList.fromJSON(metadataJSON);
    } catch (Exception e) {
        logger.warn("Not able to parse the event metadata {}: {}", metadataJSON, getStackTrace(e));
        throw new IllegalArgumentException("Not able to parse the event metadata " + metadataJSON, e);
    }
    return updateEventMetadata(id, metadataList, index);
}
Also used : MetadataList(org.opencastproject.index.service.catalog.adapter.MetadataList) 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)

Example 5 with MetadataList

use of org.opencastproject.index.service.catalog.adapter.MetadataList in project opencast by opencast.

the class IndexServiceImpl method getMetadataListWithCommonSeriesCatalogUIAdapters.

/**
 * @return A {@link MetadataList} with only the common SeriesCatalogUIAdapter's empty {@link MetadataCollection}
 *         available
 */
private MetadataList getMetadataListWithCommonSeriesCatalogUIAdapters() {
    MetadataList metadataList = new MetadataList();
    metadataList.add(seriesCatalogUIAdapter.getFlavor(), seriesCatalogUIAdapter.getUITitle(), seriesCatalogUIAdapter.getRawFields());
    return metadataList;
}
Also used : MetadataList(org.opencastproject.index.service.catalog.adapter.MetadataList)

Aggregations

MetadataList (org.opencastproject.index.service.catalog.adapter.MetadataList)27 MetadataCollection (org.opencastproject.metadata.dublincore.MetadataCollection)15 NotFoundException (org.opencastproject.util.NotFoundException)10 Path (javax.ws.rs.Path)8 JSONArray (org.json.simple.JSONArray)8 JSONParser (org.json.simple.parser.JSONParser)8 RestQuery (org.opencastproject.util.doc.rest.RestQuery)8 ParseException (org.json.simple.parser.ParseException)7 IOException (java.io.IOException)6 Produces (javax.ws.rs.Produces)6 IndexServiceException (org.opencastproject.index.service.exception.IndexServiceException)6 EventCatalogUIAdapter (org.opencastproject.metadata.dublincore.EventCatalogUIAdapter)6 GET (javax.ws.rs.GET)5 WebApplicationException (javax.ws.rs.WebApplicationException)5 JSONObject (org.json.simple.JSONObject)5 CommonEventCatalogUIAdapter (org.opencastproject.index.service.catalog.adapter.events.CommonEventCatalogUIAdapter)5 IngestException (org.opencastproject.ingest.api.IngestException)5 MediaPackageException (org.opencastproject.mediapackage.MediaPackageException)5 AccessControlList (org.opencastproject.security.api.AccessControlList)5 UnauthorizedException (org.opencastproject.security.api.UnauthorizedException)5