/** * Check the given task is not finished. * * @param task * The task to check. */ default void checkNotFinished(final T task) { if (isFinished(task)) { // Request a next step but is already finished (canceled?) throw new BusinessException("Already finished"); } }
/** * Normalize OpenLdap date format. * * @param utc * OpenLdap date format. * @return normalized date. */ public Date parseLdapDate(final String utc) { Date date = null; // setup x.208 generalized time formatter final DateFormat formatter = new SimpleDateFormat(OPEN_LDAP_DATE_FORMAT); try { // parse utc into Date date = formatter.parse(utc); } catch (final ParseException e) { log.info("Error while parsing date {}: {}", utc, e.getMessage()); throw new BusinessException(BusinessException.KEY_UNKNOW_ID); } return date; }
/** * Check the related node can be updated by the current principal. * * @param id * The node identifier to check. * @param checker * The function checking the node from its identifier (first parameter) for a given user (second * parameter) and return the check {@link Node} identity. * @return the checked node. */ public Node checkNode(final String id, final BiFunction<String, String, Node> checker) { final Node node = checker.apply(id, securityHelper.getLogin()); if (node == null) { // Node is not readable or does not exists throw new BusinessException("read-only-node", "node", id); } return node; }
/** * Update the business hours : only one range. * * @param id * The business hours identifier. */ @DELETE @Path("business-hours/{id:\\d+}") public void deleteBusinessHours(@PathParam("id") final int id) { final BusinessHours businessHours = findConfigured(businessHoursRepository, id); // Check there is at least one business range if (businessHours.getConfiguration().getBusinessHours().size() == 1) { throw new BusinessException("service:bt:no-business-hours"); } businessHoursRepository.delete(businessHours); }
private Snapshotting getSnapshot(final Node node) { return Optional.ofNullable(locator.getResource(node.getId(), Snapshotting.class)) .orElseThrow(() -> new BusinessException("snapshot-no-supported", node.getRefined().getId())); }
/** * Check there is no running task for given subscription before the deletion. * * @param lockedId * The locked entity's identifier. */ default void deleteTask(final I lockedId) { // Check there is no running import Optional.ofNullable(getTaskInternal(lockedId)).filter(t -> !isFinished(t)).ifPresent(t -> { throw new BusinessException("Running import not finished", t.getAuthor(), t.getStart(), lockedId); }); // We can safely delete the tasks getTaskRepository().deleteAllBy("locked.id", lockedId); }
private ParameterValue saveOrUpdate(final Map<String, ParameterValue> existing, final ParameterValueCreateVo value) { if (value.isUntouched()) { // Untouched value, keep the previous value but must exists return Optional.ofNullable(existing.get(value.getParameter())).orElseThrow( () -> new BusinessException(BusinessException.KEY_UNKNOW_ID, "parameter", value.getParameter())); } // Updated or created value final ParameterValue entity = existing.get(value.getParameter()); if (entity == null) { // Need to parse and recreate the value return createInternal(value); } // Update mode return checkSaveOrUpdate(value, entity.getParameter(), entity); }
/** * Check the parameter is related to the given node. * * @param parameter * The parameter to check. * @param node * The node scope the parameter must be related to. */ public void checkOwnership(final Parameter parameter, final Node node) { if (!equalsOrParentOf(parameter.getOwner(), node)) { // This parameter is detached from the node's hierarchy throw new BusinessException("invalid-parameter-node-ownership", "parameter", parameter.getId(), "node", node.getId()); } }
/** * Check there no running task within the same scope of the locked's identifier and starts a new task. * * @param lockedId * The locked entity's identifier. * @param initializer * The function to call while initializing the task. * @return the locked task with status. */ @Transactional(value = TxType.REQUIRES_NEW) default T startTask(final I lockedId, final Consumer<T> initializer) { synchronized (getLockedRepository()) { // Check there is no running task on the same node Optional.ofNullable(getTaskRepository().findNotFinishedByLocked(lockedId)).ifPresent(t -> { // On this service, there is already a running import throw new BusinessException("concurrent-task", t.getAuthor(), t.getStart(), lockedId); }); // Build a new task as needed final T task = createAsNeeded(lockedId); // Reset the specific fields initializer.accept(task); // Save this entity inside this transaction return getTaskRepository().saveAndFlush(task); } }
/** * Delete an existing {@link Node} from its identifier. The whole cache of nodes is invalidated. The deletion can * only succeed if there are no related subscription. They need to be previously deleted. The administrator rights * are also checked. * * @param id * The node identifier. * @throws Exception * When the related plug-in implementation throws an exception during the deletion. * */ @DELETE @Path("{id:service:.+:.+:.*}") @CacheRemoveAll(cacheName = "nodes") public void delete(@PathParam("id") final String id) throws Exception { checkAdministerable(id); final int nbSubscriptions = subscriptionRepository.countByNode(id); if (nbSubscriptions > 0) { // Subscriptions need to be deleted first throw new BusinessException("existing-subscriptions", nbSubscriptions); } // Delegate the deletion deleteWithTasks(id, id, true); pvResource.deleteByNode(id); parameterRepository.deleteByNode(id); eventRepository.deleteByNode(id); repository.deleteById(id); }
/** * Get or create a new task associated to given subscription. * * @param lockedId * The locked entity's identifier. The related entity will be locked. * @return The task, never <code>null</code>. */ default T createAsNeeded(final I lockedId) { final T task = Optional.ofNullable(getTaskInternal(lockedId)).map(t -> { // Additional remote check (optional) if (!isFinished(t)) { // On this service, there is already a running remote task throw new BusinessException("concurrent-task-remote", t.getAuthor(), t.getStart(), lockedId); } return t; }).orElseGet(() -> { final T newTask = newTask().get(); newTask.setLocked(getLockedRepository().findOneExpected(lockedId)); return newTask; }); // Reset internal fields task.setAuthor(SpringUtils.getBean(SecurityHelper.class).getLogin()); task.setStart(DateUtils.newCalendar().getTime()); task.setEnd(null); task.setFailed(false); return task; }