1 20 package org.enhydra.barracuda.contrib.sam.examples.data; 21 22 import java.util.*; 23 import javax.servlet.http.*; 24 import javax.servlet.*; 25 26 import org.enhydra.barracuda.core.comp.*; 27 import org.enhydra.barracuda.core.event.*; 28 import org.enhydra.barracuda.core.event.helper.*; 29 import org.enhydra.barracuda.contrib.dbroggisch.display.*; 30 import org.enhydra.barracuda.contrib.sam.xmlform.*; 31 import org.enhydra.barracuda.contrib.sam.data.*; 32 import org.enhydra.barracuda.contrib.sam.models.*; 33 import org.apache.log4j.*; 34 import org.enhydra.barracuda.contrib.sam.examples.events.*; 35 import org.enhydra.barracuda.contrib.sam.examples.xmlc.*; 36 37 public class UserGateway extends DataObjectGateway { 38 39 protected static Logger logger = Logger.getLogger(UserGateway.class.getName()); 41 protected Class getDefaultEvent() { return org.enhydra.barracuda.contrib.sam.examples.events.User.class; } 42 protected Class getDefaultHandler() { return UserHandler.class; } 43 protected Class getDeleteEvent() { return UserDelete.class; } 44 protected Class getDeleteHandler() { return UserDeleteHandler.class; } 45 protected Class getListEvent() { return UserList.class; } 46 protected Class getListHandler() { return UserListHandler.class; } 47 protected Class getNewEvent() { return UserNew.class; } 48 protected Class getNewHandler() { return UserNewHandler.class; } 49 protected Class getSaveEvent() { return UserSave.class; } 50 protected Class getSaveHandler() { return UserSaveHandler.class; } 51 protected Class getRenderEvent() { return RenderUser.class; } 52 protected Class getRenderHandler() { return RenderUserHandler.class; } 53 54 57 public UserGateway() { 58 super(); 59 logger.info("registering UserGateway's listeners"); 60 specifyLocalEventInterests(new EventConnectorFactory(UserListHandler.class), UserList.class); 61 specifyLocalEventInterests(new EventConnectorFactory(RenderUserListHandler.class), RenderUserList.class); 62 specifyLocalEventInterests(new EventConnectorFactory(UserSaveEmailHandler.class), UserSaveEmail.class); 63 } 64 65 public static class UserConfig implements Config { 66 67 public DataObject getDataObject(String oid) throws DataObjectException { 68 return org.enhydra.barracuda.contrib.sam.examples.data.User.getUser(oid); 69 } 70 71 public String getFormName() { return "user";} 72 public String getMapName() { return "map"; } 73 public String getOidName() { return "oid"; } 74 public ViewEvent getViewEvent() { return new RenderUser(); } 75 public String getXmlFormName() { return "/WEB-INF/userform.xml"; } 76 } 77 78 public static class UserNewHandler extends DataObjectNewHandler { 79 public Config getConfig() { return new UserConfig(); } 80 } 81 82 public static class UserDeleteHandler extends DataObjectDeleteHandler { 83 public Config getConfig() { return new UserConfig(); } 84 } 85 86 public static class UserSaveHandler extends DataObjectSaveHandler { 87 public Config getConfig() { return new UserConfig(); } 88 protected Object modifyFormData(String key, Object value ) { 89 if (key.equals("password") && ((value==null) || ((String )value).length()==0)) return "sample"; 90 return value; 91 } 92 93 } 94 95 public static class UserSaveEmailHandler extends DefaultBaseEventListener { 96 protected static Logger logger = Logger.getLogger(UserSaveEmailHandler.class.getName()); 97 public void handleControlEvent(ControlEventContext context) throws EventException, javax.servlet.ServletException , java.io.IOException { 98 Config config = new UserConfig(); 99 100 ErrorModel.create(context, new Exception ("the email feature is currently not implemented. This is just an example how you can override default behavior of the SaveHandler.")); 101 103 } 104 } 105 106 public static class UserHandler extends DataObjectHandler { 107 public Config getConfig() { return new UserConfig(); } 108 } 109 110 public static class RenderUserHandler extends DataObjectViewHandler { 111 112 public Config getConfig() { return new UserConfig(); } 113 114 public Object getTemplateModels(ViewContext vc) { 115 List l = (List)super.getTemplateModels(vc); 116 l.add(new ErrorModel(vc)); 117 HashMapModel hhm = new HashMapModel("Submit"); 118 hhm.setItem("Save", new BAction(new UserSave())); 119 hhm.setItem("Delete", new BAction(new UserDelete())); 120 hhm.setItem("New", new BAction(new UserNew())); 121 hhm.setItem("SaveEmail", new BAction(new UserSaveEmail())); 122 l.add(hhm); 123 return l; 124 } 125 126 public Class getTemplateClass(ViewContext vc) {return userHTML.class;} 127 } 128 129 public static class UserListHandler extends DefaultBaseEventListener { 130 131 public void handleControlEvent(ControlEventContext context) throws EventException, javax.servlet.ServletException , java.io.IOException { 132 BaseEvent event = context.getEvent(); 133 HttpServletRequest request = context.getRequest(); 134 HttpSession session = request.getSession(); 135 136 event.setHandled(true); 137 context.getQueue().addEvent(new RenderUserList()); 138 } 139 } 140 141 public static class RenderUserListHandler extends BTemplateViewHandler { 142 public Object getTemplateModels() { return new UserListModel(); } 143 public Class getTemplateClass() {return userlistHTML.class;} 144 } 145 146 public static class UserListModel extends IterativeCollectionModel { 147 148 public UserListModel() { 149 super("Nav"); 150 Collection l = org.enhydra.barracuda.contrib.sam.examples.data.User.getAll(); 151 setModels(l); 152 } 153 154 public Object getItem(String key) { 155 try { 156 org.enhydra.barracuda.contrib.sam.examples.data.User user = (org.enhydra.barracuda.contrib.sam.examples.data.User)_current; 157 if (key.equals("Show")) { 158 BLink r = new BLink((String )user.get("login"), new org.enhydra.barracuda.contrib.sam.examples.events.User()); 159 r.setParam("oid", (String )user.get("oid")); 160 return r; 161 } 162 else return super.getItem(key); 163 } catch (DataObjectException ex) { 164 logger.error(ex.getMessage(),ex); 165 return ex.getMessage(); 166 } 167 } 168 } 169 } 170 | Popular Tags |