1 64 65 package com.jcorporate.expresso.services.controller.dbmaint; 66 67 import com.jcorporate.expresso.core.controller.Controller; 68 import com.jcorporate.expresso.core.controller.ControllerException; 69 import com.jcorporate.expresso.core.controller.ControllerRequest; 70 import com.jcorporate.expresso.core.controller.ControllerResponse; 71 import com.jcorporate.expresso.core.controller.Input; 72 import com.jcorporate.expresso.core.controller.NonHandleableException; 73 import com.jcorporate.expresso.core.controller.Transition; 74 import com.jcorporate.expresso.core.dataobjects.DataFieldMetaData; 75 import com.jcorporate.expresso.core.dataobjects.DataObject; 76 import com.jcorporate.expresso.core.dataobjects.DataObjectMetaData; 77 import com.jcorporate.expresso.core.dataobjects.Securable; 78 import com.jcorporate.expresso.core.db.DBException; 79 import com.jcorporate.expresso.core.dbobj.SecuredDBObject; 80 import com.jcorporate.expresso.core.dbobj.ValidValue; 81 import com.jcorporate.expresso.core.misc.StringUtil; 82 import com.jcorporate.expresso.core.security.User; 83 import com.jcorporate.expresso.services.controller.ui.DefaultAutoElement; 84 import com.jcorporate.expresso.services.dbobj.Setup; 85 86 import java.util.Vector ; 87 88 89 96 public class Search 97 extends GetBase { 98 99 public Search() { 100 101 } 102 103 109 public Search(String code, String descrip) { 110 super(code, descrip); 111 } 112 113 118 protected void autoField(String oneFieldName) 119 throws DBException, ControllerException { 120 boolean readOnly = false; 121 122 if (oneFieldName == null) { 123 throw new ControllerException("Field Name must not be null"); 124 } 125 126 DataObject myDBObj = this.getDataObject(); 127 DataObjectMetaData objectMetadata = myDBObj.getMetaData(); 128 129 if (myDBObj == null) { 130 throw new DBException("Database object must be " + 131 "initialized before calling autoField"); 132 } 133 134 135 String oneFieldValue = myDBObj.getDataField(oneFieldName).asString(); 136 137 if (oneFieldValue == null) { 138 oneFieldValue = (""); 139 } 140 141 String fixedValue = (String ) getFixedFields().get(oneFieldName); 142 boolean skipField = false; 143 144 if (fixedValue != null) { 145 oneFieldValue = fixedValue; 146 skipField = true; 147 readOnly = true; 148 } 149 150 151 152 153 Input oneField = null; 154 DataFieldMetaData metaData = myDBObj.getFieldMetaData(oneFieldName); 155 156 if (metaData.isBinaryObjectType()) { 157 return; 158 } 159 160 if (metaData.isReadOnly()) { 161 oneField = new Input(oneFieldName); 166 oneField.setLabel(objectMetadata.getDescription(this.getControllerRequest().getLocale(), oneFieldName)); 167 oneField.setDefaultValue(oneFieldValue); 168 String oneFieldSize = Integer.toString(metaData.getLengthInt()); 169 170 if (oneFieldSize.equals("0")) { 171 oneFieldSize = ("30"); 172 } 173 174 int fieldSize = metaData.getLengthInt(); 175 if (fieldSize == 0 && metaData.isNumericType()) { 176 fieldSize = 12; 177 } else { 178 fieldSize = fieldSize + 2; 179 } 180 181 int displayFieldSize = fieldSize; 182 183 if (displayFieldSize > 60) { 189 displayFieldSize = 60; 190 } 191 if (displayFieldSize < 20 && metaData.isNumericType()) { 192 displayFieldSize = 22; 193 } 194 195 196 oneField.setMaxLength(128); 199 oneField.setDisplayLength(displayFieldSize); 200 201 if (objectMetadata.isMultiValued(oneFieldName)) { 202 oneField.setValidValues(new Vector (myDBObj.getValidValuesList(oneFieldName))); 203 oneField.setAttribute(Input.ATTRIBUTE_MULTIVALUED, "Y"); 204 oneField.setAttribute(Input.ATTRIBUTE_DROPDOWN, "Y"); 205 oneField.setType(Input.ATTRIBUTE_DROPDOWN); 206 } 207 } else { 208 oneField = DefaultAutoElement.getAutoControllerElement() 209 .renderDBObjectField(this.getControllerResponse(), 210 myDBObj, 211 oneFieldName, 212 oneFieldValue, 213 readOnly); 214 } 215 216 if (oneField == null) { 217 return; 218 } 219 220 addInput(oneField); 221 222 223 if (!skipField) { 224 oneField.setDefaultValue(""); 225 226 227 228 if (objectMetadata.isMultiValued(oneFieldName)) { 229 Vector values = oneField.getValidValues(); 230 231 if (values == null) { 232 throw new DBException("Valid values for field " + 233 oneFieldName + " from object " + 234 objectMetadata.getName() + " were null"); 235 } 236 237 values.add(new ValidValue("", "All Values")); 238 oneField.setValidValues(values); 239 oneField.setDefaultValue(""); 240 } else { 241 oneField.setMaxLength(128); 243 if (oneField.getDisplayLength() < 20) { 244 oneField.setDisplayLength(20); 245 } 246 } 247 248 249 250 251 String lookupObjectName = StringUtil.notNull(objectMetadata.getLookupObject(oneFieldName)); 252 253 if (!lookupObjectName.equals("")) { 254 Transition lookup = new Transition(); 255 lookup.setName("lookup"); 256 lookup.addParam("dbobj", lookupObjectName); 257 lookup.addParam(Controller.STATE_PARAM_KEY, "Search"); 258 lookup.setDescription("Look up Values"); 259 oneField.addNested(lookup); 260 } 261 262 } 263 } 264 265 266 272 public void run(ControllerRequest req, ControllerResponse res) 273 throws NonHandleableException, ControllerException { 274 super.run(req, res); 275 276 DataObject myDBObj = this.getDataObject(); 277 278 try { 279 if (myDBObj instanceof Securable) { 280 ((Securable) myDBObj).isAllowed("S"); 281 } else { 282 if (getUid() == SecuredDBObject.SYSTEM_ACCOUNT 283 || User.getUserFromId(getUid(), this.getControllerRequest().getDataContext()).isAdmin()) { 284 } else { 286 String allowInsecure = Setup.getValue(req.getDataContext(), 287 com.jcorporate.expresso.core.ExpressoSchema.class.getName(), 288 "insecureDBMaint"); 289 if (!(StringUtil.toBoolean(allowInsecure))) { 290 throw new SecurityException ("Access to unsecured Objects not allowed"); 291 } 292 } 293 } 294 myDBObj.clear(); 295 showNext = false; 296 showPrev = false; 297 showForm(); 298 299 Transition searchList = new Transition("SearchList", 300 getController()); 301 addParams(searchList); 302 add(searchList); 303 } catch (DBException de) { 304 throw new ControllerException(de); 305 } 306 } 307 308 } | Popular Tags |