use of com.emc.sa.descriptor.ServiceFieldTable in project coprhd-controller by CoprHD.
the class WorkflowServiceDescriptor method mapWorkflowToServiceDescriptor.
private ServiceDescriptor mapWorkflowToServiceDescriptor(final CustomServicesWorkflow from) {
final ServiceDescriptor to = new ServiceDescriptor();
try {
final CustomServicesWorkflowDocument wfDocument = WorkflowHelper.toWorkflowDocument(from);
if (StringUtils.isEmpty(from.getState()) || from.getState().equals(CustomServicesWorkflowStatus.NONE) || from.getState().equals(CustomServicesWorkflowStatus.INVALID)) {
log.error("Workflow state is not valid. State:{} Workflow name:{}", from.getState(), from.getLabel());
throw new IllegalStateException(String.format("Workflow state is not valid. State %s Workflow name: %s Workflow id: %s", from.getState(), from.getLabel(), from.getId()));
}
to.setCategory(CUSTOM_SERVICE_CATEGORY);
to.setDescription(StringUtils.isNotBlank(wfDocument.getDescription()) ? wfDocument.getDescription() : wfDocument.getName());
to.setDestructive(false);
final String wfID = URIUtil.asString(from.getId());
to.setServiceId(StringUtils.isNotBlank(wfID) ? wfID : wfDocument.getName());
to.setTitle(wfDocument.getName());
to.setWorkflowId(wfDocument.getName());
to.setRoles(new ArrayList<String>(Arrays.asList(Role.SYSTEM_ADMIN.toString())));
for (final Step step : wfDocument.getSteps()) {
if (null != step.getInputGroups()) {
// Looping through all input groups
for (final InputGroup inputGroup : step.getInputGroups().values()) {
final MultiValueMap tableMap = new MultiValueMap();
for (final Input wfInput : inputGroup.getInputGroup()) {
final ServiceField serviceField = new ServiceField();
if (CustomServicesConstants.InputType.FROM_USER.toString().equals(wfInput.getType())) {
serviceField.setType(wfInput.getInputFieldType());
} else if (CustomServicesConstants.InputType.ASSET_OPTION_SINGLE.toString().equals(wfInput.getType())) {
serviceField.setType(wfInput.getValue());
} else if (CustomServicesConstants.InputType.ASSET_OPTION_MULTI.toString().equals(wfInput.getType())) {
serviceField.setType(wfInput.getValue());
serviceField.setSelect(ServiceField.SELECT_MANY);
} else if (CustomServicesConstants.InputType.FROM_USER_MULTI.toString().equals(wfInput.getType())) {
serviceField.setType(ServiceField.TYPE_CHOICE);
if (StringUtils.isNotBlank(wfInput.getDefaultValue())) {
// For list of options
final Map<String, String> options = new HashMap<>();
final List<String> defaultList = Arrays.asList(wfInput.getDefaultValue().split(","));
for (final String value : defaultList) {
// making the key and value the same
options.put(value, value);
}
serviceField.setOptions(options);
serviceField.setInitialValue(options.get(defaultList.get(0)));
} else if (MapUtils.isNotEmpty(wfInput.getOptions())) {
// For options Map
serviceField.setOptions(wfInput.getOptions());
}
} else {
continue;
}
final String inputName = wfInput.getName();
if (StringUtils.isNotBlank(wfInput.getDescription())) {
serviceField.setDescription(wfInput.getDescription());
}
final String friendlyName = StringUtils.isBlank(wfInput.getFriendlyName()) ? inputName : wfInput.getFriendlyName();
serviceField.setLabel(friendlyName);
serviceField.setName(friendlyName.replaceAll(CustomServicesConstants.SPACES_REGEX, StringUtils.EMPTY));
serviceField.setRequired(wfInput.getRequired());
if (!(CustomServicesConstants.InputType.FROM_USER_MULTI.toString().equals(wfInput.getType()))) {
// Initial value already set for FROM_USER_MULTI
serviceField.setInitialValue(wfInput.getDefaultValue());
}
// Setting all unlocked fields as lockable
if (!wfInput.getLocked()) {
serviceField.setLockable(true);
}
// if there is a table name we will build ServiceFieldTable later
if (null != wfInput.getTableName()) {
tableMap.put(wfInput.getTableName(), serviceField);
} else {
to.getItems().put(friendlyName, serviceField);
}
}
for (final String table : (Set<String>) tableMap.keySet()) {
final ServiceFieldTable serviceFieldTable = new ServiceFieldTable();
serviceFieldTable.setType(ServiceItem.TYPE_TABLE);
serviceFieldTable.setLabel(table);
serviceFieldTable.setName(table);
for (final ServiceField serviceField : (List<ServiceField>) tableMap.getCollection(table)) {
serviceFieldTable.addItem(serviceField);
}
to.getItems().put(table, serviceFieldTable);
}
}
}
}
} catch (final IOException io) {
log.error("Error deserializing workflow", io);
throw new IllegalStateException(String.format("Error deserializing workflow %s", from.getLabel()));
}
log.debug("Mapped workflow service descriptor for {}", from.getLabel());
return to;
}
Aggregations