private void checkIfFoundAndLatest(String id, Integer version, IResourceStorage.IResource resource) throws ResourceNotFoundException, ResourceModifiedException { if (resource == null) { IResourceStorage.IHistoryResource historyLatest = resourceStorage.readHistoryLatest(id); if (historyLatest == null || historyLatest.isDeleted() || version > historyLatest.getVersion()) { throw createResourceNotFoundException(id, version); } throw createResourceAlreadyModifiedException(id, version); } }
private static boolean isResourceIdValid(IResourceStore.IResourceId resourceId) { return resourceId.getId() != null && resourceId.getVersion() > 0; } }
@Override public synchronized void deleteAllPermanently(String id) { resourceStorage.removeAllPermanently(id); }
private static <T> Map<IResourceId, T> readConfigs(IResourceStore<T> store, List<URI> configUris) throws IResourceStore.ResourceNotFoundException, IResourceStore.ResourceStoreException { Map<IResourceId, T> ret = new LinkedHashMap<>(); for (URI uri : configUris) { IResourceId resourceId = RestUtilities.extractResourceId(uri); ret.put(resourceId, store.read(resourceId.getId(), resourceId.getVersion())); } return ret; }
@Override public Integer update(String id, Integer version, T content) throws IResourceStore.ResourceStoreException, IResourceStore.ResourceModifiedException, IResourceStore.ResourceNotFoundException { RuntimeUtilities.checkNotNull(id, "id"); RuntimeUtilities.checkNotNull(version, "version"); RuntimeUtilities.checkNotNull(content, "content"); IResourceStorage.IResource resource = resourceStorage.read(id, version); checkIfFoundAndLatest(id, version, resource); IResourceStorage.IHistoryResource history = resourceStorage.newHistoryResourceFor(resource, false); resourceStorage.store(history); try { Integer newVersion = resource.getVersion() + 1; IResourceStorage.IResource newResource = resourceStorage.newResource(resource.getId(), newVersion, content); resourceStorage.store(newResource); return newVersion; } catch (IOException e) { throw new IResourceStore.ResourceStoreException(e.getLocalizedMessage(), e); } }
@Override public OutputConfigurationSet readOutputSet(String id, Integer version, String filter, String order, Integer index, Integer limit) { try { return outputStore.read(id, version, filter, order, index, limit); } catch (IResourceStore.ResourceNotFoundException e) { throw new NotFoundException(e.getLocalizedMessage(), e); } catch (IResourceStore.ResourceStoreException e) { log.error(e.getLocalizedMessage(), e); throw new InternalServerErrorException(e.getLocalizedMessage(), e); } }
@Override public OutputConfigurationSet read(String id, Integer version, String filter, String order, Integer index, Integer limit) throws ResourceNotFoundException, ResourceStoreException { RuntimeUtilities.checkNotNull(filter, "filter"); RuntimeUtilities.checkNotNull(order, "order"); RuntimeUtilities.checkNotNull(index, "index"); RuntimeUtilities.checkNotNull(limit, "limit"); OutputConfigurationSet outputConfigurationSet = outputResourceStore.read(id, version); ResultManipulator<OutputConfiguration> outputManipulator; outputManipulator = new ResultManipulator<>(outputConfigurationSet.getOutputSet(), OutputConfiguration.class); try { outputManipulator.filterEntities(filter); } catch (ResultManipulator.FilterEntriesException e) { throw new ResourceStoreException(e.getLocalizedMessage(), e); } outputManipulator.sortEntities(OUTPUT_COMPARATOR, order); outputManipulator.limitEntities(index, limit); return outputConfigurationSet; }
protected T read(String id, Integer version) { RuntimeUtilities.checkNotNull(id, "id"); RuntimeUtilities.checkNotNull(version, "version"); RuntimeUtilities.checkNotNegative(version, "version"); try { return resourceStore.read(id, version); } catch (IResourceStore.ResourceNotFoundException e) { throw new NotFoundException(e.getLocalizedMessage(), e); } catch (IResourceStore.ResourceStoreException e) { log.error(e.getLocalizedMessage(), e); throw new InternalServerErrorException(e.getLocalizedMessage(), e); } }
@Override public T read(String id, Integer version) throws IResourceStore.ResourceNotFoundException, IResourceStore.ResourceStoreException { RuntimeUtilities.checkNotNull(id, "id"); RuntimeUtilities.checkNotNull(version, "version"); IResourceStorage.IResource<T> current = resourceStorage.read(id, version); if (current == null) { IResourceStorage.IHistoryResource historyResource = resourceStorage.readHistory(id, version); if (historyResource == null || historyResource.isDeleted()) { throw createResourceNotFoundException(id, version); } current = historyResource; } try { return current.getData(); } catch (IOException e) { String message = "Unable to deserialize resource (id=%s, version=%s)"; message = String.format(message, id, version); throw new IResourceStore.ResourceStoreException(message, e); } }
@Override public void deleteTestCase(String id) { try { testCaseStore.delete(id, 0); } catch (IResourceStore.ResourceStoreException e) { log.error(e.getLocalizedMessage(), e); throw new InternalServerErrorException(e); } catch (IResourceStore.ResourceModifiedException e) { log.debug(e.getLocalizedMessage(), e); throw new NoLogWebApplicationException(Response.Status.CONFLICT); } catch (IResourceStore.ResourceNotFoundException e) { log.debug(e.getLocalizedMessage(), e); throw new NoLogWebApplicationException(Response.Status.NOT_FOUND); } } }
@Override public IResourceId create(T content) throws IResourceStore.ResourceStoreException { RuntimeUtilities.checkNotNull(content, "content"); try { IResourceStorage.IResource currentResource = resourceStorage.newResource(content); resourceStorage.store(currentResource); return currentResource; } catch (IOException e) { throw new IResourceStore.ResourceStoreException(e.getLocalizedMessage(), e); } }
@Override public IResourceStore.IResourceId getCurrentResourceId(final String id) throws ResourceNotFoundException { RuntimeUtilities.checkNotNull(id, "id"); final Integer version = resourceStorage.getCurrentVersion(id); if (version == -1) { throw new ResourceNotFoundException("No document found for id (" + id + ")"); } return new IResourceId() { @Override public String getId() { return id; } @Override public Integer getVersion() { return version; } }; }
@Override public Integer getCurrentVersion(String id) { try { IResourceStore.IResourceId currentResourceId = getCurrentResourceId(id); return currentResourceId.getVersion(); } catch (IResourceStore.ResourceNotFoundException e) { throw new NotFoundException(e.getLocalizedMessage(), e); } }
static IResourceStore.ResourceNotFoundException createResourceNotFoundException(String id, Integer version) { String message = "Resource not found. (id=%s, version=%s)"; message = String.format(message, id, version); return new IResourceStore.ResourceNotFoundException(message); }
@Override public List<T> readDescriptors(String type, String filter, Integer index, Integer limit, boolean includeDeleted) throws IResourceStore.ResourceStoreException, IResourceStore.ResourceNotFoundException { List<IResourceFilter.QueryFilter> queryFiltersRequired = new LinkedList<>(); String filterURI = "eddi://" + type + ".*"; queryFiltersRequired.add(new IResourceFilter.QueryFilter(FIELD_RESOURCE, filterURI)); queryFiltersRequired.add(new IResourceFilter.QueryFilter(FIELD_DELETED, includeDeleted)); IResourceFilter.QueryFilters required = new IResourceFilter.QueryFilters(queryFiltersRequired); List<IResourceFilter.QueryFilter> queryFiltersOptional = new LinkedList<>(); if (filter != null) { filter = StringUtilities.convertToSearchString(filter); queryFiltersOptional.add(new IResourceFilter.QueryFilter(FIELD_NAME, filter)); queryFiltersOptional.add(new IResourceFilter.QueryFilter(FIELD_DESCRIPTION, filter)); } IResourceFilter.QueryFilters optional = new IResourceFilter.QueryFilters(IResourceFilter.QueryFilters.ConnectingType.OR, queryFiltersOptional); return resourceFilter.readResources(new IResourceFilter.QueryFilters[]{required, optional}, index, limit, FIELD_LAST_MODIFIED); }
private Response throwConflictException(String id, IResourceStore.ResourceModifiedException e) { try { IResourceStore.IResourceId currentId = resourceStore.getCurrentResourceId(id); throw RestUtilities.createConflictException(resourceURI, currentId); } catch (IResourceStore.ResourceNotFoundException e1) { throw new NotFoundException(e.getLocalizedMessage(), e); } }
private static ResourceModifiedException createResourceAlreadyModifiedException(String id, Integer version) { String message = "Resource already modified. Local update is necessary. (id=%s, version=%s)"; message = String.format(message, id, version); return new IResourceStore.ResourceModifiedException(message); }
@Override public List<String> readOutputKeys(String id, Integer version, String filter, String order, Integer limit) { try { return outputStore.readOutputActions(id, version, filter, order, limit); } catch (IResourceStore.ResourceStoreException e) { log.error(e.getLocalizedMessage(), e); throw new InternalServerErrorException(e.getLocalizedMessage(), e); } catch (IResourceStore.ResourceNotFoundException e) { throw new NotFoundException(e.getLocalizedMessage(), e); } }
public IResourceId create(final String id, final Integer version, T content) throws IResourceStore.ResourceStoreException { RuntimeUtilities.checkNotNull(id, "id"); RuntimeUtilities.checkNotNull(version, "version"); RuntimeUtilities.checkNotNull(content, "content"); try { IResourceStorage.IResource currentResource = resourceStorage.newResource(id, version, content); resourceStorage.store(currentResource); return currentResource; } catch (IOException e) { throw new IResourceStore.ResourceStoreException(e.getLocalizedMessage(), e); } } }
@Override public List<DocumentDescriptor> readDescriptors(String type, String filter, Integer index, Integer limit) { try { return documentDescriptorStore.readDescriptors(type, filter, index, limit, false); } catch (IResourceStore.ResourceStoreException e) { log.error(e.getLocalizedMessage(), e); throw new InternalServerErrorException(e.getLocalizedMessage(), e); } catch (IResourceStore.ResourceNotFoundException e) { throw new NotFoundException(e.getLocalizedMessage(), e); } }