1 64 package com.jcorporate.expresso.services.controller.ui; 65 66 import com.jcorporate.expresso.core.ExpressoSchema; 67 import com.jcorporate.expresso.core.controller.Block; 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.ErrorCollection; 72 import com.jcorporate.expresso.core.controller.Input; 73 import com.jcorporate.expresso.core.controller.Output; 74 import com.jcorporate.expresso.core.dataobjects.DataException; 75 import com.jcorporate.expresso.core.dataobjects.DataField; 76 import com.jcorporate.expresso.core.dataobjects.DataFieldMetaData; 77 import com.jcorporate.expresso.core.dataobjects.DataObject; 78 import com.jcorporate.expresso.core.dataobjects.DataObjectMetaData; 79 import com.jcorporate.expresso.core.dataobjects.NestableDataObject; 80 import com.jcorporate.expresso.core.db.DBException; 81 import com.jcorporate.expresso.core.dbobj.DBObject; 82 import com.jcorporate.expresso.core.dbobj.SecuredDBObject; 83 import com.jcorporate.expresso.core.dbobj.ValidValue; 84 import com.jcorporate.expresso.core.misc.ConfigManager; 85 import com.jcorporate.expresso.core.misc.DateTime; 86 import com.jcorporate.expresso.core.misc.Format; 87 import com.jcorporate.expresso.core.misc.StringUtil; 88 import com.jcorporate.expresso.kernel.util.FastStringBuffer; 89 import com.jcorporate.expresso.services.dbobj.MediaDBObject; 90 import com.jcorporate.expresso.services.dbobj.MimeTypes; 91 import org.apache.log4j.Logger; 92 93 import java.text.DateFormat ; 94 import java.text.ParseException ; 95 import java.text.SimpleDateFormat ; 96 import java.util.Date ; 97 import java.util.HashMap ; 98 import java.util.Iterator ; 99 import java.util.Locale ; 100 import java.util.Map ; 101 import java.util.Vector ; 102 103 104 112 public class DefaultAutoElement implements AutoControllerElement { 113 116 protected static AutoControllerElement theDefault = null; 117 118 121 public static final String CLASS_HANDLER_NAME = "AutoControllerElement"; 122 123 126 public static final String DEFAULT_CLASS_HANDLER = DefaultAutoElement.class.getName(); 127 128 131 public static final String DEFAULT_STYLE = "jc-formfield"; 132 133 136 public static final String REQUIRED_STYLE = "jc-required-field"; 137 138 141 public static final String ERROR_STYLE = "jc-error-field"; 142 143 146 protected static Logger log = Logger.getLogger(DefaultAutoElement.class); 147 148 151 public static final String SESSION_KEY = "expresso.services.controller.ui.dbobjList"; 152 private static final String EXPRESSO_SCHEMA = ExpressoSchema.class.getName(); 153 public static final String DBOBJECT_LABEL = "dbobject"; 154 public static final String BLOCK_TITLE = "block-title"; 155 156 160 protected DefaultAutoElement() { 161 } 162 163 170 public static synchronized AutoControllerElement getAutoControllerElement() 171 throws ControllerException { 172 if (theDefault == null) { 173 String classHandler = ConfigManager.getClassHandler("AutoControllerElement"); 174 175 if (classHandler == null) { 176 classHandler = DEFAULT_CLASS_HANDLER; 177 } 178 179 try { 180 theDefault = (AutoControllerElement) Class.forName(classHandler) 181 .newInstance(); 182 } catch (Exception e) { 183 log.error("Unable to instantiate controller renderer", e); 184 throw new ControllerException("Error instantiating controller renderer", 185 e); 186 } 187 } 188 189 return theDefault; 190 } 191 192 197 public String getErrorStyle() { 198 return ERROR_STYLE; 199 } 200 201 207 public String getNormalStyle() { 208 return DEFAULT_STYLE; 209 } 210 211 217 public String getRequiredDecorator() { 218 return "*"; 219 } 220 221 226 public String getRequiredStyle() { 227 return REQUIRED_STYLE; 228 } 229 230 242 public Block createDBObjectBlock(ControllerRequest request, ControllerResponse response, DataObject dbobj) throws ControllerException { 243 return createDBObjectBlock(request, response, 244 dbobj.getMetaData().getDescription(request.getLocale()), dbobj); 245 } 246 247 260 public Block createDBObjectBlock(ControllerRequest request, 261 ControllerResponse response, 262 String title, DataObject dbobj) 263 throws ControllerException { 264 Block b = new Block(DBOBJECT_LABEL); 265 b.add(new Output(BLOCK_TITLE, title)); 266 267 Map map = (Map ) request.getSession().getAttribute(SESSION_KEY); 268 DataObject dbobj2 = dbobj; 269 270 if (map != null) { 271 DBObject temp = (DBObject) map.get(dbobj.getClass() 272 .getName()); 273 274 if (temp != null) { 275 dbobj2 = temp; 276 } 277 } 278 279 request.getSession().removeAttribute(SESSION_KEY); 280 281 try { 282 String oneFieldName = null; 283 DataObjectMetaData objMetaData = dbobj2.getMetaData(); 284 285 for (Iterator e = objMetaData.getFieldListArray().iterator(); 286 e.hasNext();) { 287 oneFieldName = (String ) e.next(); 288 289 if (!objMetaData.isSecret(oneFieldName)) { 290 Input i = renderDBObjectField(response, dbobj2, 291 oneFieldName, dbobj2.getField(oneFieldName), false); 292 if (i != null) { 293 b.add(i); 294 } 295 296 retrieveCachedValueInForm(response, oneFieldName, i); 298 } 299 300 301 } 302 303 304 return b; 305 } catch (DBException de) { 306 throw new ControllerException(de); 307 } 308 } 309 310 321 protected void retrieveCachedValueInForm(ControllerResponse response, String oneFieldName, Input i) throws ControllerException { 322 } 323 324 325 337 public DataObject parseBlock(ControllerRequest request, 338 DataObject oneObject, ErrorCollection ec) throws ControllerException { 339 DataObject[] objs = parseBlocks(request, 340 new DataObject[]{oneObject}, ec); 341 342 if ((objs == null) || (objs.length == 0)) { 343 return null; 344 } else { 345 return objs[0]; 346 } 347 } 348 349 365 public DataObject[] parseBlocks(ControllerRequest request, 366 DataObject[] theObjects, ErrorCollection ec) throws ControllerException { 367 if (request == null) { 368 throw new IllegalArgumentException ("parseBlocks - request cannot be null"); 369 } 370 371 if (ec == null) { 372 throw new IllegalArgumentException ("parseBlocks - ec cannot be null"); 373 } 374 375 if (theObjects == null) { 376 throw new IllegalArgumentException ("parseBlocks - theObjects cannot be null"); 377 } 378 379 String [] dbobjPrefixes = new String [theObjects.length]; 380 381 for (int i = 0; i < theObjects.length; i++) { 383 dbobjPrefixes[i] = theObjects[i].getClass().getName() + 384 "."; 385 } 386 387 for (int i = 0; i < theObjects.length; i++) { 388 DataObject oneObject = theObjects[i]; 389 390 try { 395 for (Iterator it = oneObject.getMetaData().getFieldListArray() 396 .iterator(); it.hasNext();) { 397 String fieldName = (String ) it.next(); 398 DataFieldMetaData metadata = oneObject.getFieldMetaData(fieldName); 399 400 if (metadata.getAttribute("checkbox") != null) { 401 DataField dataField = oneObject.getDataField(fieldName); 402 403 if (dataField.getFieldMetaData().isBooleanType()) { 404 oneObject.set(fieldName, new Boolean (false)); 405 } else { 406 oneObject.set(fieldName, "N"); 407 } 408 } 409 } 410 } catch (DBException ex) { 411 log.error("Error checking boolean fields", ex); 412 } 413 } 414 415 for (Iterator e = request.getParameters().keySet().iterator(); 421 e.hasNext();) { 422 String oneParameterName = (String ) e.next(); 423 424 for (int i = 0; i < dbobjPrefixes.length; i++) { 425 if (oneParameterName.startsWith(dbobjPrefixes[i]) && 426 (oneParameterName.length() > dbobjPrefixes[i].length())) { 427 String fieldName = oneParameterName.substring(dbobjPrefixes[i].length()); 428 429 parseSingleInput(request, theObjects[i], fieldName, 431 oneParameterName, ec); 432 } 433 } 434 } 435 436 Map dbobjList = (Map ) request.getSession().getPersistentAttribute(SESSION_KEY); 441 442 if (dbobjList == null) { 443 dbobjList = new HashMap (theObjects.length); 444 } 445 446 for (int i = 0; i < theObjects.length; i++) { 447 try { 450 theObjects[i].setFieldsWithDefaults(); 451 } catch (DataException ex1) { 452 ec.addError(ex1.getMessage()); 453 continue; 454 } 455 456 for (Iterator it = theObjects[i].getMetaData().getFieldListArray() 457 .iterator(); it.hasNext();) { 458 String fieldName = (String ) it.next(); 459 460 try { 461 String fieldValue = theObjects[i].getField(fieldName); 462 463 if ((fieldValue == null) || (fieldValue.length() == 0)) { 464 theObjects[i].checkField(fieldName, fieldValue); 465 } 466 } catch (DBException dbe) { 467 ec.addError(dbe.getMessage()); 468 } 469 } 470 471 dbobjList.put(theObjects[i].getClass().getName(), theObjects[i]); 472 } 473 474 request.getSession().setAttribute(SESSION_KEY, dbobjList); 476 477 return theObjects; 478 } 479 480 497 public DataObject parseDBObject(ControllerRequest request, 498 DataObject oneObject, ErrorCollection ec, boolean validate) 499 throws ControllerException { 500 if (request == null) { 501 throw new IllegalArgumentException ("parseBlocks - request cannot be null"); 502 } 503 504 if (ec == null) { 505 throw new IllegalArgumentException ("parseBlocks - ec cannot be null"); 506 } 507 508 if (oneObject == null) { 509 throw new IllegalArgumentException ("parseBlocks - theObjects cannot be null"); 510 } 511 512 try { 517 for (Iterator it = oneObject.getMetaData().getFieldListArray() 518 .iterator(); it.hasNext();) { 519 String fieldName = (String ) it.next(); 520 DataFieldMetaData metadata = oneObject.getFieldMetaData(fieldName); 521 522 if (metadata.getAttribute("checkbox") != null) { 523 DataField dataField = oneObject.getDataField(fieldName); 524 525 if (dataField.getFieldMetaData().isBooleanType()) { 526 oneObject.set(fieldName, new Boolean (false)); 527 } else { 528 oneObject.set(fieldName, "N"); 529 } 530 } 531 } 532 } catch (DBException ex) { 533 log.error("Error checking boolean fields", ex); 534 } 535 536 for (Iterator i = oneObject.getMetaData().getFieldListArray().iterator(); 542 i.hasNext();) { 543 String oneFieldName = (String ) i.next(); 544 String oneParameterValue = request.getParameter(oneFieldName); 545 546 if (oneParameterValue != null) { 547 parseSingleInput(request, oneObject, oneFieldName, null, ec); 548 } 549 } 550 551 if (validate) { 552 for (Iterator it = oneObject.getMetaData().getFieldListArray() 553 .iterator(); it.hasNext();) { 554 String fieldName = (String ) it.next(); 555 556 try { 557 String fieldValue = oneObject.getField(fieldName); 558 559 if (((fieldValue == null) || (fieldValue.length() == 0)) && 560 !oneObject.getFieldMetaData(fieldName).isReadOnly()) { 561 oneObject.checkField(fieldName, fieldValue); 562 } 563 } catch (DBException dbe) { 564 ec.addError(dbe.getMessage()); 565 } 566 } 567 } 568 569 Map dbobjList = (Map ) request.getSession().getAttribute(SESSION_KEY); 575 576 if (dbobjList == null) { 577 dbobjList = new HashMap (1); 578 } 579 580 dbobjList.put(oneObject.getClass().getName(), oneObject); 581 582 request.getSession().setAttribute(SESSION_KEY, dbobjList); 583 584 return oneObject; 585 } 586 587 602 public DataObject parseDBObject(ControllerRequest request, 603 DataObject oneObject, ErrorCollection ec) throws ControllerException { 604 return parseDBObject(request, oneObject, ec, true); 605 } 606 607 661 public String parseSingleInput(ControllerRequest request, DataObject dbobj, 662 String fieldName, String parameterName, ErrorCollection ec) 663 throws ControllerException { 664 DataFieldMetaData metaData = dbobj.getFieldMetaData(fieldName); 665 Locale currentLocale = request.getLocale(); 666 667 if (parameterName == null) { 668 parameterName = fieldName; 669 } 670 671 String oneParameter = request.getParameter(parameterName); 672 673 if (oneParameter == null) { 674 ec.addError("Missing field: " + fieldName); 675 676 return null; 677 } 678 679 if (request.isFileParameter(fieldName)) { 683 try { 684 String fileName = request.getFileName(fieldName); 685 String param = request.getParameter(fieldName); 686 dbobj.getDataField(fieldName).setAttribute("fileName", fileName); 687 688 String origFileName = null; 689 String mimeType = null; 690 691 if (param != null) { 692 origFileName = getOriginalFileName(param); 693 dbobj.getDataField(fieldName).setAttribute("origFileName", 694 origFileName); 695 mimeType = MimeTypes.getMimeType(fileName, 696 request.getDataContext()).getField(MimeTypes.FLD_MIMENUMBER); 697 698 dbobj.getDataField(fieldName).setAttribute("mimeType", 701 mimeType); 702 } 703 704 if (dbobj instanceof MediaDBObject) { 705 MediaDBObject mediaObject = (MediaDBObject) dbobj; 706 mediaObject.setField(fieldName + "_mimeType", mimeType); 707 mediaObject.setField(fieldName + "_fileName", origFileName); 708 } 709 710 return param; 711 } catch (DBException ex) { 712 log.warn("Validation error for field:", ex); 713 ec.addError(ex.getMessage()); 714 715 return null; 716 } 717 } 718 719 if (!metaData.isVirtual() && !metaData.isBinaryObjectType()) { 720 try { 721 if (metaData.isDateOnlyType()) { 725 try { 726 DateFormat df = DateFormat.getDateInstance(DateFormat.SHORT, 727 currentLocale); 728 df.setLenient(true); 729 730 Date dt = df.parse(oneParameter); 731 dbobj.set(fieldName, 732 DateTime.getDateForDB(dt, request.getDataContext())); 733 734 } catch (ParseException ex) { 736 dbobj.set(fieldName, oneParameter); 739 740 } 742 } else if (metaData.isTimeType()) { 743 try { 744 DateFormat df = DateFormat.getTimeInstance(DateFormat.SHORT, 745 currentLocale); 746 df.setLenient(true); 747 748 Date dt = df.parse(oneParameter); 749 dbobj.set(fieldName, 750 DateTime.getTimeForDB(dt, request.getDataContext())); 751 752 } catch (ParseException ex) { 754 dbobj.set(fieldName, oneParameter); 755 756 } 758 } else if (metaData.isDateTimeType()) { 759 try { 760 DateFormat df = DateFormat.getDateTimeInstance(DateFormat.SHORT, 761 DateFormat.SHORT, currentLocale); 762 df.setLenient(true); 763 764 Date dt = df.parse(oneParameter); 765 dbobj.set(fieldName, 766 DateTime.getDateTimeForDB(dt, 767 request.getDataContext())); 768 769 } catch (ParseException ex) { 771 dbobj.set(fieldName, oneParameter); 772 } 773 } else if (metaData.isNumericType()) { 774 785 786 if (oneParameter != null && oneParameter.length() == 0) { 787 dbobj.set(fieldName, null); 788 } else { 789 dbobj.set(fieldName, oneParameter); 790 } 791 792 } else { 793 796 dbobj.set(fieldName, oneParameter); 800 } 801 } catch (DBException de) { 802 if (log.isInfoEnabled()) { 803 log.info("Validation error for field:", de); 804 } 805 806 ec.addError(de.getMessage()); 807 } 808 } 809 810 return null; 811 } 812 813 830 public Input renderDBObjectField(ControllerResponse response, 831 DataObject dbobj, String fieldName, String cachedValue, boolean readOnly) 832 throws ControllerException { 833 DataFieldMetaData metaData = dbobj.getFieldMetaData(fieldName); 834 DataObjectMetaData objMetadata = dbobj.getMetaData(); 835 836 if (log.isDebugEnabled()) { 837 FastStringBuffer logMsg = FastStringBuffer.getInstance(); 838 logMsg.append("entering renderDBObjectField. Parameters: \n\tdbobj="); 839 logMsg.append(dbobj.getClass().getName()); 840 logMsg.append("\n\tfieldName="); 841 logMsg.append(fieldName); 842 logMsg.append("\n\tdefaultValue="); 843 logMsg.append(cachedValue); 844 logMsg.append("\n\treadOnly="); 845 logMsg.append(readOnly); 846 log.debug(logMsg.toString()); 847 logMsg.release(); 848 } 849 850 try { 851 StringUtil.assertNotBlank(fieldName, "Field Name must not be null"); 852 853 if (dbobj == null) { 854 throw new DBException("Database object must be " + 855 "initialized before calling autoField"); 856 } 857 858 if (metaData.isVirtual()) { 859 return null; 860 } 861 862 String oneFieldValue = dbobj.getField(fieldName); 863 864 if (oneFieldValue == null) { 866 oneFieldValue = (""); 867 } 868 869 int fieldSize = metaData.getLengthInt(); 873 fieldSize = fieldSize + 2; 874 875 876 int displayFieldSize = fieldSize; 877 878 if (displayFieldSize > 60) { 884 displayFieldSize = 60; 885 } 886 887 if (displayFieldSize < 10) { 891 if (metaData.isNumericType()) { 892 displayFieldSize = 15; 893 } else if (metaData.isDateTimeType()) { 894 displayFieldSize = 30; 895 } else if (metaData.isDateOnlyType()) { 896 displayFieldSize = 15; 897 } else if (metaData.isTimeType()) { 898 displayFieldSize = 15; 899 } else { 900 displayFieldSize = 10; 901 } 902 } 903 904 Input oneField = new Input(fieldName); 905 oneField.setDisplayLength(displayFieldSize); 906 907 if (fieldSize > displayFieldSize) { 908 oneField.setMaxLength(fieldSize); 909 } else { 910 oneField.setMaxLength(displayFieldSize + 2); 911 } 912 913 if ((cachedValue != null) && (cachedValue.length() > 0)) { 914 oneFieldValue = cachedValue; 915 } 916 917 920 oneField.setAttribute(Input.ATTRIBUTE_TYPE, metaData.getTypeString()); 930 931 Iterator dbAttribs = metaData.getAttributesIterator(); 933 934 if (dbAttribs != null) { 935 String oneAttrib = null; 936 Object oneAttribValue = null; 937 938 while (dbAttribs.hasNext()) { 939 oneAttrib = (String ) dbAttribs.next(); 940 oneAttribValue = metaData.getAttribute(oneAttrib); 941 942 if (oneAttribValue != null) { 943 oneField.setAttribute(oneAttrib, 944 oneAttribValue.toString()); 945 } 946 } 947 } 948 949 { 951 String description; 952 953 Locale locale = response.getLocale(); 957 958 Object [] args = {}; 968 String stringCode = objMetadata.getDescription(fieldName); 969 970 try { 971 description = com.jcorporate.expresso.core.i18n.Messages.getString(objMetadata.getSchema(), 972 locale, stringCode, args); 973 } catch (IllegalArgumentException iae) { 974 if (log.isDebugEnabled()) { 975 log.debug("DefaultAutoElement: Key [" + stringCode + 976 "] not found in " + objMetadata.getSchema()); 977 } 978 979 description = null; 980 981 if (!objMetadata.getSchema().equals(EXPRESSO_SCHEMA)) { 982 try { 983 description = com.jcorporate.expresso.core.i18n.Messages.getString(EXPRESSO_SCHEMA, 984 locale, stringCode, args); 985 } catch (IllegalArgumentException e) { 986 description = null; 987 } 988 } 989 } 990 991 if ((description == null) || (description.length() == 0)) { 992 description = stringCode; 993 } 994 995 if (metaData.isDateType() && !metaData.isReadOnly()) { 999 String pattern = DateTime.getDateFormatString(metaData.isDateOnlyType(), 1001 locale); 1002 1003 if (pattern.length() > 0) { 1004 pattern = " (" + pattern + ")"; 1005 } 1006 1007 oneField.setLabel(description + pattern); 1008 } else { 1009 oneField.setLabel(description); 1010 } 1011 } 1012 1013 String dbobjStatus = dbobj.getStatus(); 1014 1015 1016 1017 1018 1019 1020 if (metaData.isReadOnly()) { 1021 readOnly = true; 1022 } else if (metaData.isVirtual()) { 1023 readOnly = true; 1024 } else if (!dbobjStatus.equals(DBObject.STATUS_NEW)) { 1025 if (dbobj.getMetaData().getKeyFieldListArray().contains(fieldName)) { 1026 readOnly = true; 1028 } 1029 } 1030 1031 1036 if (dbobjStatus.equals(DBObject.STATUS_NEW)) { 1037 if (metaData.isReadOnly()) { 1038 return null; 1039 } 1040 } 1041 1042 String displayAttrib = (String ) metaData.getAttribute("display"); 1043 1044 if (displayAttrib != null) { 1045 oneField.setAttribute("display", displayAttrib); 1046 } 1047 1048 if ((cachedValue != null) && (cachedValue.length() > 0)) { 1051 if ((oneFieldValue == null) || (oneFieldValue.length() == 0)) { 1052 oneField.setDefaultValue(cachedValue); 1053 } 1054 } 1055 1056 if (readOnly) { 1057 oneField = renderReadOnlyInput(response, oneField, dbobj, 1058 fieldName, oneFieldValue); 1059 } else { 1060 1061 String oneFieldSize = Integer.toString(displayFieldSize); 1062 oneField = renderReadWriteInput(response, oneField, dbobj, 1063 fieldName, oneFieldValue, oneFieldSize); 1064 } 1065 1066 1067 cachedValue = oneField.getDefaultValue(); 1068 1069 if (!StringUtil.notNull(cachedValue).equals("")) { 1070 oneField.setDefaultValue(cachedValue); 1071 1072 if (log.isDebugEnabled()) { 1073 log.debug("Cached value for '" + fieldName + "' was '" + 1074 cachedValue + "'"); 1075 } 1076 } else { 1077 1078 String defValue = dbobj.getMetaData().getDefaultValue(fieldName); 1079 1080 if (defValue != null) { 1081 oneField.setDefaultValue(defValue); 1082 } 1083 } 1084 1085 return oneField; 1086 } catch (DBException dbe) { 1087 throw new ControllerException("Error rendering input.", dbe); 1088 } 1089 } 1090 1091 1100 protected String displayValue(DataFieldMetaData metaData, Date dt, Locale l) { 1101 if (dt == null) { 1102 return ""; 1103 } 1104 1105 if (metaData.isDateOnlyType()) { 1106 DateFormat df = DateFormat.getDateInstance(DateFormat.SHORT, l); 1107 df = modifyDateFormat(df); 1108 1109 return df.format(dt); 1110 } else if (metaData.isTimeType()) { 1111 DateFormat df = DateFormat.getTimeInstance(DateFormat.SHORT, l); 1112 df = modifyDateFormat(df); 1113 1114 return df.format(dt); 1115 } else { 1116 DateFormat df = DateFormat.getDateTimeInstance(DateFormat.SHORT, 1117 DateFormat.SHORT, l); 1118 df = modifyDateFormat(df); 1119 1120 return df.format(dt); 1121 } 1122 } 1123 1124 1135 protected String displayValue(DataFieldMetaData metaData, 1136 String fieldValue, String fieldName, Locale l) 1137 throws ControllerException { 1138 try { 1139 if (metaData.getTypeString().equalsIgnoreCase("money")) { 1140 if (fieldValue.length() > 0) { 1141 return new Format("%-10.2f").form(new Double (fieldValue).doubleValue()); 1142 } 1143 } else { 1144 return fieldValue; 1145 } 1146 } catch (NumberFormatException ne) { 1147 throw new ControllerException("Number for field not in a " + 1148 "valid numeric format:" + fieldValue + ":" + ne.getMessage()); 1149 } 1150 1151 return null; 1152 } 1153 1154 1155 1169 protected Input renderReadOnlyInput(ControllerResponse response, 1170 Input oneField, DataObject dbobj, String fieldName, String oneFieldValue) 1171 throws DBException, ControllerException { 1172 DataFieldMetaData metaData = dbobj.getFieldMetaData(fieldName); 1173 DataObjectMetaData objMetaData = dbobj.getMetaData(); 1174 1175 oneField.setAttribute(Input.ATTRIBUTE_READONLY, "Y"); 1176 oneField.setAttribute(Input.ATTRIBUTE_ORIGINAL_VALUE, oneFieldValue); 1177 1178 if (metaData.isMultiValued()) { 1179 java.util.List values = dbobj.getValidValuesList(fieldName); 1180 1181 if (values == null) { 1182 throw new DBException("Valid values for field " + fieldName + 1183 " from object " + objMetaData.getName() + " were null"); 1184 } 1185 1186 String fieldValue = null; 1187 ValidValue oneVV = null; 1188 1189 for (Iterator e = values.iterator(); e.hasNext();) { 1190 oneVV = (ValidValue) e.next(); 1191 1192 if (oneVV.getValue().equals(oneFieldValue)) { 1193 fieldValue = oneVV.getDescription(); 1194 } 1195 } 1196 1197 if (fieldValue == null) { 1198 oneField.setDefaultValue(oneFieldValue); 1199 } else { 1200 oneField.setDefaultValue(fieldValue); 1201 } 1202 } else { 1203 if (metaData.isDateType()) { 1204 if ((oneFieldValue == null) || (oneFieldValue.length() == 0)) { 1205 oneField.setDefaultValue(displayValue(metaData, 1206 oneFieldValue, fieldName, response.getLocale())); 1207 } else { 1208 oneField.setDefaultValue(displayValue(metaData, 1209 dbobj.getField(fieldName), fieldName, 1210 response.getLocale())); 1211 } 1212 } else { 1213 oneField.setDefaultValue(displayValue(metaData, oneFieldValue, 1214 fieldName, response.getLocale())); 1215 } 1216 } 1217 1218 return oneField; 1219 } 1220 1221 1237 protected void renderReadWriteBlob(ControllerResponse response, 1238 Input oneField, DataObject dbobj, String fieldName, 1239 String oneFieldValue, String oneFieldSize) 1240 throws DBException, ControllerException { 1241 oneField.setType("file"); 1242 oneField.setDisplayLength(60); 1243 1244 if (dbobj instanceof NestableDataObject) { 1245 String tempFieldName = fieldName; 1246 fieldName = ((NestableDataObject) dbobj).getFieldFromNestedName(fieldName); 1247 dbobj = ((NestableDataObject) dbobj).getNestedFromFieldName(tempFieldName); 1248 } 1249 1250 if (dbobj instanceof MediaDBObject) { 1251 MediaDBObject media = (MediaDBObject) dbobj; 1252 String mimeNumber = media.getField(fieldName + "_mimeType"); 1253 1254 if (mimeNumber == null) { 1255 return; 1256 } 1257 1258 MimeTypes mt = new MimeTypes(SecuredDBObject.SYSTEM_ACCOUNT); 1259 mt.setField(MimeTypes.FLD_MIMENUMBER, mimeNumber); 1260 1261 if (mt.find()) { 1262 oneField.setAttribute("IconURL", mt.getIconURL()); 1263 } else { 1264 log.error("Unable to find mime type for mime number: " + 1265 mimeNumber); 1266 } 1267 1268 } 1274 } 1275 1276 1290 protected Input renderReadWriteInput(ControllerResponse response, 1291 Input oneField, DataObject dbobj, String fieldName, 1292 String oneFieldValue, String oneFieldSize) 1293 throws DBException, ControllerException { 1294 if (log.isDebugEnabled()) { 1295 log.debug("Entering DefaultAutoElement.renderReadWriteInput"); 1296 } 1297 1298 DataFieldMetaData metaData = dbobj.getFieldMetaData(fieldName); 1299 DataObjectMetaData dbobjMetaData = dbobj.getMetaData(); 1300 1301 DataField df = dbobj.getDataField(fieldName); 1302 1303 if (df.getAttribute(DBObject.ATTRIBUTE_ERROR) != null) { 1307 oneField.setAttribute(Input.ATTRIBUTE_CSS_STYLE, 1308 getErrorStyle()); 1309 } else if (!metaData.allowsNull()) { 1310 oneField.setAttribute(Input.ATTRIBUTE_CSS_STYLE, 1311 getRequiredStyle()); 1312 } else { 1313 oneField.setAttribute(Input.ATTRIBUTE_CSS_STYLE, 1314 getNormalStyle()); 1315 } 1316 1317 if (!metaData.allowsNull()) { 1322 oneField.setLabel(oneField.getLabel() + 1323 getRequiredDecorator()); 1324 } 1325 1326 1327 1328 if (metaData.isMultiValued()) { 1329 oneField.setAttribute(Input.ATTRIBUTE_MULTIVALUED, "Y"); 1330 oneField.setAttribute(Input.ATTRIBUTE_DROPDOWN, "Y"); 1331 oneField.setType(Input.ATTRIBUTE_DROPDOWN); 1332 1333 java.util.List values = dbobj.getValidValuesList(fieldName); 1334 1335 if (values == null) { 1337 throw new DBException("Valid values for field " + fieldName + 1338 " from object " + dbobjMetaData.getName() + " were null"); 1339 } 1340 1341 oneField.setValidValues(new Vector (values)); 1342 oneField.setDefaultValue(oneFieldValue); 1343 } else { 1344 if (metaData.isBinaryObjectType()) { 1348 renderReadWriteBlob(response, oneField, dbobj, fieldName, 1349 oneFieldValue, oneFieldSize); 1350 } 1351 1352 if (metaData.isDateType()) { 1353 oneField.setDisplayLength(10); 1354 1355 if ((oneFieldValue == null) || (oneFieldValue.length() == 0)) { 1357 oneField.setDefaultValue(displayValue(metaData, 1358 oneFieldValue, fieldName, response.getLocale())); 1359 } else { 1360 oneField.setDefaultValue(displayValue(metaData, 1361 dbobj.getDataField(fieldName).asDate(), 1362 response.getLocale())); 1363 } 1364 } else { 1365 if (metaData.isCharacterLongObjectType()) { 1366 oneField.setDisplayLength(80); 1367 oneField.setLines(4); 1368 oneField.setMaxLength(4096); 1369 oneField.setType("text"); 1370 } else { 1371 } 1373 1374 oneField.setDefaultValue(displayValue(metaData, oneFieldValue, 1375 fieldName, response.getLocale())); 1376 } 1377 } 1378 1379 if (log.isDebugEnabled()) { 1380 if (oneField != null) { 1381 log.debug("Leaving DefaultAutoElement.renderReadWriteInput " + 1382 oneField.toString()); 1383 } else { 1384 log.debug("Leaving DefaultAutoElement.renderReadWriteInput oneField=null"); 1385 } 1386 } 1387 1388 return oneField; 1389 } 1390 1391 1399 1400 1414 1422 1423 1439 1446 1471 1479 private String getOriginalFileName(String fileNameParam) { 1480 if (fileNameParam == null) { 1481 return null; 1482 } 1483 1484 int lastSlash = fileNameParam.lastIndexOf("\\"); 1485 1486 if (lastSlash < 0) { 1487 lastSlash = fileNameParam.lastIndexOf("/"); 1488 1489 if (lastSlash < 0) { 1490 return fileNameParam; 1491 } 1492 } 1493 1494 return fileNameParam.substring(lastSlash + 1); 1495 } 1496 1497 1509 1546 1556 1591 1598 private DateFormat modifyDateFormat(DateFormat df) { 1599 SimpleDateFormat sdf = null; 1600 1601 try { 1602 sdf = (SimpleDateFormat ) df; 1603 } catch (ClassCastException cce) { 1604 return df; 1605 } 1606 1607 String sTemp = sdf.toPattern(); 1608 int iLen = sTemp.length(); 1609 int i = sTemp.lastIndexOf('y') + 1; 1610 sTemp = sTemp.substring(0, i) + "yy" + 1611 ((i < iLen) ? sTemp.substring(i, iLen) : ""); 1612 sdf.applyPattern(sTemp); 1613 1614 return sdf; 1615 } 1616} 1617 | Popular Tags |