Checking the Number of Hours Recorded (Example)
You want to prevent more than eight hours of working time being recorded for any one day for any employee.
If you want to add the customer-specific check described below to CATS notebook, you must implement the check with equal thoroughness in SAP R/3. To do so, use the CATS0006 SAP enhancement (Validate Entire Time Sheet). Note also that data can be supplemented in SAP R/3 only. To supplement data, use the CATS0002 SAP enhancement (Supplement Recorded Data).
Proceed as follows:
Java Source Code
package com.sap.mycats.customer.examples;
import com.sap.mycats.basics.tools.badi.*;
import com.sap.mycats.customer.businessLogic.*;
public class ZTimeSheetChecker2
implements ITimeSheetChecker
{
private final static String FIELD_NAME_ZACTIVITY = "ZACTIVITY";
private final static String FIELD_NAME_ZACTIVITY_SOME_FLAG = "MYFLAG";
private final static String ZMSGID = "ZZ";
private final static String MSG_NO_8_HOURS_EXCEEDED = "002";
private final static String FIELD_NAME_WORKDATE = "WORKDATE";
private final static String FIELD_NAME_CATSHOURS = "CATSHOURS";
private final static String FIELD_NAME_TRANSKEY = "TRANSKEY";
public IMessageContainer checkRecord (String anOperation,
IRecordReadOnly aRecord,
ITimeSheetReader aTimeSheetReader) {
IMessageContainer result
= MessageContainerFactory.createIMessageContainer();
try {
result.addMessages(checkZAcitivity(anOperation, aRecord,
aTimeSheetReader));
} catch (MessageNotDefinedException e) {
e.printStackTrace();
throw new Error(e.toString());
}
return result;
}
private IMessageContainer checkZAcitivity (String anOperation,
IRecordReadOnly aRecord,
ITimeSheetReader aTimeSheetReader)
throws MessageNotDefinedException {
IFieldDate workdate
= aRecord.getFieldAsDate(FIELD_NAME_WORKDATE);
String currentRecordId
= aRecord.getFieldAsString(FIELD_NAME_TRANSKEY);
IMessageContainer result
= MessageContainerFactory.createIMessageContainer();
ITableReadOnly records
= aTimeSheetReader.selectCurrentRecordVersions(workdate,
workdate);
float hours = aRecord.getFieldAsFloat(FIELD_NAME_CATSHOURS);
for (int i = 0; i < records.getRowCount(); i++) {
IRecordReadOnly record = records.getRecordReadOnly(i);
String recordId
= record.getFieldAsString(FIELD_NAME_TRANSKEY);
if (!currentRecordId.equals(recordId)) {
hours += record.getFieldAsFloat(FIELD_NAME_CATSHOURS);
}
}
if (hours > 8) {
result.addMessage(ZMSGID, IMessageContainer.TYPE_ERROR,
MSG_NO_8_HOURS_EXCEEDED);
}
return result;
}
}