ChecklistSaveAction.java
package com.mycim.webapp.actions.checklist;
import com.fa.sesa.exception.Assert;
import com.fa.sesa.exception.Errors;
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.PropertyUtils;
import com.mycim.framework.utils.lang.StringUtils;
import com.mycim.framework.utils.lang.collections.CollectionUtils;
import com.mycim.framework.utils.lang.collections.ListUtils;
import com.mycim.framework.utils.lang.math.NumberUtils;
import com.mycim.valueobject.MessageIdList;
import com.mycim.valueobject.ObjectList;
import com.mycim.valueobject.SystemConstant;
import com.mycim.valueobject.consts.ReferenceDetailNames;
import com.mycim.valueobject.consts.SessionNames;
import com.mycim.valueobject.ems.Checklist;
import com.mycim.valueobject.ems.ChecklistItem;
import com.mycim.valueobject.ems.PmsSchedule;
import com.mycim.valueobject.sys.ReferenceFileDetail;
import com.mycim.webapp.Constants;
import com.mycim.webapp.TemplateLocation;
import com.mycim.webapp.WebUtils;
import com.mycim.webapp.actions.EmsSetupAction;
import com.mycim.webapp.forms.ChecklistInfoForm;
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.*;
import java.util.stream.Collectors;
/**
* check List Action
*
* @author pinyan.song
* @version 6.0.0
* @date 2019-11-5 14:10
**/
public class ChecklistSaveAction extends EmsSetupAction {
public ActionForward create(ActionMapping mapping, ChecklistInfoForm theform, HttpServletRequest request,
HttpServletResponse response) throws Exception {
Checklist checklist = new Checklist(theform.getInstanceId().trim().toUpperCase(),
getNamedSpace(ObjectList.CHECKLIST_KEY, LocalContext.getFacilityRrn()),
ObjectList.CHECKLIST_KEY);
theform.setObjectType("EVENT_TYPE");
PropertyUtils.copyProperties(checklist, theform);
validateTheObject(checklist);
checklist.setTransPerformedby(LocalContext.getUserId());
checklist.setTransId(Constants.CREATE_KEY);
process(checklist);
WebUtils.setSuccessMsg(request);
return init(mapping, theform, request, response);
}
public ActionForward modify(ActionMapping mapping, ChecklistInfoForm theform, HttpServletRequest request,
HttpServletResponse response) throws Exception {
Checklist checklist = getCheckList(theform);
theform.setObjectType("EVENT_TYPE");
PropertyUtils.copyProperties(checklist, theform);
validateTheObject(checklist);
checklist.setTransPerformedby(LocalContext.getUserId());
checklist.setTransId(Constants.MODIFY_KEY);
process(checklist);
WebUtils.setSuccessMsg(request);
return init(mapping, theform, request, response);
}
public ActionForward delete(ActionMapping mapping, ChecklistInfoForm theform, HttpServletRequest request,
HttpServletResponse response) throws Exception {
Checklist checklist = new Checklist(theform.getInstanceId(),
getNamedSpace(ObjectList.CHECKLIST_KEY, LocalContext.getFacilityRrn()),
ObjectList.CHECKLIST_KEY);
checklist = (Checklist) getInstance(checklist);
String checklistId = checklist.getInstanceId();
long checklistRrn = checklist.getInstanceRrn();
if (checklistId != null && !"".equals(checklistId)) {
checklistId = checklistId.trim().toUpperCase();
checklistRrn = this
.getInstanceRrn(checklistId, getNamedSpace(ObjectList.CHECKLIST_KEY, LocalContext.getFacilityRrn()),
ObjectList.CHECKLIST_KEY);
}
Assert.isFalse(checklistRrn < 1,
Errors.create().key(MessageIdList.ENTITYPM_INVALID_CHECKLIST).content("无效检查单ID").build());
//List checklistPMs = pmsService.getPmListByCheckList(checklistRrn);
PmsSchedule pmsSchedule = new PmsSchedule();
pmsSchedule.setChecklistRrn(checklistRrn);
List checklistPMs = pmsService.qryEqptPMInfo(pmsSchedule);
Assert.isFalse(CollectionUtils.isNotEmpty(checklistPMs),
Errors.create().key(MessageIdList.ENTITYPM_CHECKLIST_USEING_SCHEDULE).content("检查单在使用中不能删除!")
.build());
List<Map<String, Object>> checklistEntityCounters = emsService.getChecklistEntityCounterLists(checklistRrn);
Assert.isFalse(CollectionUtils.isNotEmpty(checklistEntityCounters),
Errors.create().key(MessageIdList.ENTITYPM_CHECKLIST_USEING_COUNTER).content("检查单在使用中不能删除!")
.build());
checklist.setTransPerformedby(LocalContext.getUserId());
checklist.setTransId(Constants.DELETE_KEY);
process(checklist);
WebUtils.setSuccessMsg(request);
return cancel(mapping, theform, request, response);
}
@Override
public ActionForward init(ActionMapping mapping, ActionForm form, HttpServletRequest request,
HttpServletResponse response) throws Exception {
ChecklistInfoForm theform = (ChecklistInfoForm) form;
String id = theform.getInstanceId().trim().toUpperCase();
Checklist checklist = getCheckList(theform);
PropertyUtils.copyProperties(theform, checklist);
if (checklist.getInstanceRrn() <= 0) {
theform.setTransId(Constants.CREATE_KEY);
} else {
request.setAttribute(SessionNames.CHECKLIST_KEY, checklist);
theform.setTransId(Constants.MODIFY_KEY);
}
return mapping.getInputForward();
}
public ActionForward copy(ActionMapping mapping, ActionForm form, HttpServletRequest request,
HttpServletResponse response) throws Exception {
ChecklistInfoForm theform = (ChecklistInfoForm) form;
String id = request.getParameter(Constants.COPY_KEY);
Checklist checklist = new Checklist(id, getNamedSpace(ObjectList.CHECKLIST_KEY, LocalContext.getFacilityRrn()),
ObjectList.CHECKLIST_KEY);
doCopy(theform, checklist);
theform.setTransId(Constants.CREATE_KEY);
return mapping.getInputForward();
}
public ActionForward member(ActionMapping mapping, ChecklistInfoForm theform, HttpServletRequest request,
HttpServletResponse response) throws Exception {
return mapping.findForward(Constants.MEMBERS_KEY);
}
public ActionForward deleteMember(ActionMapping mapping, ChecklistInfoForm theform, HttpServletRequest request,
HttpServletResponse response) throws Exception {
int checklistSequence = WebUtils.getParameterInt(Constants.ITEM_KEY, request);
Checklist checklist = getCheckList(theform);
ChecklistItem checklistItem = new ChecklistItem();
checklistItem.setChecklistRrn(checklist.getInstanceRrn());
checklistItem.setTransId(Constants.DELETE_KEY);
checklistItem.setTransPerformedby(LocalContext.getUserId());
if (checklist.getCurrentVersion() == null) {
checklistItem.setChecklistVersion(0);
} else {
checklistItem.setChecklistVersion(checklist.getCurrentVersion());
}
checklistItem.setChecklistSequence(checklistSequence);
emsService.deleteChecklistItem(checklistItem);
WebUtils.setSuccessMsg(request);
return init(mapping, theform, request, response);
}
public ActionForward down(ActionMapping mapping, ChecklistInfoForm theform, HttpServletRequest request,
HttpServletResponse response) throws Exception {
long seq = WebUtils.getParameterLong(Constants.ITEM_KEY, request);
Checklist checklist = getCheckList(theform);
int i = checklist.getChecklistItems().size();
if (seq < (new Integer(i)).longValue()) {
emsService.changeChecklistItemSeq(checklist.getInstanceRrn(), seq, seq + 1);
WebUtils.setSuccessMsg(request);
}
return init(mapping, theform, request, response);
}
public ActionForward up(ActionMapping mapping, ChecklistInfoForm theform, HttpServletRequest request,
HttpServletResponse response) throws Exception {
long seq = WebUtils.getParameterLong(Constants.ITEM_KEY, request);
Checklist checklist = getCheckList(theform);
if (seq > 1) {
emsService.changeChecklistItemSeq(checklist.getInstanceRrn(), seq, seq - 1);
WebUtils.setSuccessMsg(request);
}
WebUtils.setSuccessMsg(request);
return init(mapping, theform, request, response);
}
private Checklist getCheckList(ChecklistInfoForm theform) {
Checklist checklist = new Checklist(theform.getInstanceId().trim().toUpperCase(),
getNamedSpace(ObjectList.CHECKLIST_KEY, LocalContext.getFacilityRrn()),
ObjectList.CHECKLIST_KEY);
Checklist c = (Checklist) getInstance(checklist);
if (c != null) {
checklist = c;
checklist.setChecklistItems(emsService.getChecklistItems(checklist.getInstanceRrn()));
}
return checklist;
}
private void validateTheObject(Checklist checklist) {
Long facilityRrn = LocalContext.getFacilityRrn();
if (checklist.getRoleId().trim().length() > 0) {
long roleRrn = getInstanceRrn(checklist.getRoleId().trim(), getNamedSpace(ObjectList.ROLE_KEY, facilityRrn),
ObjectList.ROLE_KEY);
Assert.isFalse(roleRrn <= 0,
Errors.create().key(MessageIdList.SYSTEM_INSTANCE_NOT_FOUND).content("{} 没有找到对象!")
.args("Role").build());
checklist.setRoleRrn(roleRrn);
} else {
checklist.setRoleRrn(null);
}
/*
long startEventRrn = 0;
long endEventRrn = 0;
if (checklist.getStartEventId() != null && checklist.getStartEventId().trim().length() > 0) {
startEventRrn = getInstanceRrn(checklist.getStartEventId().trim().toUpperCase(),
getNamedSpace(ObjectList.EVENT_KEY, facilityRrn),
ObjectList.EVENT_KEY);
}
if (checklist.getEndEventId() != null && checklist.getEndEventId().trim().length() > 0) {
endEventRrn = getInstanceRrn(checklist.getEndEventId().trim().toUpperCase(),
getNamedSpace(ObjectList.EVENT_KEY, facilityRrn),
ObjectList.EVENT_KEY);
}
if (startEventRrn <= 0) {
throw new WebException("entitypm.check_eqp_event_start",
"Please check the job start EQP event id.");
} else {
checklist.setStartEventRrn(startEventRrn);
}
if (endEventRrn <= 0) {
throw new WebException("entitypm.check_eqp_event_completion",
"Please check the job completion EQP event id.");
} else {
checklist.setEndEventRrn(endEventRrn);
}
*/
}
public void downloadTemplate(HttpServletResponse response) throws Exception {
String fileName = "checklistItem_import_template-version.xlsx";
WebUtils.exportExcel(fileName, new HashMap<>(), new ArrayList(), TemplateLocation.CHECKLISTITEM_IMPORT_TEMPLATE, 1, 1,
response);
}
public ActionForward doImportExcel(ActionMapping mapping, ChecklistInfoForm theform,
HttpServletRequest request, HttpServletResponse response) throws Exception {
FormFile excelFile = theform.getFormFile();
Assert.isFalse(!StringUtils.endsWith(excelFile.getFileName(), ".xlsx") || excelFile.getFileSize() < 1,
Errors.create().key(MessageIdList.IMPORT_ERROR_NAME).content("Only import .xlsx files!").build());
long facilityRrn = LocalContext.getFacilityRrn();
long refFileRrnType = this.getInstanceRrn(ReferenceDetailNames.CHECKLIST_DATA_TYPE, facilityRrn, ObjectList.REFERENCE_FILE_KEY);
Assert.isFalse(refFileRrnType <= 0, Errors.create().content(
"Reference Detail File : "+ReferenceDetailNames.CHECKLIST_DATA_TYPE+ "Not Found!").build());
long refFileRrnUnit = this.getInstanceRrn(ReferenceDetailNames.CHECKLIST_DATA_TYPE_UNIT, facilityRrn, ObjectList.REFERENCE_FILE_KEY);
Assert.isFalse(refFileRrnUnit <= 0, Errors.create().content(
"Reference Detail File : "+ReferenceDetailNames.CHECKLIST_DATA_TYPE_UNIT+ "Not Found!").build());
List<ReferenceFileDetail> refFilesUnit = sysService.getReferenceFileDetails(refFileRrnUnit);
List<ReferenceFileDetail> refFilesType = sysService.getReferenceFileDetails(refFileRrnType);
List<ChecklistInfoForm> results = new ArrayList<>();
Checklist checklist = getCheckList(theform);
// List<ChecklistItem> checklistItemList = (ArrayList) checklist.getChecklistItems();
// Map<String, String> checkListItemIdMap = checklistItemList.stream().collect(Collectors.toMap(ChecklistItem::getChecklistItemId, ChecklistItem::getChecklistItemId, (key1, key2) -> key2));
Set<String> refFilesUnitSet = refFilesUnit.stream().map(ReferenceFileDetail::getData1Value).collect(Collectors.toSet());
Set<String> refFilesTypeSet = refFilesType.stream().map(r -> r.getData1Value().toUpperCase()).collect(Collectors.toSet());
List<String> boolRange = ListUtils.newArrayList(SystemConstant.Str.YES, SystemConstant.Str.NO);
if (CollectionUtils.isNotEmpty(refFilesUnit) && CollectionUtils.isNotEmpty(refFilesType)) {
results = new ExcelImport().attributeFrom(0).attributeTo(0).tableFrom(1)
.mapper(new ExcelParser<Object, ChecklistInfoForm>() {
@Override
public Object attributeMapper(List<ExcelRow> attributeRows,
int attributeFrom, int attributeTo) {
return null;
}
@Override
public ChecklistInfoForm tableRowMapper(ExcelRow excelRow, int rowNum,
Object attributeData) {
if(StringUtils.isNotBlank(excelRow.getString(0))){
ChecklistInfoForm checklistInfoForm = new ChecklistInfoForm();
StringBuilder stringBuilder = new StringBuilder();
String checklistItemId = StringUtils.trim(excelRow.getString(0));
// if(checkListItemIdMap.containsKey(checklistItemId)){//添加不成功,代表已经存在了
// stringBuilder.append(I18nUtils.getMessage(MessageIdList.ENTITYPM_CHECKLIST_ITEM_ID_EXIST,"Item ID already exists!"));
// stringBuilder.append("<br/>");
// }
checklistInfoForm.setChecklistItemId(checklistItemId);
String dataType = StringUtils.trim(excelRow.getString(1));
String unit = StringUtils.trim(excelRow.getString(2));
if (StringUtils.isNotBlank(dataType)){
checklistInfoForm.setDataType(dataType);
if (refFilesTypeSet.add(dataType.toUpperCase())){//添加成功,代表不存在
stringBuilder.append(I18nUtils.getMessage(MessageIdList.ENTITYPM_DATATYPE_RANGE,"dataType should be optional range;"));
stringBuilder.append("<br/>");
}
if(StringUtils.equals(dataType, Constants.NUMBER_KEY)) {
if(refFilesUnitSet.add(unit)) {//添加成功,代表不存在
stringBuilder.append(I18nUtils.getMessage(MessageIdList.ENTITYPM_UNIT_RANGE,"unit should be optional range;"));
stringBuilder.append("<br/>");
}
}
}else {
stringBuilder.append(I18nUtils.getMessage(MessageIdList.ENTITYPM_CHECKLIST_ITEM_TYPE_CANNOT_EMPTY,"dataType cannot be empty!"));
stringBuilder.append("<br/>");
}
if(StringUtils.equals(dataType, Constants.NUMBER_KEY)) {
String upperLimit = StringUtils.trim(excelRow.getString(4));
String lowerLimit = StringUtils.trim(excelRow.getString(5));
if(!NumberUtils.isDouble(upperLimit) || !NumberUtils.isDouble(lowerLimit)) {
stringBuilder.append(I18nUtils.getMessage(MessageIdList.ENTITYPM_UPPER_LOWER_NUMBER,"The upper and lower limits must be numbers!"));
stringBuilder.append("<br/>");
}else {
if(NumberUtils.toDouble(lowerLimit) > NumberUtils.toDouble(upperLimit)) {
stringBuilder.append(I18nUtils.getMessage(MessageIdList.ENTITYPM_LOWER_NOT_GREATER_UPPER,"Upper should be greater than Lower!"));
stringBuilder.append("<br/>");
}
}
checklistInfoForm.setLowerLimit(NumberUtils.toDouble(lowerLimit));
checklistInfoForm.setUpperLimit(NumberUtils.toDouble(upperLimit));
checklistInfoForm.setUnit(unit);
}
if (StringUtils.equals(dataType, Constants.BOOLEAN_KEY)) {
String bool = StringUtils.trim(excelRow.getString(6));
if (StringUtils.isBlank(bool)){
stringBuilder.append(I18nUtils.getMessage(MessageIdList.ENTITYPM_CHECKLIST_ITEM_BOOL_CANNOT_EMPTY,"Bool cannot be empty!"));
stringBuilder.append("<br/>");
}
if (StringUtils.isNotBlank(bool) && !boolRange.contains(bool.toUpperCase())){
stringBuilder.append(I18nUtils.getMessage(MessageIdList.ENTITYPM_BOOL_RANGE,"Bool should be optional range!"));
stringBuilder.append("<br/>");
}
checklistInfoForm.setBool(bool);
}
checklistInfoForm.setChecklistItemDesc(StringUtils.trim(excelRow.getString(3)));
checklistInfoForm.setErrMsg(stringBuilder.toString());
return checklistInfoForm;
}else {
return null;
}
}
}).file(excelFile.getInputStream()).getTableDataList();
}
StringBuilder errMsg = new StringBuilder();
for (int i=0; i < results.size();i++){
if(StringUtils.isNotEmpty(results.get(i).getErrMsg())){
int rowNum = i+1;
errMsg.append("Row Num "+ rowNum +": </br>").append(results.get(i).getErrMsg());
}
}
if(StringUtils.isEmpty(errMsg.toString())) {
for (ChecklistInfoForm checklistInfoForm : results) {
ChecklistItem checklistItem = new ChecklistItem();
checklistItem.setChecklistItemId(checklistInfoForm.getChecklistItemId());
checklistItem.setChecklistItemDesc(checklistInfoForm.getChecklistItemDesc());
checklistItem.setDataType(checklistInfoForm.getDataType());
checklistItem.setUnit(checklistInfoForm.getUnit());
checklistItem.setBool(checklistInfoForm.getBool());
checklistItem.setUpperLimit(checklistInfoForm.getUpperLimit());
checklistItem.setLowerLimit(checklistInfoForm.getLowerLimit());
checklistItem.setBorRrn(checklistInfoForm.getBorRrn());
checklistItem.setParameterSetId(checklistInfoForm.getParameterSetId());
checklistItem.setChecklistVersion(0);
checklistItem.setTransPerformedby(LocalContext.getUserId());
checklistItem.setChecklistSequence(checklist.getChecklistItems().size() + 1);//写了也没用
checklistItem.setTransId(Constants.CREATE_KEY);
checklistItem.setChecklistRrn(checklist.getInstanceRrn());
emsService.insertChecklistItem(checklistItem);
}
} else {
Assert.isFalse(true, Errors.create().content(errMsg.toString()).build());
}
return init(mapping, theform, request, response);
}
}