net.n2oapp.framework.engine.validation.N2oValidationModule Maven / Gradle / Ivy
The newest version!
package net.n2oapp.framework.engine.validation;
import net.n2oapp.criteria.api.CollectionPage;
import net.n2oapp.criteria.dataset.DataSet;
import net.n2oapp.framework.api.exception.N2oException;
import net.n2oapp.framework.api.processing.DataProcessing;
import net.n2oapp.framework.api.ui.*;
import net.n2oapp.framework.engine.validation.engine.FailInfo;
import net.n2oapp.framework.engine.validation.engine.ValidationProcessor;
import net.n2oapp.framework.engine.validation.engine.info.ObjectValidationInfo;
import net.n2oapp.framework.engine.validation.engine.info.QueryValidationInfo;
import java.util.Collection;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import static net.n2oapp.framework.api.metadata.global.dao.validation.N2oValidation.ServerMoment.*;
/**
* Процессинговый модуль валидации
*/
public class N2oValidationModule implements DataProcessing {
private ValidationProcessor processor;
private AlertMessageBuilder alertMessageBuilder;
public N2oValidationModule(ValidationProcessor processor, AlertMessageBuilder alertMessageBuilder) {
this.processor = processor;
this.alertMessageBuilder = alertMessageBuilder;
}
@Override
public void processAction(ActionRequestInfo requestInfo, ActionResponseInfo responseInfo, DataSet dataSet) {
List fails = processor.validate(buildInfo(requestInfo, dataSet), beforeOperation);
prepareResponse(fails, requestInfo, responseInfo);
}
@Override
public void processActionError(ActionRequestInfo requestInfo, ActionResponseInfo responseInfo, DataSet dataSet, N2oException exception) {
List fails = processor.validate(buildInfo(requestInfo, dataSet), afterFailOperation);
prepareResponse(fails, requestInfo, responseInfo);
DataProcessing.super.processActionError(requestInfo, responseInfo, dataSet, exception);
}
@Override
public void processActionResult(ActionRequestInfo requestInfo, ActionResponseInfo responseInfo, DataSet dataSet) {
List fails = processor.validate(buildInfo(requestInfo, dataSet), afterSuccessOperation);
prepareResponse(fails, requestInfo, responseInfo);
}
@Override
public void processQueryError(QueryRequestInfo requestInfo, QueryResponseInfo responseInfo, N2oException exception) {
if (requestInfo.isValidationEnable() && requestInfo.getSize() == 1) {
List fails = processor.validate(buildInfo(requestInfo, requestInfo.getData()), afterFailQuery);
prepareResponse(fails, requestInfo, responseInfo);
}
DataProcessing.super.processQueryError(requestInfo, responseInfo, exception);
}
@Override
public void processQuery(QueryRequestInfo requestInfo, QueryResponseInfo responseInfo) {
if (requestInfo.isValidationEnable()) {
List fails = processor.validate(buildInfo(requestInfo, requestInfo.getData()), beforeQuery);
prepareResponse(fails, requestInfo, responseInfo);
}
}
@Override
public void processQueryResult(QueryRequestInfo requestInfo, QueryResponseInfo responseInfo, CollectionPage page) {
final Collection list = page.getCollection();
if (requestInfo.isValidationEnable() && !list.isEmpty()) {
List fails = processor.validate(buildInfo(requestInfo, list.iterator().next()), afterSuccessQuery);
prepareResponse(fails, requestInfo, responseInfo);
}
}
private ObjectValidationInfo buildInfo(ActionRequestInfo requestInfo, DataSet dataSet) {
return new ObjectValidationInfo(
requestInfo.getObject().getId(),
requestInfo.getOperation().getValidationList(),
dataSet,
requestInfo.getMessagesForm()
);
}
private QueryValidationInfo buildInfo(QueryRequestInfo requestInfo, DataSet inDataSet) {
DataSet dataSet = inDataSet != null ? inDataSet : requestInfo.getData();
DataSet result = new DataSet();
Map paramsMap = requestInfo.getQuery().getParamToFilterIdMap();
for (String key : dataSet.keySet()) {
if (paramsMap.containsKey(key)) {
if (DataSet.isSpreadKey(paramsMap.get(key)) && !(dataSet.get(key) instanceof Collection))
result.put(paramsMap.get(key), Collections.singletonList(dataSet.get(key)));
else
result.put(paramsMap.get(key), dataSet.get(key));
} else
result.put(key, dataSet.get(key));
}
return new QueryValidationInfo(
requestInfo.getQuery().getObject(),
requestInfo.getQuery().getValidations(),
result,
requestInfo.getMessagesForm()
);
}
private void prepareResponse(List fails, RequestInfo requestInfo, ResponseInfo responseInfo) {
for (FailInfo fail : fails) {
ResponseMessage message = responseInfo.constructMessage(requestInfo, fail.getSeverity(), alertMessageBuilder);
message.setText(fail.getMessage());
message.setTitle(fail.getMessageTitle());
message.setField(fail.getFieldId());
responseInfo.addMessage(message);
}
}
}