public static void checkNotNullOrEmpty(Collection<?> collection, String errorMessage){ if(collection == null || collection.isEmpty()) { throw new ApplicationException(ApplicationException.Code.INVALID_INPUT, errorMessage); } }
protected <T> T readValue(String json, Class<T> tClass) { try { return objectMapper.readValue(json, tClass); } catch (IOException ex) { throw new ApplicationException(ApplicationException.Code.INTERNAL_ERROR, ex); } }
protected String toJson(Object value) { try { return objectMapper.writeValueAsString(value); } catch (JsonProcessingException ex) { throw new ApplicationException(ApplicationException.Code.INTERNAL_ERROR, ex); } }
protected <T> T readValue(String json, TypeReference<T> typeReference) { try { return objectMapper.readValue(json, typeReference); } catch (IOException ex) { throw new ApplicationException(ApplicationException.Code.INTERNAL_ERROR, ex); } }
protected <V> V fromJson(String value, Class<V> returnType) { if (null == value) { return null; } try { return om.readValue(value, returnType); } catch (IOException ex) { throw new ApplicationException(Code.BACKEND_ERROR, "Could not convert JSON '" + value + "' to " + returnType.getName(), ex); } }
public Query(ObjectMapper objectMapper, Connection connection, String query) { this.rawQuery = query; this.om = objectMapper; try { this.statement = connection.prepareStatement(query); } catch (SQLException ex) { throw new ApplicationException(Code.BACKEND_ERROR, "Cannot prepare statement for query: " + ex.getMessage(), ex); } }
private void checkNotEmptyDefinitions(WorkflowDef workflowDefinition) { // Obtain the names of the tasks with missing definitions Set<String> missingTaskDefinitionNames = workflowDefinition.collectTasks().stream() .filter(MetadataMapperService::shouldPopulateDefinition) .map(WorkflowTask::getName) .collect(Collectors.toSet()); if (!missingTaskDefinitionNames.isEmpty()) { logger.error("Cannot find the task definitions for the following tasks used in workflow: {}", missingTaskDefinitionNames); Monitors.recordWorkflowStartError(workflowDefinition.getName(), WorkflowContext.get().getClientApp()); throw new ApplicationException(ApplicationException.Code.INVALID_INPUT, "Cannot find the task definitions for the following tasks used in workflow: " + missingTaskDefinitionNames); } }
protected final Query addParameterInternal(InternalParameterSetter setter) { int index = getAndIncrementIndex(); try { setter.apply(this.statement, index); return this; } catch (SQLException ex) { throw new ApplicationException(Code.BACKEND_ERROR, "Could not apply bind parameter at index " + index, ex); } }
@Override public void removeTaskDef(String name) { final String DELETE_TASKDEF_QUERY = "DELETE FROM meta_task_def WHERE name = ?"; executeWithTransaction(DELETE_TASKDEF_QUERY, q -> { if (!q.addParameter(name).executeDelete()) { throw new ApplicationException(ApplicationException.Code.NOT_FOUND, "No such task definition"); } taskDefCache.remove(name); }); }
@VisibleForTesting WorkflowMetadata getWorkflowMetadata(String workflowId) { ResultSet resultSet = session.execute(selectTotalStatement.bind(UUID.fromString(workflowId))); recordCassandraDaoRequests("getWorkflowMetadata"); return Optional.ofNullable(resultSet.one()) .map(row -> { WorkflowMetadata workflowMetadata = new WorkflowMetadata(); workflowMetadata.setTotalTasks(row.getInt(TOTAL_TASKS_KEY)); workflowMetadata.setTotalPartitions(row.getInt(TOTAL_PARTITIONS_KEY)); return workflowMetadata; }).orElseThrow(() -> new ApplicationException(ApplicationException.Code.NOT_FOUND, String.format("Workflow with id: %s not found in data store", workflowId))); }
@Override public void removeWorkflowDef(String name, Integer version) { final String DELETE_WORKFLOW_QUERY = "DELETE from meta_workflow_def WHERE name = ? AND version = ?"; executeWithTransaction(DELETE_WORKFLOW_QUERY, q -> { if (!q.addParameter(name).addParameter(version).executeDelete()) { throw new ApplicationException(ApplicationException.Code.NOT_FOUND, String.format("No such workflow definition: %s version: %d", name, version)); } }); }
private void removeTaskLookup(Task task) { try { recordCassandraDaoRequests("removeTaskLookup", task.getTaskType(), task.getWorkflowType()); session.execute(deleteTaskLookupStatement.bind(UUID.fromString(task.getTaskId()))); } catch (Exception e) { Monitors.error(CLASS_NAME, "removeTaskLookup"); String errorMsg = String.format("Failed to remove task lookup: %s", task.getTaskId()); LOGGER.error(errorMsg, e); throw new ApplicationException(ApplicationException.Code.BACKEND_ERROR, errorMsg); } }
@Override public boolean addEventExecution(EventExecution eventExecution) { try { return getWithTransaction(tx -> insertEventExecution(tx, eventExecution)); } catch (Exception e) { throw new ApplicationException(ApplicationException.Code.BACKEND_ERROR, "Unable to add event execution " + eventExecution.getId(), e); } }
@Override public void removeEventExecution(EventExecution eventExecution) { try { withTransaction(tx -> removeEventExecution(tx, eventExecution)); } catch (Exception e) { throw new ApplicationException(ApplicationException.Code.BACKEND_ERROR, "Unable to remove event execution " + eventExecution.getId(), e); } }
@Override public void updateEventExecution(EventExecution eventExecution) { try { withTransaction(tx -> updateEventExecution(tx, eventExecution)); } catch (Exception e) { throw new ApplicationException(ApplicationException.Code.BACKEND_ERROR, "Unable to update event execution " + eventExecution.getId(), e); } }
@Service public void registerWorkflowDef(WorkflowDef workflowDef) { if (workflowDef.getName().contains(":")) { throw new ApplicationException(Code.INVALID_INPUT, "Workflow name cannot contain the following set of characters: ':'"); } if (workflowDef.getSchemaVersion() < 1 || workflowDef.getSchemaVersion() > 2) { workflowDef.setSchemaVersion(2); } metadataDAO.create(workflowDef); }
@Override public void removeEventHandlerStatus(String name) { final String DELETE_EVENT_HANDLER_QUERY = "DELETE FROM meta_event_handler WHERE name = ?"; withTransaction(tx -> { EventHandler existing = getEventHandler(tx, name); if (existing == null) { throw new ApplicationException(ApplicationException.Code.NOT_FOUND, "EventHandler with name " + name + " not found!"); } execute(tx, DELETE_EVENT_HANDLER_QUERY, q -> q.addParameter(name).executeDelete()); }); }
@Override public boolean addEventExecution(EventExecution eventExecution) { try { String key = nsKey(EVENT_EXECUTION, eventExecution.getName(), eventExecution.getEvent(), eventExecution.getMessageId()); String json = objectMapper.writeValueAsString(eventExecution); recordRedisDaoEventRequests("addEventExecution", eventExecution.getEvent()); recordRedisDaoPayloadSize("addEventExecution", json.length(), eventExecution.getEvent(), "n/a"); return dynoClient.hsetnx(key, eventExecution.getId(), json) == 1L; } catch (Exception e) { throw new ApplicationException(Code.BACKEND_ERROR, "Unable to add event execution for " + eventExecution.getId(), e); } }
@Override public void removeEventExecution(EventExecution eventExecution) { try { String key = nsKey(EVENT_EXECUTION, eventExecution.getName(), eventExecution.getEvent(), eventExecution.getMessageId()); logger.info("removing event execution {}", key); dynoClient.hdel(key, eventExecution.getId()); recordRedisDaoEventRequests("removeEventExecution", eventExecution.getEvent()); } catch (Exception e) { throw new ApplicationException(Code.BACKEND_ERROR, "Unable to remove event execution for " + eventExecution.getId(), e); } }
public String rerun(RerunWorkflowRequest request) { Preconditions.checkNotNull(request.getReRunFromWorkflowId(), "reRunFromWorkflowId is missing"); if (!rerunWF(request.getReRunFromWorkflowId(), request.getReRunFromTaskId(), request.getTaskInput(), request.getWorkflowInput(), request.getCorrelationId())) { throw new ApplicationException(INVALID_INPUT, "Task " + request.getReRunFromTaskId() + " not found"); } return request.getReRunFromWorkflowId(); }