Search in sources :

Example 1 with IndexServiceException

use of org.opencastproject.index.service.exception.IndexServiceException 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 IndexServiceException

use of org.opencastproject.index.service.exception.IndexServiceException in project opencast by opencast.

the class IndexServiceImpl method createEvent.

@Override
public String createEvent(HttpServletRequest request) throws IndexServiceException {
    JSONObject metadataJson = null;
    MediaPackage mp = null;
    // regex for form field name matching an attachment or a catalog
    // The first sub items identifies if the file is an attachment or catalog
    // The second is the item flavor
    // Example form field names:  "catalog/captions/timedtext" and "attachment/captions/vtt"
    // The prefix of field name for attachment and catalog
    List<String> assetList = new LinkedList<String>();
    try {
        if (ServletFileUpload.isMultipartContent(request)) {
            mp = ingestService.createMediaPackage();
            for (FileItemIterator iter = new ServletFileUpload().getItemIterator(request); iter.hasNext(); ) {
                FileItemStream item = iter.next();
                String fieldName = item.getFieldName();
                if (item.isFormField()) {
                    if ("metadata".equals(fieldName)) {
                        String metadata = Streams.asString(item.openStream());
                        try {
                            metadataJson = (JSONObject) new JSONParser().parse(metadata);
                        } catch (Exception e) {
                            logger.warn("Unable to parse metadata {}", metadata);
                            throw new IllegalArgumentException("Unable to parse metadata");
                        }
                    }
                } else {
                    if ("presenter".equals(item.getFieldName())) {
                        mp = ingestService.addTrack(item.openStream(), item.getName(), MediaPackageElements.PRESENTER_SOURCE, mp);
                    } else if ("presentation".equals(item.getFieldName())) {
                        mp = ingestService.addTrack(item.openStream(), item.getName(), MediaPackageElements.PRESENTATION_SOURCE, mp);
                    } else if ("audio".equals(item.getFieldName())) {
                        mp = ingestService.addTrack(item.openStream(), item.getName(), new MediaPackageElementFlavor("presenter-audio", "source"), mp);
                    // For dynamic uploads, cannot get flavor at this point, so saving with temporary flavor
                    } else if (item.getFieldName().toLowerCase().matches(attachmentRegex)) {
                        assetList.add(item.getFieldName());
                        mp = ingestService.addAttachment(item.openStream(), item.getName(), new MediaPackageElementFlavor(item.getFieldName(), "*"), mp);
                    } else if (item.getFieldName().toLowerCase().matches(catalogRegex)) {
                        // Cannot get flavor at this point, so saving with temporary flavor
                        assetList.add(item.getFieldName());
                        mp = ingestService.addCatalog(item.openStream(), item.getName(), new MediaPackageElementFlavor(item.getFieldName(), "*"), mp);
                    } else if (item.getFieldName().toLowerCase().matches(trackRegex)) {
                        // Cannot get flavor at this point, so saving with temporary flavor
                        assetList.add(item.getFieldName());
                        mp = ingestService.addTrack(item.openStream(), item.getName(), new MediaPackageElementFlavor(item.getFieldName(), "*"), mp);
                    } else {
                        logger.warn("Unknown field name found {}", item.getFieldName());
                    }
                }
            }
            // MH-12085 update the flavors of any newly added assets.
            try {
                JSONArray assetMetadata = (JSONArray) ((JSONObject) metadataJson.get("assets")).get("options");
                if (assetMetadata != null) {
                    mp = updateMpAssetFlavor(assetList, mp, assetMetadata, isOverwriteExistingAsset);
                }
            } catch (Exception e) {
                // Assuming a parse error versus a file error and logging the error type
                logger.warn("Unable to process asset metadata {}", metadataJson.get("assets"), e);
                throw new IllegalArgumentException("Unable to parse metadata", e);
            }
        } else {
            throw new IllegalArgumentException("No multipart content");
        }
        // MH-10834 If there is only an audio track, change the flavor from presenter-audio/source to presenter/source.
        if (mp.getTracks().length == 1 && mp.getTracks()[0].getFlavor().equals(new MediaPackageElementFlavor("presenter-audio", "source"))) {
            Track audioTrack = mp.getTracks()[0];
            mp.remove(audioTrack);
            audioTrack.setFlavor(MediaPackageElements.PRESENTER_SOURCE);
            mp.add(audioTrack);
        }
        return createEvent(metadataJson, mp);
    } catch (Exception e) {
        logger.error("Unable to create event: {}", getStackTrace(e));
        throw new IndexServiceException(e.getMessage());
    }
}
Also used : JSONArray(org.json.simple.JSONArray) MediaPackageElementFlavor(org.opencastproject.mediapackage.MediaPackageElementFlavor) LinkedList(java.util.LinkedList) 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) ServletFileUpload(org.apache.commons.fileupload.servlet.ServletFileUpload) JSONObject(org.json.simple.JSONObject) FileItemStream(org.apache.commons.fileupload.FileItemStream) MediaPackage(org.opencastproject.mediapackage.MediaPackage) JSONParser(org.json.simple.parser.JSONParser) FileItemIterator(org.apache.commons.fileupload.FileItemIterator) Track(org.opencastproject.mediapackage.Track) IndexServiceException(org.opencastproject.index.service.exception.IndexServiceException)

Example 3 with IndexServiceException

use of org.opencastproject.index.service.exception.IndexServiceException in project opencast by opencast.

the class IndexServiceImpl method updateEventMetadata.

@Override
public MetadataList updateEventMetadata(String id, MetadataList metadataList, AbstractSearchIndex index) throws IndexServiceException, SearchIndexException, NotFoundException, UnauthorizedException {
    Opt<Event> optEvent = getEvent(id, index);
    if (optEvent.isNone())
        throw new NotFoundException("Cannot find an event with id " + id);
    Event event = optEvent.get();
    MediaPackage mediaPackage = getEventMediapackage(event);
    Opt<Set<String>> presenters = Opt.none();
    Opt<MetadataCollection> eventCatalog = metadataList.getMetadataByAdapter(getCommonEventCatalogUIAdapter());
    if (eventCatalog.isSome()) {
        presenters = updatePresenters(eventCatalog.get());
    }
    updateMediaPackageMetadata(mediaPackage, metadataList);
    switch(getEventSource(event)) {
        case WORKFLOW:
            Opt<WorkflowInstance> workflowInstance = getCurrentWorkflowInstance(event.getIdentifier());
            if (workflowInstance.isNone()) {
                logger.error("No workflow instance for event {} found!", event.getIdentifier());
                throw new IndexServiceException("No workflow instance found for event " + event.getIdentifier());
            }
            try {
                WorkflowInstance instance = workflowInstance.get();
                instance.setMediaPackage(mediaPackage);
                updateWorkflowInstance(instance);
            } catch (WorkflowException e) {
                logger.error("Unable to update workflow event {} with metadata {} because {}", id, RestUtils.getJsonStringSilent(metadataList.toJSON()), getStackTrace(e));
                throw new IndexServiceException("Unable to update workflow event " + id);
            }
            break;
        case ARCHIVE:
            assetManager.takeSnapshot(DEFAULT_OWNER, mediaPackage);
            break;
        case SCHEDULE:
            try {
                schedulerService.updateEvent(id, Opt.<Date>none(), Opt.<Date>none(), Opt.<String>none(), presenters, Opt.some(mediaPackage), Opt.<Map<String, String>>none(), Opt.<Map<String, String>>none(), Opt.<Opt<Boolean>>none(), SchedulerService.ORIGIN);
            } catch (SchedulerException e) {
                logger.error("Unable to update scheduled event {} with metadata {} because {}", id, RestUtils.getJsonStringSilent(metadataList.toJSON()), getStackTrace(e));
                throw new IndexServiceException("Unable to update scheduled event " + id);
            }
            break;
        default:
            logger.error("Unkown event source!");
    }
    return metadataList;
}
Also used : Set(java.util.Set) HashSet(java.util.HashSet) WorkflowSet(org.opencastproject.workflow.api.WorkflowSet) SchedulerException(org.opencastproject.scheduler.api.SchedulerException) WorkflowException(org.opencastproject.workflow.api.WorkflowException) NotFoundException(org.opencastproject.util.NotFoundException) WorkflowInstance(org.opencastproject.workflow.api.WorkflowInstance) MediaPackage(org.opencastproject.mediapackage.MediaPackage) Event(org.opencastproject.index.service.impl.index.event.Event) MetadataCollection(org.opencastproject.metadata.dublincore.MetadataCollection) IndexServiceException(org.opencastproject.index.service.exception.IndexServiceException)

Example 4 with IndexServiceException

use of org.opencastproject.index.service.exception.IndexServiceException 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 IndexServiceException

use of org.opencastproject.index.service.exception.IndexServiceException in project opencast by opencast.

the class IndexServiceImpl method removeCatalogByFlavor.

@Override
public void removeCatalogByFlavor(Event event, MediaPackageElementFlavor flavor) throws IndexServiceException, NotFoundException, UnauthorizedException {
    MediaPackage mediaPackage = getEventMediapackage(event);
    Catalog[] catalogs = mediaPackage.getCatalogs(flavor);
    if (catalogs.length == 0) {
        throw new NotFoundException(String.format("Cannot find a catalog with flavor '%s' for event with id '%s'.", flavor.toString(), event.getIdentifier()));
    }
    for (Catalog catalog : catalogs) {
        mediaPackage.remove(catalog);
    }
    switch(getEventSource(event)) {
        case WORKFLOW:
            Opt<WorkflowInstance> workflowInstance = getCurrentWorkflowInstance(event.getIdentifier());
            if (workflowInstance.isNone()) {
                logger.error("No workflow instance for event {} found!", event.getIdentifier());
                throw new IndexServiceException("No workflow instance found for event " + event.getIdentifier());
            }
            try {
                WorkflowInstance instance = workflowInstance.get();
                instance.setMediaPackage(mediaPackage);
                updateWorkflowInstance(instance);
            } catch (WorkflowException e) {
                logger.error("Unable to remove catalog with flavor {} by updating workflow event {} because {}", flavor, event.getIdentifier(), getStackTrace(e));
                throw new IndexServiceException("Unable to update workflow event " + event.getIdentifier());
            }
            break;
        case ARCHIVE:
            assetManager.takeSnapshot(DEFAULT_OWNER, mediaPackage);
            break;
        case SCHEDULE:
            try {
                schedulerService.updateEvent(event.getIdentifier(), Opt.<Date>none(), Opt.<Date>none(), Opt.<String>none(), Opt.<Set<String>>none(), Opt.some(mediaPackage), Opt.<Map<String, String>>none(), Opt.<Map<String, String>>none(), Opt.<Opt<Boolean>>none(), SchedulerService.ORIGIN);
            } catch (SchedulerException e) {
                logger.error("Unable to remove catalog with flavor {} by updating scheduled event {} because {}", flavor, event.getIdentifier(), getStackTrace(e));
                throw new IndexServiceException("Unable to update scheduled event " + event.getIdentifier());
            }
            break;
        default:
            throw new IndexServiceException(String.format("Unable to handle event source type '%s'", getEventSource(event)));
    }
}
Also used : SchedulerException(org.opencastproject.scheduler.api.SchedulerException) WorkflowException(org.opencastproject.workflow.api.WorkflowException) MediaPackage(org.opencastproject.mediapackage.MediaPackage) NotFoundException(org.opencastproject.util.NotFoundException) WorkflowInstance(org.opencastproject.workflow.api.WorkflowInstance) DublinCoreCatalog(org.opencastproject.metadata.dublincore.DublinCoreCatalog) Catalog(org.opencastproject.mediapackage.Catalog) IndexServiceException(org.opencastproject.index.service.exception.IndexServiceException)

Aggregations

IndexServiceException (org.opencastproject.index.service.exception.IndexServiceException)21 NotFoundException (org.opencastproject.util.NotFoundException)18 SchedulerException (org.opencastproject.scheduler.api.SchedulerException)13 WebApplicationException (javax.ws.rs.WebApplicationException)12 IOException (java.io.IOException)11 SearchIndexException (org.opencastproject.matterhorn.search.SearchIndexException)11 UnauthorizedException (org.opencastproject.security.api.UnauthorizedException)11 WorkflowDatabaseException (org.opencastproject.workflow.api.WorkflowDatabaseException)11 AssetManagerException (org.opencastproject.assetmanager.api.AssetManagerException)9 IngestException (org.opencastproject.ingest.api.IngestException)9 MediaPackageException (org.opencastproject.mediapackage.MediaPackageException)9 WorkflowException (org.opencastproject.workflow.api.WorkflowException)9 ParseException (java.text.ParseException)8 JSONException (org.codehaus.jettison.json.JSONException)8 EventCommentException (org.opencastproject.event.comment.EventCommentException)8 MediaPackage (org.opencastproject.mediapackage.MediaPackage)8 MetadataParsingException (org.opencastproject.metadata.dublincore.MetadataParsingException)7 SeriesException (org.opencastproject.series.api.SeriesException)7 JSONObject (org.json.simple.JSONObject)6 WorkflowInstance (org.opencastproject.workflow.api.WorkflowInstance)6