use of org.opencastproject.util.NotFoundException 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)));
}
}
use of org.opencastproject.util.NotFoundException in project opencast by opencast.
the class IndexServiceImpl method getEventMediapackage.
@Override
public MediaPackage getEventMediapackage(Event event) throws IndexServiceException {
switch(getEventSource(event)) {
case WORKFLOW:
Opt<WorkflowInstance> currentWorkflowInstance = getCurrentWorkflowInstance(event.getIdentifier());
if (currentWorkflowInstance.isNone()) {
logger.error("No workflow instance for event {} found!", event.getIdentifier());
throw new IndexServiceException("No workflow instance found for event " + event.getIdentifier());
}
return currentWorkflowInstance.get().getMediaPackage();
case ARCHIVE:
final AQueryBuilder q = assetManager.createQuery();
final AResult r = q.select(q.snapshot()).where(q.mediaPackageId(event.getIdentifier()).and(q.version().isLatest())).run();
if (r.getSize() > 0) {
logger.debug("Found event in archive with id {}", event.getIdentifier());
return enrich(r).getSnapshots().head2().getMediaPackage();
}
logger.error("No event with id {} found from archive!", event.getIdentifier());
throw new IndexServiceException("No archived event found with id " + event.getIdentifier());
case SCHEDULE:
try {
MediaPackage mediaPackage = schedulerService.getMediaPackage(event.getIdentifier());
logger.debug("Found event in scheduler with id {}", event.getIdentifier());
return mediaPackage;
} catch (NotFoundException e) {
logger.error("No scheduled event with id {} found!", event.getIdentifier());
throw new IndexServiceException(e.getMessage(), e);
} catch (UnauthorizedException e) {
logger.error("Unauthorized to get event with id {} from scheduler because {}", event.getIdentifier(), getStackTrace(e));
throw new IndexServiceException(e.getMessage(), e);
} catch (SchedulerException e) {
logger.error("Unable to get event with id {} from scheduler because {}", event.getIdentifier(), getStackTrace(e));
throw new IndexServiceException(e.getMessage(), e);
}
default:
throw new IllegalStateException("Unknown event type!");
}
}
use of org.opencastproject.util.NotFoundException in project opencast by opencast.
the class EventHttpServletRequest method deserializeMetadataList.
/**
* Change the simplified fields of key values provided to the external api into a {@link MetadataList}.
*
* @param json
* The json string that contains an array of metadata field lists for the different catalogs.
* @param startDatePattern
* The pattern to use to parse the start date from the json payload.
* @param startTimePattern
* The pattern to use to parse the start time from the json payload.
* @return A {@link MetadataList} with the fields populated with the values provided.
* @throws ParseException
* Thrown if unable to parse the json string.
* @throws NotFoundException
* Thrown if unable to find the catalog or field that the json refers to.
*/
protected static MetadataList deserializeMetadataList(String json, List<EventCatalogUIAdapter> catalogAdapters, Opt<String> startDatePattern, Opt<String> startTimePattern) throws ParseException, NotFoundException, java.text.ParseException {
MetadataList metadataList = new MetadataList();
JSONParser parser = new JSONParser();
JSONArray jsonCatalogs = (JSONArray) parser.parse(json);
for (int i = 0; i < jsonCatalogs.size(); i++) {
JSONObject catalog = (JSONObject) jsonCatalogs.get(i);
if (catalog.get("flavor") == null || StringUtils.isBlank(catalog.get("flavor").toString())) {
throw new IllegalArgumentException("Unable to create new event as no flavor was given for one of the metadata collections");
}
String flavorString = catalog.get("flavor").toString();
MediaPackageElementFlavor flavor = MediaPackageElementFlavor.parseFlavor(flavorString);
MetadataCollection collection = null;
EventCatalogUIAdapter adapter = null;
for (EventCatalogUIAdapter eventCatalogUIAdapter : catalogAdapters) {
if (eventCatalogUIAdapter.getFlavor().equals(flavor)) {
adapter = eventCatalogUIAdapter;
collection = eventCatalogUIAdapter.getRawFields();
}
}
if (collection == null) {
throw new IllegalArgumentException(String.format("Unable to find an EventCatalogUIAdapter with Flavor '%s'", flavorString));
}
String fieldsJson = catalog.get("fields").toString();
if (StringUtils.trimToNull(fieldsJson) != null) {
Map<String, String> fields = RequestUtils.getKeyValueMap(fieldsJson);
for (String key : fields.keySet()) {
if ("subjects".equals(key)) {
// Handle the special case of allowing subjects to be an array.
MetadataField<?> field = collection.getOutputFields().get(DublinCore.PROPERTY_SUBJECT.getLocalName());
if (field == null) {
throw new NotFoundException(String.format("Cannot find a metadata field with id 'subject' from Catalog with Flavor '%s'.", flavorString));
}
collection.removeField(field);
try {
JSONArray subjects = (JSONArray) parser.parse(fields.get(key));
collection.addField(MetadataField.copyMetadataFieldWithValue(field, StringUtils.join(subjects.iterator(), ",")));
} catch (ParseException e) {
throw new IllegalArgumentException(String.format("Unable to parse the 'subjects' metadata array field because: %s", e.toString()));
}
} else if ("startDate".equals(key)) {
// Special handling for start date since in API v1 we expect start date and start time to be separate fields.
MetadataField<String> field = (MetadataField<String>) collection.getOutputFields().get(key);
if (field == null) {
throw new NotFoundException(String.format("Cannot find a metadata field with id '%s' from Catalog with Flavor '%s'.", key, flavorString));
}
SimpleDateFormat apiSdf = MetadataField.getSimpleDateFormatter(startDatePattern.getOr(field.getPattern().get()));
SimpleDateFormat sdf = MetadataField.getSimpleDateFormatter(field.getPattern().get());
DateTime newStartDate = new DateTime(apiSdf.parse(fields.get(key)), DateTimeZone.UTC);
if (field.getValue().isSome()) {
DateTime oldStartDate = new DateTime(sdf.parse(field.getValue().get()), DateTimeZone.UTC);
newStartDate = oldStartDate.withDate(newStartDate.year().get(), newStartDate.monthOfYear().get(), newStartDate.dayOfMonth().get());
}
collection.removeField(field);
collection.addField(MetadataField.copyMetadataFieldWithValue(field, sdf.format(newStartDate.toDate())));
} else if ("startTime".equals(key)) {
// Special handling for start time since in API v1 we expect start date and start time to be separate fields.
MetadataField<String> field = (MetadataField<String>) collection.getOutputFields().get("startDate");
if (field == null) {
throw new NotFoundException(String.format("Cannot find a metadata field with id '%s' from Catalog with Flavor '%s'.", "startDate", flavorString));
}
SimpleDateFormat apiSdf = MetadataField.getSimpleDateFormatter(startTimePattern.getOr("HH:mm"));
SimpleDateFormat sdf = MetadataField.getSimpleDateFormatter(field.getPattern().get());
DateTime newStartDate = new DateTime(apiSdf.parse(fields.get(key)), DateTimeZone.UTC);
if (field.getValue().isSome()) {
DateTime oldStartDate = new DateTime(sdf.parse(field.getValue().get()), DateTimeZone.UTC);
newStartDate = oldStartDate.withTime(newStartDate.hourOfDay().get(), newStartDate.minuteOfHour().get(), newStartDate.secondOfMinute().get(), newStartDate.millisOfSecond().get());
}
collection.removeField(field);
collection.addField(MetadataField.copyMetadataFieldWithValue(field, sdf.format(newStartDate.toDate())));
} else {
MetadataField<?> field = collection.getOutputFields().get(key);
if (field == null) {
throw new NotFoundException(String.format("Cannot find a metadata field with id '%s' from Catalog with Flavor '%s'.", key, flavorString));
}
collection.removeField(field);
collection.addField(MetadataField.copyMetadataFieldWithValue(field, fields.get(key)));
}
}
}
metadataList.add(adapter, collection);
}
setStartDateAndTimeIfUnset(metadataList);
return metadataList;
}
use of org.opencastproject.util.NotFoundException in project opencast by opencast.
the class EventHttpServletRequest method setFormField.
/**
* Set a value for creating a new event from a form field.
*
* @param eventCatalogUIAdapters
* The list of event catalog ui adapters used for loading the metadata for the new event.
* @param eventHttpServletRequest
* The current details of the request that have been loaded.
* @param item
* The content of the field.
* @param fieldName
* The key of the field.
* @param startDatePattern
* The pattern to use to parse the start date from the request.
* @param startTimePattern
* The pattern to use to parse the start time from the request.
* @throws IOException
* Thrown if unable to laod the content of the field.
* @throws NotFoundException
* Thrown if unable to find a metadata catalog or field that matches an input catalog or field.
*/
private static void setFormField(List<EventCatalogUIAdapter> eventCatalogUIAdapters, EventHttpServletRequest eventHttpServletRequest, FileItemStream item, String fieldName, Opt<String> startDatePattern, Opt<String> startTimePattern) throws IOException, NotFoundException {
if (METADATA_JSON_KEY.equals(fieldName)) {
String metadata = Streams.asString(item.openStream());
try {
MetadataList metadataList = deserializeMetadataList(metadata, eventCatalogUIAdapters, startDatePattern, startTimePattern);
eventHttpServletRequest.setMetadataList(metadataList);
} catch (IllegalArgumentException e) {
throw e;
} catch (ParseException e) {
throw new IllegalArgumentException(String.format("Unable to parse event metadata because: '%s'", e.toString()));
} catch (NotFoundException e) {
throw e;
} catch (java.text.ParseException e) {
throw new IllegalArgumentException(String.format("Unable to parse event metadata because: '%s'", e.toString()));
}
} else if ("acl".equals(item.getFieldName())) {
String access = Streams.asString(item.openStream());
try {
AccessControlList acl = deserializeJsonToAcl(access, true);
eventHttpServletRequest.setAcl(acl);
} catch (Exception e) {
logger.warn("Unable to parse acl {}", access);
throw new IllegalArgumentException("Unable to parse acl");
}
} else if ("processing".equals(item.getFieldName())) {
String processing = Streams.asString(item.openStream());
JSONParser parser = new JSONParser();
try {
eventHttpServletRequest.setProcessing((JSONObject) parser.parse(processing));
} catch (Exception e) {
logger.warn("Unable to parse processing configuration {}", processing);
throw new IllegalArgumentException("Unable to parse processing configuration");
}
}
}
use of org.opencastproject.util.NotFoundException in project opencast by opencast.
the class EventIndexUtils method updateComments.
/**
* Update an event with the given has comments and has open comments status.
*
* @param eventId
* the event id
* @param hasComments
* whether it has comments
* @param hasOpenComments
* whether it has open comments
* @param organization
* the organization
* @param user
* the user
* @param searchIndex
* the serach index
* @throws SearchIndexException
* if error occurs
* @throws NotFoundException
* if event has not been found
*/
public static void updateComments(String eventId, boolean hasComments, boolean hasOpenComments, boolean needsCutting, String organization, User user, AbstractSearchIndex searchIndex) throws SearchIndexException, NotFoundException {
if (!hasComments && hasOpenComments)
throw new IllegalStateException("Invalid comment update request: You can't have open comments without having any comments!");
if (!hasOpenComments && needsCutting)
throw new IllegalStateException("Invalid comment update request: You can't have an needs cutting comment without having any open comments!");
Event event = getEvent(eventId, organization, user, searchIndex);
if (event == null)
throw new NotFoundException("No event with id " + eventId + " found.");
event.setHasComments(hasComments);
event.setHasOpenComments(hasOpenComments);
event.setNeedsCutting(needsCutting);
try {
searchIndex.addOrUpdate(event);
} catch (SearchIndexException e) {
logger.warn("Unable to update event '{}': {}", event, ExceptionUtils.getStackTrace(e));
}
}
Aggregations