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.ControllerException; 69 import com.jcorporate.expresso.core.controller.Output; 70 import com.jcorporate.expresso.core.dataobjects.DataField; 71 import com.jcorporate.expresso.core.dataobjects.DataFieldMetaData; 72 import com.jcorporate.expresso.core.dataobjects.DataObject; 73 import com.jcorporate.expresso.core.dataobjects.DataObjectMetaData; 74 import com.jcorporate.expresso.core.dataobjects.NestableDataObject; 75 import com.jcorporate.expresso.core.dataobjects.jdbc.JDBCDataObject; 76 import com.jcorporate.expresso.core.dataobjects.jdbc.LobField; 77 import com.jcorporate.expresso.core.db.DBException; 78 import com.jcorporate.expresso.core.dbobj.ValidValue; 79 import com.jcorporate.expresso.services.dbobj.MediaDBObject; 80 import org.apache.log4j.Logger; 81 82 import java.util.Iterator ; 83 84 85 94 public abstract class UpdateBase 95 extends DynamicCmd { 96 97 private static Logger log = Logger.getLogger(UpdateBase.class); 98 99 public UpdateBase() { 100 } 101 102 108 public UpdateBase(String code, String descrip) { 109 super(code, descrip); 110 } 111 112 120 protected void showKey(boolean withEditLink) 121 throws DBException, ControllerException { 122 DataObject myDBObj = this.getDataObject(); 123 DataObjectMetaData metadata = myDBObj.getMetaData(); 124 String controller = getControllerName(); 125 126 Block keyTable = new Block("key"); 127 addBlock(keyTable); 128 keyTable.setAttribute("table", "Y"); 129 130 String oneKeyFieldName = null; 131 132 for (Iterator e = myDBObj.getMetaData().getKeyFieldListArray().iterator(); e.hasNext();) { 133 oneKeyFieldName = (String ) e.next(); 134 DataFieldMetaData fieldMetadata = myDBObj.getFieldMetaData(oneKeyFieldName); 135 136 Block oneRow = new Block("oneRow"); 137 oneRow.setAttribute("row", "Y"); 138 keyTable.add(oneRow); 139 oneRow.add(new Output("FieldDescrip", metadata.getDescription(this 140 .getControllerRequest().getLocale(), oneKeyFieldName))); 141 143 if (fieldMetadata.isMultiValued()) { 144 java.util.List values = myDBObj.getValidValuesList(oneKeyFieldName); 145 146 if (values == null) { 147 throw new DBException("Valid values for field " + 148 oneKeyFieldName + " from object " + 149 fieldMetadata.getName() + " were null"); 150 } 151 152 String fieldValue = null; 153 ValidValue oneVV = null; 154 155 for (Iterator ve = values.iterator(); ve.hasNext();) { 156 oneVV = (ValidValue) ve.next(); 157 158 if (oneVV.getValue().equals(myDBObj.getDataField(oneKeyFieldName).asString())) { 159 fieldValue = oneVV.getDescription(); 160 } 161 } 162 if (fieldValue == null) { 163 Output fieldOutput = new Output("FieldValue", 164 myDBObj.getDataField(oneKeyFieldName).asString()); 165 166 if (withEditLink) { 167 showEditLink(oneKeyFieldName, 168 myDBObj.getDataField(oneKeyFieldName).asString(), 169 myDBObj, fieldOutput, controller); 170 } 171 172 oneRow.add(fieldOutput); 173 } else { 174 Output fieldOutput = new Output("FieldValue", fieldValue); 175 176 if (withEditLink) { 177 showEditLink(oneKeyFieldName, fieldValue, myDBObj, 178 fieldOutput, controller); 179 } 180 181 oneRow.add(fieldOutput); 182 } 183 } else { 184 Output fieldOutput = new Output("FieldValue", 185 myDBObj.getDataField(oneKeyFieldName).asString()); 186 187 if (withEditLink) { 188 showEditLink(oneKeyFieldName, 189 myDBObj.getDataField(oneKeyFieldName).asString(), myDBObj, 190 fieldOutput, controller); 191 } 192 193 oneRow.add(fieldOutput); 194 } 195 } 196 197 } 198 199 209 protected void saveBlobFields(DataObject myDBObj) throws DBException { 210 211 212 if (!(myDBObj instanceof JDBCDataObject || myDBObj instanceof NestableDataObject)) { 214 return; 215 } 216 217 218 219 for (Iterator i = myDBObj.getMetaData().getFieldListArray().iterator(); i.hasNext();) { 225 String nextField = (String ) i.next(); 226 DataFieldMetaData metadata = myDBObj.getFieldMetaData(nextField); 227 228 if (metadata.isBinaryObjectType()) { 229 DataField field = myDBObj.getDataField(nextField); 230 String uploadedFileName = (String ) field.getAttribute("fileName"); 231 232 if (uploadedFileName != null && uploadedFileName.length() > 0) { 233 DataObject daoToUse = myDBObj; 234 if (daoToUse instanceof NestableDataObject) { 235 daoToUse = ((NestableDataObject) myDBObj).getNestedFromFieldName(nextField); 236 if (!(daoToUse instanceof JDBCDataObject)) { 237 log.warn("Nested DataObject is not of type JDBCDataObject. Skipping update of file name"); 238 continue; 239 } 240 241 nextField = ((NestableDataObject) myDBObj).getFieldFromNestedName(nextField); 242 field = daoToUse.getDataField(nextField); 243 } 244 245 246 if (daoToUse instanceof MediaDBObject) { 247 MediaDBObject mediaObj = (MediaDBObject) daoToUse; 248 mediaObj.saveBlob(nextField); 249 } else { 250 java.io.File f = new java.io.File (uploadedFileName); 251 if (f == null) { 252 log.error("File: " + uploadedFileName + " doesn't appear to exist."); 253 throw new DBException("Unable to retrieve uploaded file!"); 254 } 255 256 saveBlob(nextField, f, (JDBCDataObject) daoToUse); 257 258 } 259 } 260 } 261 } 262 } 263 264 272 protected void saveBlob(String fieldName, 273 java.io.File value, 274 JDBCDataObject criteria) throws DBException { 275 276 int fileSize = (int) value.length(); 277 java.io.InputStream is; 278 try { 279 is = new java.io.FileInputStream (value); 280 } catch (java.io.FileNotFoundException ex) { 281 log.error("File: " + value.getName() + " doesn't appear to exist.", ex); 282 throw new DBException("Unable to retrieve uploaded file!", ex); 283 } 284 285 LobField lf = new LobField(); 286 287 lf.setCriteria(criteria); 288 try { 289 lf.saveBlob(fieldName, is, fileSize); 290 } finally { 291 lf.close(); 292 } 293 } 294 295 296 } 297 298 | Popular Tags |