ProcessSpecAction.java
package com.mycim.webapp.actions.spec;
import com.fa.sesa.exception.Assert;
import com.fa.sesa.exception.Errors;
import com.fa.sesa.exception.SystemIllegalArgumentException;
import com.fa.sesa.i18n.I18nUtils;
import com.fa.sesa.threadlocal.LocalContext;
import com.mycim.framework.file.excel.im.ExcelImport;
import com.mycim.framework.file.excel.im.ExcelParser;
import com.mycim.framework.file.excel.im.ExcelRow;
import com.mycim.framework.utils.beans.BeanUtils;
import com.mycim.framework.utils.lang.StringUtils;
import com.mycim.framework.utils.lang.collections.CollectionUtils;
import com.mycim.framework.utils.lang.collections.MapUtils;
import com.mycim.framework.utils.lang.math.NumberUtils;
import com.mycim.framework.utils.lang.time.DateUtils;
import com.mycim.valueobject.MessageIdList;
import com.mycim.valueobject.ObjectList;
import com.mycim.valueobject.Pair;
import com.mycim.valueobject.consts.VersionStatus;
import com.mycim.valueobject.prp.ProcessPlanning;
import com.mycim.valueobject.prp.ProcessSpecFormDto;
import com.mycim.valueobject.prp.ProcessSpecItemDto;
import com.mycim.valueobject.prp.ProcessVersion;
import com.mycim.webapp.TemplateLocation;
import com.mycim.webapp.WebUtils;
import com.mycim.webapp.actions.PrpSetupAction;
import com.mycim.webapp.forms.RecipeContextValueForm;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.upload.FormFile;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.*;
/**
* @author Luopeng.Wang
* @version 6.0.0
* @date 2021/3/22
**/
public class ProcessSpecAction extends PrpSetupAction {
@Override
public ActionForward init(ActionMapping mapping, ActionForm form, HttpServletRequest request,
HttpServletResponse response) throws Exception {
String[] types = new String[]{"STAGEID", "OPERATIONTYPE", "EDC_COLLECTION_RULE", "EDC_COLLECTION_RULE_TYPE",
"PROCESSLOCATION", "WORKAREATYPE", "BANKFLAG", "ACCEPTCONTAMINATION",
"ENTITYGROUPRRN", "SORTERFLIPTYPE"};
registerOptionType(LocalContext.getFacilityRrn(), Arrays.asList(types), request);
return mapping.getInputForward();
}
public Map<String, Object> query(ProcessSpecFormDto processSpecForm) {
buildForm(processSpecForm);
List<ProcessSpecItemDto> processSpecItems = processSpecService.queryProcessSpecItems(processSpecForm);
Map<String, Object> map = new HashMap<>();
map.put("totalItems", processSpecItems.size());
map.put("items", processSpecItems);
map.put("canRestore", processSpecService.canRestore(processSpecForm));
map.put("processSpecInfo", processSpecService.getProcessSpecInfo(processSpecForm));
return map;
}
public void saveSpec(ProcessSpecFormDto processSpecForm) {
processSpecService.saveProcessSpecItems(buildForm(processSpecForm));
}
public void freezeSpec(ProcessSpecFormDto processSpecForm) {
processSpecService.freezeProcessSpecItems(buildForm(processSpecForm));
}
public void unfreezeSpec(ProcessSpecFormDto processSpecForm) {
processSpecService.unfreezeProcessSpecItems(buildForm(processSpecForm));
}
public void activateSpec(ProcessSpecFormDto processSpecForm) {
processSpecService.activateProcessSpecItems(buildForm(processSpecForm));
}
public void restoreSpec(ProcessSpecFormDto processSpecForm) {
processSpecService.restoreProcessSpecItems(buildForm(processSpecForm));
}
public ActionForward exportProcessSpec(HttpServletRequest request, HttpServletResponse response) throws Exception {
Map<String, Object> map = WebUtils.getExportParams(request);
ProcessSpecFormDto processSpecForm = new ProcessSpecFormDto();
processSpecForm.setProcessId(MapUtils.getString(map, "processId"));
processSpecForm.setProcessVersion(MapUtils.getInteger(map, "processVersion"));
List<ProcessSpecItemDto> processSpecItems = processSpecService.queryProcessSpecItems(
buildForm(processSpecForm));
List<Map<String, Object>> list = new ArrayList<>();
for (int i = 0; i < processSpecItems.size(); i++) {
ProcessSpecItemDto processSpecItem = processSpecItems.get(i);
Map<String, Object> item = BeanUtils.copyBeanToMap(processSpecItem);
item.put("seq", i + 1);
list.add(item);
}
Map<String, Object> titles = WebUtils.getExportTitles(request);
if (StringUtils.equalsIgnoreCase("CN", I18nUtils.getCurrentLanguage().toString())) {
titles.put("seq", "序号");
titles.put("processSpecInfo", "流程RECIPE信息");
titles.put("processIdTitle", "流程号:");
titles.put("processId", processSpecForm.getProcessId());
titles.put("processVerTitle", "流程版本号:");
titles.put("processVer", processSpecForm.getProcessVersion());
} else {
titles.put("seq", "seq");
titles.put("processSpecInfo", "Process Spec Info");
titles.put("processIdTitle", "Process Id:");
titles.put("processId", processSpecForm.getProcessId());
titles.put("processVerTitle", "Process Version:");
titles.put("processVer", processSpecForm.getProcessVersion());
}
String fileName = "ProcessSpecList_" + DateUtils.getNowTime(DateUtils.DATE_FORMAT4NOSPLICING) + ".xlsx";
WebUtils.exportExcel(fileName, titles, list, TemplateLocation.PROCESS_SPEC, response);
return WebUtils.NULLActionForward;
}
public Map<String, Object> importFromExcel(RecipeContextValueForm theForm) {
FormFile upFile = theForm.getUpFile();
String fileName = upFile.getFileName();
Assert.isFalse(StringUtils.isEmpty(fileName), Errors.create().key(MessageIdList.SPEC_PLEASE_SELECT_EXCEL)
.content("Please select the Excel file to import!")
.build());
return processSpecService.checkImportProcessSpecItems(theForm.getProcessId(), theForm.getProcessVersion(),
buildImportData(upFile));
}
public List<Pair<Integer, String>> queryComboboxItemsForProcessVersion(ProcessSpecFormDto processSpecForm) {
List<Pair<Integer, String>> rebuildProcessVersions = new ArrayList<>();
ProcessPlanning processPlanning = new ProcessPlanning(processSpecForm.getProcessId(),
getNamedSpace(ObjectList.WFL_KEY,
LocalContext.getFacilityRrn()),
ObjectList.WFL_KEY);
processPlanning = prpService.getProcessPlanning(processPlanning);
long processRrn = processPlanning != null ? processPlanning.getInstanceRrn() : 0;
if (processRrn <= 0) {
return rebuildProcessVersions;
}
List<ProcessVersion> processVersions = prpService.getProcessVersions(processPlanning);
Assert.isFalse(CollectionUtils.isEmpty(processVersions),
Errors.create().key(MessageIdList.PROCESS_NO_VERSION).build());
for (ProcessVersion processVersion : processVersions) {
int version = processVersion.getInstanceVersion();
String versionDesc = version + " (" + processVersion.getVersionStatus() + ")";
rebuildProcessVersions.add(new Pair<>(version, versionDesc));
}
return rebuildProcessVersions;
}
public Map<String, Object> queryComboboxItemsByProduct(ProcessSpecFormDto processSpecForm) {
String productId = StringUtils.trimToUpperCase(processSpecForm.getProductId());
Map<String, Object> result = new HashMap<>();
if (StringUtils.isBlank(productId)) {
return result;
}
long productRrn = getInstanceRrn(productId, LocalContext.getFacilityRrn(), ObjectList.PRODUCT_KEY);
List<String> processIds = prpService.getProcessIdsByProductRrn(productRrn);
String processId = StringUtils.EMPTY;
if (CollectionUtils.isNotEmpty(processIds)) {
processId = processIds.get(0);
}
if (StringUtils.isNotBlank(processId)) {
processSpecForm.setProcessId(processId);
result.put("processId", processId);
result.put("processVersion", queryComboboxItemsForProcessVersion(processSpecForm));
}
return result;
}
private List<ProcessSpecItemDto> buildImportData(FormFile upFile) {
try {
ExcelImport excelImport = new ExcelImport().attributeFrom(4).attributeTo(4).tableFrom(6);
return excelImport.mapper(new ExcelParser<Object, ProcessSpecItemDto>() {
private String processId;
private Integer processVersion;
@Override
public Object attributeMapper(List<ExcelRow> attributeRows, int attributeFrom, int attributeTo) {
ExcelRow attributeRow = attributeRows.get(0);
processId = StringUtils.trimToUpperCase(attributeRow.getString(5));
processVersion = NumberUtils.toInt(StringUtils.trim(attributeRow.getString(9)));
return null;
}
@Override
public ProcessSpecItemDto tableRowMapper(ExcelRow excelRow, int rowNum, Object attributeData) {
if (rowNum == 6) {
return null;
}
ProcessSpecItemDto processSpecItemDto = new ProcessSpecItemDto();
processSpecItemDto.setProcessId(processId);
processSpecItemDto.setProcessVersion(processVersion);
processSpecItemDto.setRouteId(StringUtils.trimToUpperCase(excelRow.getString(3)));
processSpecItemDto.setStageId(StringUtils.trimToUpperCase(excelRow.getString(4)));
processSpecItemDto.setOperationId(StringUtils.trimToUpperCase(excelRow.getString(5)));
processSpecItemDto.setFlowSeq(StringUtils.trim(excelRow.getString(6)));
processSpecItemDto.setOperationDesc(excelRow.getString(7));
processSpecItemDto.setWorkArea(StringUtils.trimToUpperCase(excelRow.getString(8)));
processSpecItemDto.setRecipeId(StringUtils.trimToUpperCase(excelRow.getString(9)));
processSpecItemDto.setReticleFamilyId(StringUtils.trimToUpperCase(excelRow.getString(10)));
processSpecItemDto.setParameterSetId(StringUtils.trimToUpperCase(excelRow.getString(11)));
processSpecItemDto.setEquipmentGroupId(StringUtils.trimToUpperCase(excelRow.getString(12)));
processSpecItemDto.setPollutionLevel(StringUtils.trim(excelRow.getString(13)));
processSpecItemDto.setBankFlag(StringUtils.trimToUpperCase(excelRow.getString(14)));
// processSpecItemDto.setFlipType(StringUtils.trimToUpperCase(excelRow.getString(15)));
processSpecItemDto.setFlipTypeDesc(StringUtils.trimToUpperCase(excelRow.getString(15)));
processSpecItemDto.setOperationType(StringUtils.trimToUpperCase(excelRow.getString(20)));
processSpecItemDto.setProcessLocation(StringUtils.trimToUpperCase(excelRow.getString(21)));
return processSpecItemDto;
}
}).file(upFile.getInputStream()).getTableDataList();
} catch (Exception e) {
throw new SystemIllegalArgumentException(Errors.create().key(MessageIdList.SYSTEM_UPLOAD_FILE_FAILURE)
.content("Failed to parse the file. Please check if the " +
"file matches!").build());
}
}
private ProcessSpecFormDto buildForm(ProcessSpecFormDto processSpecForm) {
processSpecForm.setProcessId(StringUtils.trimToUpperCase(processSpecForm.getProcessId()));
Assert.isFalse(StringUtils.isEmpty(processSpecForm.getProcessId()),
Errors.create().content("Process ID can not be empty!").build());
Assert.isFalse(processSpecForm.getProcessVersion() == null || processSpecForm.getProcessVersion() <= 0,
Errors.create().content("Invalid process version!").build());
long processRrn = getInstanceRrn(processSpecForm.getProcessId(), LocalContext.getFacilityRrn(),
ObjectList.WFL_KEY);
Assert.isFalse(processRrn <= 0, Errors.create().content("Process does not exist!").build());
ProcessVersion version = prpService.getProcessVersion(processRrn, processSpecForm.getProcessVersion());
Assert.isFalse(VersionStatus.UNFROZEN_KEY.equals(version.getVersionStatus()),
Errors.create().content("Please freeze the process!").build());
processSpecForm.setProcessRrn(processRrn);
return processSpecForm;
}
}