1 64 65 package com.jcorporate.expresso.services.controller; 66 67 72 73 import com.jcorporate.expresso.core.controller.ControllerException; 74 import com.jcorporate.expresso.core.controller.ControllerRequest; 75 import com.jcorporate.expresso.core.controller.ControllerResponse; 76 import com.jcorporate.expresso.core.controller.DBController; 77 import com.jcorporate.expresso.core.controller.Input; 78 import com.jcorporate.expresso.core.controller.Output; 79 import com.jcorporate.expresso.core.controller.ServletControllerRequest; 80 import com.jcorporate.expresso.core.controller.State; 81 import com.jcorporate.expresso.core.controller.Transition; 82 import com.jcorporate.expresso.core.db.DBException; 83 import com.jcorporate.expresso.core.i18n.Messages; 84 import com.jcorporate.expresso.core.jsdkapi.GenericSession; 85 import com.jcorporate.expresso.core.misc.StringUtil; 86 import com.jcorporate.expresso.services.dbobj.UserPreference; 87 import com.jcorporate.expresso.services.dbobj.UserPreferenceDef; 88 import com.jcorporate.expresso.services.dbobj.UserPreferenceVal; 89 90 import javax.servlet.ServletException ; 91 import javax.servlet.http.HttpServletRequest ; 92 import java.util.Enumeration ; 93 import java.util.Iterator ; 94 import java.util.StringTokenizer ; 95 96 97 106 public class EditUserPreference 107 extends DBController { 108 109 112 public EditUserPreference() { 113 super(); 114 115 State edit = new State("edit", "Edit Preferences"); 116 edit.addOptionalParameter("class"); 117 addState(edit); 118 setInitialState("edit"); 119 120 State update = new State("update", "Update Preferences"); 121 addState(update); 122 this.setSchema(com.jcorporate.expresso.core.ExpressoSchema.class); 123 } 124 125 130 public String getTitle() { 131 return ("Edit User Preferences"); 132 } 133 134 140 167 protected void runEditState(ControllerRequest params, 168 ControllerResponse myResponse) 169 throws ControllerException, DBException { 170 UserPreferenceDef defList = new UserPreferenceDef(); 171 defList.setDataContext(params.getDataContext()); 172 173 if (!StringUtil.notNull(params.getParameter("class")).equals("")) { 174 defList.setField("ClassName", params.getParameter("class")); 175 } 176 177 UserPreferenceDef oneDef = null; 178 179 for (Iterator ee = defList.searchAndRetrieveList("ClassName").iterator(); 180 ee.hasNext();) { 181 oneDef = (UserPreferenceDef) ee.next(); 182 183 String oneInputName = oneDef.getField("ClassName") + "_" + 184 oneDef.getField("PrefCode"); 185 Input oneInput = new Input(oneInputName); 186 oneInput.setLabel(oneDef.getField("Descrip")); 187 myResponse.addInput(oneInput); 188 189 UserPreferenceVal validVals = new UserPreferenceVal(); 190 validVals.setDataContext(params.getDataContext()); 191 validVals.setField("ClassName", oneDef.getField("ClassName")); 192 validVals.setField("PrefCode", oneDef.getField("PrefCode")); 193 194 UserPreferenceVal oneVal = null; 195 196 for (Iterator ev = validVals.searchAndRetrieveList().iterator(); 197 ev.hasNext();) { 198 oneVal = (UserPreferenceVal) ev.next(); 199 oneInput.addValidValue(oneVal.getField("PrefValue"), 200 oneVal.getField("Descrip")); 201 } 202 203 204 205 UserPreference currentVal = new UserPreference(); 206 currentVal.setDataContext(params.getDataContext()); 207 currentVal.setField("ExpUid", params.getUid()); 208 currentVal.setField("ClassName", oneDef.getField("ClassName")); 209 currentVal.setField("PrefCode", oneDef.getField("PrefCode")); 210 211 if (currentVal.find()) { 212 oneInput.setDefaultValue(currentVal.getField("PrefValue")); 213 } else { 214 oneInput.setDefaultValue(oneDef.getField("DefaultVal")); 215 } 216 } 217 218 219 Transition update = new Transition("Update", getClass().getName()); 220 update.addParam(STATE_PARAM_KEY, "update"); 221 myResponse.addTransition(update); 222 } 223 224 225 232 protected void runUpdateState(ControllerRequest params, 233 ControllerResponse myResponse) 234 throws ControllerException, DBException { 235 String oneParamName = null; 236 String oneClassName = null; 237 String onePrefCode = null; 238 int updatedCount = 0; 239 UserPreferenceDef oneDef = new UserPreferenceDef(); 240 oneDef.setDataContext(params.getDataContext()); 241 242 UserPreference onePreference = new UserPreference(); 243 onePreference.setDataContext(params.getDataContext()); 244 245 boolean reestablishLocale = false; 246 for (Enumeration p = params.getParameters().keys(); 247 p.hasMoreElements();) { 248 oneParamName = (String ) p.nextElement(); 249 250 StringTokenizer stk = new StringTokenizer (oneParamName, "_"); 251 252 if (!stk.hasMoreTokens()) { 253 continue; 254 } 255 256 oneClassName = stk.nextToken(); 257 258 if (!stk.hasMoreTokens()) { 259 continue; 260 } 261 262 onePrefCode = stk.nextToken(); 263 oneDef.clear(); 264 oneDef.setField("ClassName", oneClassName); 265 oneDef.setField("PrefCode", onePrefCode); 266 267 268 if (oneDef.find()) { 269 onePreference.clear(); 270 onePreference.setField("ClassName", oneClassName); 271 onePreference.setField("PrefCode", onePrefCode); 272 onePreference.setField("ExpUid", params.getUid()); 273 274 if (onePreference.find()) { 275 if (!onePreference.getField("PrefValue").equals(params.getParameter(oneParamName))) { 276 onePreference.setField("PrefValue", 277 params.getParameter(oneParamName)); 278 onePreference.update(); 279 updatedCount++; 280 } 281 } else { 282 onePreference.clear(); 283 onePreference.setField("ClassName", oneClassName); 284 onePreference.setField("PrefCode", onePrefCode); 285 onePreference.setField("ExpUid", params.getUid()); 286 onePreference.setField("PrefValue", 287 params.getParameter(oneParamName)); 288 onePreference.add(); 289 updatedCount++; 290 } 291 292 if ("com.jcorporate.expresso.core.servlet.CheckLogin".equals(oneClassName)) { 293 reestablishLocale = true; 294 } 295 296 } 297 298 } 299 300 if (reestablishLocale) { 302 ServletControllerRequest sr = (ServletControllerRequest) params; 303 HttpServletRequest hreq = (HttpServletRequest ) sr.getServletRequest(); 304 try { 305 GenericSession.removeAttribute(hreq, Messages.LOCALE_KEY); 306 Messages.establishLocale(hreq); 307 } catch (ServletException se) { 308 throw new ControllerException(se); 309 } 310 } 311 312 Output count = new Output("" + updatedCount); 313 count.setLabel("Number of Settings Updated"); 314 myResponse.addOutput(count); 315 } 316 317 318 } 319 | Popular Tags |