1 64 65 package com.jcorporate.expresso.services.controller.dbmaint; 66 67 import com.jcorporate.expresso.core.controller.Block; 68 import com.jcorporate.expresso.core.controller.Controller; 69 import com.jcorporate.expresso.core.controller.ControllerException; 70 import com.jcorporate.expresso.core.controller.ControllerRequest; 71 import com.jcorporate.expresso.core.controller.ControllerResponse; 72 import com.jcorporate.expresso.core.controller.Input; 73 import com.jcorporate.expresso.core.controller.NonHandleableException; 74 import com.jcorporate.expresso.core.controller.Transition; 75 import com.jcorporate.expresso.core.controller.session.PersistentSession; 76 import com.jcorporate.expresso.core.dataobjects.DataObject; 77 import com.jcorporate.expresso.core.dataobjects.DataObjectMetaData; 78 import com.jcorporate.expresso.core.dataobjects.Defineable; 79 import com.jcorporate.expresso.core.dataobjects.Securable; 80 import com.jcorporate.expresso.core.db.DBException; 81 import com.jcorporate.expresso.core.dbobj.SecuredDBObject; 82 import com.jcorporate.expresso.core.misc.StringUtil; 83 import com.jcorporate.expresso.core.misc.URLUTF8Encoder; 84 import com.jcorporate.expresso.core.security.User; 85 import com.jcorporate.expresso.services.controller.ui.DefaultAutoElement; 86 import com.jcorporate.expresso.services.dbobj.Setup; 87 88 import java.util.Iterator ; 89 import java.util.StringTokenizer ; 90 91 92 99 public class Update 100 extends GetBase { 101 102 public Update() { 103 } 104 105 111 public Update(String code, String descrip) { 112 super(code, descrip); 113 } 114 115 125 protected void autoField(String oneFieldName) 126 throws DBException, ControllerException { 127 DataObject myDBObj = this.getDataObject(); 128 DataObjectMetaData metadata = myDBObj.getMetaData(); 129 130 if (oneFieldName == null) { 131 throw new ControllerException("Field Name must not be null"); 132 } 133 if (myDBObj == null) { 134 throw new DBException("Database object must be " + 135 "initialized before calling autoField"); 136 } 137 138 boolean readOnly = false; 139 140 141 if (isKeyField(oneFieldName)) { 142 readOnly = true; 143 } 144 145 if (metadata.isVirtual(oneFieldName)) { 146 readOnly = true; 147 } 148 149 150 if (metadata.isReadOnly(oneFieldName)) { 151 readOnly = true; 152 } 153 String cachedValue = StringUtil.notNull(getFormCache(oneFieldName)); 154 155 if (cachedValue != null && cachedValue.length() == 0) { 159 cachedValue = null; 160 } 161 162 Input oneField = DefaultAutoElement.getAutoControllerElement() 163 .renderDBObjectField(this.getControllerResponse(), 164 myDBObj, 165 oneFieldName, 166 cachedValue, 167 readOnly); 168 169 if (oneField == null) { 170 return; 171 } 172 173 addInput(oneField); 174 175 176 177 178 179 String lookupObjectName = StringUtil.notNull(metadata.getLookupObject(oneFieldName)); 180 181 if (!lookupObjectName.equals("")) { 182 Transition lookup = new Transition(); 183 lookup.setName("lookup"); 184 lookup.addParam("dbobj", lookupObjectName); 185 lookup.addParam(Controller.STATE_PARAM_KEY, "Search"); 186 lookup.setDescription("Look up Values"); 187 oneField.addNested(lookup); 188 } 189 190 if (metadata.getAttribute(oneFieldName, "checkbox") != null) { 194 if (metadata.getType(oneFieldName).equals("boolean")) { 195 Boolean boolValue = myDBObj.getDataField(oneFieldName).asBoolean(); 196 if (boolValue == null) { 197 oneField.setDefaultValue("N"); 198 } else { 199 boolean b = boolValue.booleanValue(); 200 oneField.setDefaultValue(b ? "Y" : "N"); 201 } 202 } 203 } 204 205 206 if ("file".equalsIgnoreCase(oneField.getType()) && 211 myDBObj.getFieldMetaData(oneFieldName).isLongObjectType()) { 212 213 Transition t = new Transition("view", "View" 214 , com.jcorporate.expresso.services.controller.DBMaint.class, 215 "ViewBlob"); 216 t.addParam("fieldName", oneFieldName); 217 t.addParam("dbobj", ((Object ) myDBObj).getClass().getName()); 218 this.showBlobViewLink(oneFieldName, myDBObj, oneField, 219 this.getController().getClass().getName()); 220 } 221 } 222 223 224 230 public void run(ControllerRequest req, ControllerResponse res) 231 throws NonHandleableException, ControllerException { 232 super.run(req, res); 233 234 DataObject myDBObj = this.getDataObject(); 235 DataObjectMetaData metadata = myDBObj.getMetaData(); 236 237 String allKeys = URLUTF8Encoder.decode(StringUtil.notNull(getParameter("key"))); 238 239 if (allKeys.equals("")) { 240 throw new ControllerException("Update command requires key value string to follow it"); 241 } 242 try { 243 244 myDBObj = this.retrieveMyDBObject(); 245 246 247 showNext = false; 248 showPrev = false; 249 if (myDBObj instanceof Securable) { 250 ((Securable) myDBObj).isAllowed("U"); 251 } else { 252 if (getUid() == SecuredDBObject.SYSTEM_ACCOUNT 253 || User.getUserFromId(getUid(), this.getControllerRequest().getDataContext()).isAdmin()) { 254 } else { 256 String allowInsecure = Setup.getValue(req.getDataContext(), 257 com.jcorporate.expresso.core.ExpressoSchema.class.getName(), 258 "insecureDBMaint"); 259 if (!(StringUtil.toBoolean(allowInsecure))) { 260 throw new SecurityException ("Access to unsecured Objects not allowed"); 261 } 262 } 263 } 264 showForm(); 265 266 String keyParam = this.getKeyParameter(getDataObject()); 267 268 Transition update = new Transition("UpdateUpdate", getController()); 269 addParams(update); 270 update.addParam("key", keyParam); 271 add(update); 272 273 Transition delete = new Transition("UpdateDelete", getController()); 274 addParams(delete); 275 delete.addParam("key", keyParam); 276 add(delete); 277 278 if (!StringUtil.notNull(getParameter("details")).equals("y")) { 279 280 281 282 283 284 Block detBlock = new Block("details"); 285 286 try { 287 Transition masterTrans = new Transition("Update", 288 getController()); 289 String className = ((Object ) myDBObj).getClass().getName(); 290 masterTrans.setName("master"); 291 detBlock.add(masterTrans); 292 masterTrans.setLabel(metadata.getDescription(req.getLocale())); 293 masterTrans.setAttribute("dbobj", className); 294 masterTrans.addParam("dbobj", className); 295 masterTrans.addParam(Controller.STATE_PARAM_KEY, "Update"); 296 masterTrans.addParam("key", 297 URLUTF8Encoder.encode(keyParam)); 298 masterTrans.addParam("fields", 299 URLUTF8Encoder.encode(getFieldsParam())); 300 masterTrans.addParam("search", 301 URLUTF8Encoder.encode(getSearchParam())); 302 303 if (myDBObj instanceof com.jcorporate.expresso.core.dataobjects.Defineable) { 304 masterTrans.addParam("definition", ((Defineable) myDBObj).getDefinitionName()); 305 } 306 307 String oneDet = null; 308 309 for (Iterator ee = metadata.getDetailSet().iterator(); 310 ee.hasNext();) { 311 oneDet = (String ) ee.next(); 312 313 DataObject detDBObj = null; 314 315 try { 316 detDBObj = (DataObject) Class.forName(oneDet).newInstance(); 317 detDBObj.setLocale(getControllerRequest().getLocale()); 318 } catch (Exception e) { 319 throw new ControllerException("Unable to instantiate " + 320 "detail db object '" + oneDet + "'", e); 321 } 322 323 Transition oneTrans = new Transition("SearchList", 324 getController()); 325 detBlock.add(oneTrans); 326 oneTrans.setLabel(detDBObj.getMetaData().getDescription(req.getLocale())); 327 oneTrans.setAttribute("dbobj", 328 ((Object ) detDBObj).getClass().getName()); 329 oneTrans.addParam("dbobj", oneDet); 330 oneTrans.addParam(Controller.STATE_PARAM_KEY, "SearchList"); 331 oneTrans.addParam("details", "y"); 332 333 StringBuffer fieldsString = new StringBuffer (); 334 String localFields = metadata.getDetailFieldsLocal(oneDet); 335 String foreignFields = metadata.getDetailFieldsForeign(oneDet); 336 StringTokenizer stkLocal = new StringTokenizer (localFields, "|"); 337 StringTokenizer stkForeign = new StringTokenizer (foreignFields, 338 "|"); 339 boolean needsPipe = false; 340 341 while (stkLocal.hasMoreTokens()) { 342 if (needsPipe) { 343 fieldsString.append("|"); 344 } else { 345 needsPipe = true; 346 } 347 348 String localField = stkLocal.nextToken(); 349 String foreignField = stkForeign.nextToken(); 350 fieldsString.append(foreignField); 351 fieldsString.append("|"); 352 fieldsString.append(myDBObj.getDataField(localField).asString()); 353 } 354 355 oneTrans.addParam("search", fieldsString.toString()); 356 oneTrans.addParam("fields", fieldsString.toString()); 357 } 358 359 } catch (DBException de) { 360 throw new ControllerException(de); 361 } 362 if (detBlock.getNumContents() > 1) { 363 add(detBlock); 364 365 PersistentSession mySession = getSession(); 366 mySession.setPersistentAttribute(masterObjKey, detBlock); 367 } 368 } 369 } catch (DBException de) { 370 throw new ControllerException(de); 371 } 372 } 373 374 } 375 376 | Popular Tags |