public class EditObject extends AbstractElementProcessor {
@Override
public void process(final Request request) {
final RequestContext context = request.getContext();
final String objectId = request.getOptionalProperty(OBJECT);
final String forwardEditedTo = request.getOptionalProperty(VIEW);
final String forwardErrorTo = request.getOptionalProperty(ERROR);
final boolean hideNonEditableFields = request.isRequested(HIDE_UNEDITABLE, false);
final boolean showIcon = request.isRequested(SHOW_ICON, showIconByDefault());
String buttonTitle = request.getOptionalProperty(BUTTON_TITLE);
String formTitle = request.getOptionalProperty(FORM_TITLE);
final String formId = request.getOptionalProperty(FORM_ID, request.nextFormId());
final String variable = request.getOptionalProperty(RESULT_NAME);
final String resultOverride = request.getOptionalProperty(RESULT_OVERRIDE);
final String scope = request.getOptionalProperty(SCOPE);
final String className = request.getOptionalProperty(CLASS, "edit full");
final String id = request.getOptionalProperty(ID);
final String completionMessage = request.getOptionalProperty(MESSAGE);
final ObjectAdapter object = context.getMappedObjectOrResult(objectId);
final String actualObjectId = context.mapObject(object, Scope.INTERACTION);
final String version = context.mapVersion(object);
final FormState entryState = (FormState) context.getVariable(ENTRY_FIELDS);
final ObjectSpecification specification = object.getSpecification();
final FormFieldBlock containedBlock = new FormFieldBlock() {
@Override
public boolean isVisible(final String name) {
final ObjectAssociation fld = specification.getAssociation(name);
final boolean isVisible = fld.isVisible(IsisContext.getAuthenticationSession(), object).isAllowed();
final boolean isUseable = fld.isUsable(IsisContext.getAuthenticationSession(), object).isAllowed();
return isVisible && isUseable;
}
@Override
public ObjectAdapter getCurrent(final String name) {
ObjectAdapter value = null;
if (entryState != null) {
final FieldEditState field2 = entryState.getField(name);
value = field2.getValue();
}
if (value == null) {
final ObjectAssociation fld = specification.getAssociation(name);
value = fld.get(object);
}
return value;
}
@Override
public boolean isNullable(final String name) {
final ObjectAssociation fld = specification.getAssociation(name);
return !fld.isMandatory();
}
};
request.setBlockContent(containedBlock);
request.processUtilCloseTag();
final AuthenticationSession session = IsisContext.getAuthenticationSession();
List<ObjectAssociation> viewFields = specification.getAssociations(ObjectAssociationFilters.dynamicallyVisible(session, object));
viewFields = containedBlock.includedFields(viewFields);
final InputField[] formFields = createFields(viewFields);
initializeFields(context, object, formFields, entryState, !hideNonEditableFields);
setDefaults(context, object, formFields, entryState, showIcon);
copyFieldContent(context, object, formFields, showIcon);
overrideWithHtml(context, containedBlock, formFields);
String errors = null;
if (entryState != null && entryState.isForForm(formId)) {
copyEntryState(context, object, formFields, entryState);
errors = entryState.getError();
}
final String errorView = context.fullFilePath(forwardErrorTo == null ? context.getResourceFile() : forwardErrorTo);
final List<HiddenInputField> hiddenFields = new ArrayList<HiddenInputField>();
hiddenFields.add(new HiddenInputField("_" + OBJECT, actualObjectId));
hiddenFields.add(new HiddenInputField("_" + VERSION, version));
hiddenFields.add(new HiddenInputField("_" + FORM_ID, formId));
hiddenFields.add(completionMessage == null ? null : new HiddenInputField("_" + MESSAGE, completionMessage));
hiddenFields.add(forwardEditedTo == null ? null : new HiddenInputField("_" + VIEW, context.fullFilePath(forwardEditedTo)));
hiddenFields.add(new HiddenInputField("_" + ERROR, errorView));
hiddenFields.add(variable == null ? null : new HiddenInputField("_" + RESULT_NAME, variable));
hiddenFields.add(resultOverride == null ? null : new HiddenInputField("_" + RESULT_OVERRIDE, resultOverride));
hiddenFields.add(scope == null ? null : new HiddenInputField("_" + SCOPE, scope));