Problem:
I am trying to validate based on another field (valid_to) from datasource, but the record in the server side validator only contains some of the fields from datasource (e.g. of course orgno, and org_name, type, not_effectuated etc.), but the valid_to field is not in the record. I have tried to use dependentFields to include this field, but it is still not in record. I have also tries to use applyWhen to try not to fire the validation based on the value in valid_to, but with no success. Is there some way I can reach this field in the server side validator?
SmartClient Version: v9.1p_2018-05-03/PowerEdition Deployment (built 2018-05-03)
Part of the datasource file:
The server side validator:
Try with dependent fields:
Try with applyWhen:
I am trying to validate based on another field (valid_to) from datasource, but the record in the server side validator only contains some of the fields from datasource (e.g. of course orgno, and org_name, type, not_effectuated etc.), but the valid_to field is not in the record. I have tried to use dependentFields to include this field, but it is still not in record. I have also tries to use applyWhen to try not to fire the validation based on the value in valid_to, but with no success. Is there some way I can reach this field in the server side validator?
SmartClient Version: v9.1p_2018-05-03/PowerEdition Deployment (built 2018-05-03)
Part of the datasource file:
Code:
<fields>
<field name="id" primaryKey="true" type="sequence" detail="true"/>
<field name="orgno" customSelectExpression=" case
when count(lor_beneficiary_approvals.organization_id) = 0 then null
when count(lor_beneficiary_approvals.organization_id) = 1 then o2.orgno
else null
end">
<validators>
[B] <validator type="serverCustom">
<serverObject lookupStyle="spring" bean="orgnoValidator"/>
<errorMessage>$error</errorMessage>
</validator>[/B]
</validators>
</field>
<field name="org_name" customSelectExpression=" case
when count(lor_beneficiary_approvals.organization_id) = 0 then null
when count(lor_beneficiary_approvals.organization_id) = 1 then org_er.name
else null
end"/>
<field name="type" type="integer" hidden="true"/>
<field name="not_effectuated" title="Ikke-effektuert" type="boolean" detail="true">
<validators>
<validator type="serverCustom">
<serverObject lookupStyle="spring" bean="financialStatementOnLotteryValidator"/>
<errorMessage>$error</errorMessage>
</validator>
</validators>
</field>
<field name="valid_from" title="Gyldig fra" type="date" required="true" />
<field name="valid_to" title="Gyldig til" type="date" required="true" />
<field name="venue_id" type="integer" hidden="true" title="Id til lokale"/>
<field name="venue_name" type="text" tableName="lor_venues" title="Lokale" detail="false" nativeName="name"/>
</fields>
Code:
package no.lottstift.lotteri.server;
import java.rmi.RemoteException;
import java.util.Date;
import java.util.Map;
import javax.xml.bind.JAXBException;
import com.isomorphic.datasource.DSRequest;
import com.isomorphic.util.DataTools;
import no.lottstift.server.organization.OrganizationDao;
import no.lottstift.shared.datasource.Fields;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import com.isomorphic.datasource.Validator;
import static no.lottstift.shared.datasource.Fields.VALID_TO;
/**
* Validates that the provided orgno in {@link Fields#ORGNO} is set and maps to a organzation in ER.
* If {@link Fields#DELETED_FROM} is set, the validation is ignored, since this is a delete.
* The validation is also ignored if {@link Fields#VALID_TO} is NULL or before today.
* @author rwo
*
*/
public class ValidateOrgno {
private OrganizationDao mOrganizationDao = null;
public Logger mLog = LoggerFactory.getLogger(this.getClass());
@Autowired
public void setOrganizationDao(OrganizationDao dao) {
mOrganizationDao = dao;
}
public boolean condition(Object value, Validator validator, String fieldName, Map record) throws Exception {
if(record.get(Fields.DELETED_FROM) != null) {
mLog.debug("Ignoring validation, since delete date is set");
return true;
}
[B] Date validTo = getValidTo(record);
if(validTo == null || validTo.before(new Date())) {
mLog.debug("Ignoring validation since valid to is null or before today");
return true;
}[/B]
String orgno = record.get(fieldName).toString();
return validate(validator,orgno);
}
[B] protected Date getValidTo(Map record) throws Exception {
return getDate(record, VALID_TO);
}[/B]
private boolean validate(Validator validator, String orgno) throws RemoteException, JAXBException {
boolean exists = mOrganizationDao.existsInEnhetsregisteret(orgno);
if(exists) {
return true;
}
setErrorMessage(validator, orgno+" er ikke en enhet i enhetsregisteret.");
return false;
}
private void setErrorMessage(Validator validator, String message) {
validator.addErrorMessageVariable("error", message);
}
private Date getDate(Map record, String fieldName) {
try {
return (Date) record.get(fieldName);
} catch(Exception e) {
//OK. Could be validation error on date.
}
return null;
}
}
Code:
<validator type="serverCustom">
<serverObject lookupStyle="spring" bean="orgnoValidator"/>
<errorMessage>$error</errorMessage>
<dependentFields>
<value>valid_to</value>
</dependentFields>
</validator>
Code:
<validator type="serverCustom">
<serverObject lookupStyle="spring" bean="orgnoValidator"/>
<errorMessage>$error</errorMessage>
<applyWhen fieldName="valid_to" operator="equals" value="new Date();"/>
</validator>

Comment