package dk.topsecurity.struts;
import javax.servlet.http.*; //HttpServletRequest,HttpServletResponse
import java.util.Date;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.struts.action.*; //ActionForm,DynaActionForm,ActionMessages,ActionMessage,ActionMapping,ActionForward
import org.apache.struts.actions.*; //DispatchAction
import dk.topsecurity.hibernate.*;
import dk.topsecurity.businessdelegate.*;
/**
* Struts framework 1.2+ action servlet support, which is wired into the
* webapplication through 'action-servlet.xml'
*/
public class AttributeAction extends DispatchAction {
private static Log log = LogFactory.getLog(AttributeAction.class);
private AttributeManager mgr = null;
public void setAttributeManager(AttributeManager attributeManager) { mgr = attributeManager; }
//delete usecase
public ActionForward delete(ActionMapping mapping,
ActionForm form,
HttpServletRequest request,
HttpServletResponse response)
throws Exception {
log.debug("starting 'delete' method...");
mgr.removeAttribute(request.getParameter("attribute.id"));
ActionMessages messages = new ActionMessages();
messages.add(ActionMessages.GLOBAL_MESSAGE, new ActionMessage("attribute.deleted"));
saveMessages(request, messages);
return list(mapping, form, request, response);
}
//create/update usecase
public ActionForward edit(ActionMapping mapping,
ActionForm form,
HttpServletRequest request,
HttpServletResponse response)
throws Exception {
log.debug("starting 'edit' method...");
DynaActionForm attributeForm = (DynaActionForm) form;
String attributeId = request.getParameter("id");
if (attributeId != null) { // null attributeId indicates an add
Attribute attribute = mgr.getAttribute(attributeId);
if (attribute == null) { //no attribute to corresponding id...
ActionMessages errors = new ActionMessages();
errors.add(ActionMessages.GLOBAL_MESSAGE, new ActionMessage("attribute.missing"));
saveErrors(request, errors);
return mapping.findForward("list");
} //else update
attribute.setAttributeDate( new Date() ); //currently updated
attributeForm.set("attribute", attribute); //overwrite default attribute
}
return mapping.findForward("edit");
}
//retrieve usecase
public ActionForward list(ActionMapping mapping,
ActionForm form,
HttpServletRequest request,
HttpServletResponse response)
throws Exception {
log.debug("starting 'list' method...");
request.setAttribute("listOfAllAttributes", mgr.getAttributes());
return mapping.findForward("list");
}
//create/update usecase persistance
public ActionForward save(ActionMapping mapping,
ActionForm form,
HttpServletRequest request,
HttpServletResponse response)
throws Exception {
log.debug("starting 'save' method...");
// run form validation rules - checking if field contents is valid
ActionMessages errors = form.validate(mapping, request);
if (!errors.isEmpty()) {
saveErrors(request, errors);
return mapping.findForward("edit");
}
DynaActionForm attributeForm = (DynaActionForm) form;
Attribute attr=(Attribute)attributeForm.get("attribute");
log.debug("found "+
(attr==null ? "no":
("currently (id="+attr.getId()+")"))
+" existing object");
//forbid creation of attributes with same name
if(attr.getId()==0 && //attemting to create new attribute
mgr.existAttribute( attr.getAttributeName() ) //already attribute with same name
) {
ActionMessages messages = new ActionMessages();
messages.add(ActionMessages.GLOBAL_MESSAGE, new ActionMessage("attribute.alreadyexists"));
saveMessages(request, messages);
return mapping.findForward("edit");
}
mgr.saveAttribute( attr );
log.debug("saving at id="+attr.getId());
ActionMessages messages = new ActionMessages();
messages.add(ActionMessages.GLOBAL_MESSAGE, new ActionMessage("attribute.saved"));
saveMessages(request, messages);
return list(mapping, form, request, response);
}
//default
public ActionForward unspecified(ActionMapping mapping,
ActionForm form,
HttpServletRequest request,
HttpServletResponse response)
throws Exception {
return list(mapping, form, request, response);
}
}