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;
}
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());
}
}
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;
}
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);
}
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)));
}
}
Aggregations