1 5 6 package com.raptus.owxv3.api.components; 7 8 import java.text.*; 9 import java.util.*; 10 11 import javax.servlet.http.HttpServletRequest ; 12 13 import org.apache.struts.action.ActionMapping; 14 15 import com.raptus.owxv3.*; 16 import com.raptus.owxv3.api.*; 17 18 19 20 42 public class CmpFieldsBean extends ComponentBean 43 { 44 47 protected String titleFieldName = null; 48 49 50 53 protected Locale activeLocale = null; 54 55 58 protected FieldsSelector fieldsSel = null; 59 60 61 64 protected Hashtable fieldDefList=null; 65 66 69 protected Hashtable fieldErrorMsgs=null; 70 71 74 protected Vector fieldOrder; 75 76 79 protected Hashtable simpleFieldLabel; 80 81 84 protected String currentField; 85 86 87 90 protected int simpleFieldCount; 91 92 public CmpFieldsBean() 93 { 94 fieldDefList=new Hashtable(); 95 fieldOrder=new Vector(); 96 fieldErrorMsgs=new Hashtable(); 97 simpleFieldCount=0; 98 } 99 100 public void addFieldDefinition(FieldDefinition fd) 101 { 102 fieldDefList.put(fd.getName(),fd); 103 if(fd.getWysiwyg()==false) 105 { 106 simpleFieldCount++; 108 } 109 if(fd.getMultiLanguage()) 110 { 111 fieldsSel.initLocalizedField(fd); 112 } 113 else 114 { 115 fieldsSel.initField(fd); 116 } 117 118 if( !fieldOrder.contains(fd.getName()) ) 119 { 120 fieldOrder.addElement(fd.getName()); 121 122 } 123 } 124 125 public Locale getActiveLocale() { return activeLocale; } 126 public void setActiveLocale(Locale al) { this.activeLocale = al; } 127 128 public String getTitleFieldName() { return titleFieldName; } 129 public void setTitleFieldName(String s) { this.titleFieldName=s;} 130 131 public String getCurrentField() { return currentField; } 132 public void setCurrentField(String s) { this.currentField=s;} 133 134 135 public void setSimpleFieldLabel(Hashtable h) 136 { 137 simpleFieldLabel=h; 138 } 139 140 public Vector getFieldOrder() 141 { 142 return fieldOrder; 143 } 144 145 148 public void reset(ActionMapping mapping, HttpServletRequest request) 149 { 150 151 super.reset(mapping, request); 152 } 153 154 157 public FieldsSelector getFieldsSelector() { return fieldsSel; } 158 159 162 public void setFieldsSelector(FieldsSelector fs) 163 { 164 this.fieldsSel = fs; 165 setActiveLocale(getCurrLocale()); 166 } 168 169 172 public void switchLocale(String switchToLocale) 173 { 174 if(switchToLocale != null) 175 { 176 if(!getActiveLocale().toString().equalsIgnoreCase(switchToLocale)) 177 { 178 PairOfObjects po = LocaleManager.stripLocaleString(switchToLocale); 179 if(po != null && po.isValid()) 180 { 181 setActiveLocale( new Locale((String ) po.getObjectOne(), (String ) po.getObjectTwo()) ); 182 } 184 } 185 } 186 else 187 LoggingManager.log("Failed to switch locale, because parameter was NULL!", this); 188 } 189 193 public String getFieldValue(String fieldname) 194 { 195 return getFieldValue(fieldname,activeLocale); 196 } 197 198 202 public String getFieldValue(String fieldname,Locale l) 203 { 204 FieldDefinition fd=(FieldDefinition)fieldDefList.get(fieldname); 205 if(fd==null) 206 { 207 LoggingManager.log("Unknown field name:"+fieldname+" !",this); 208 return ""; 209 } 210 if(fd.getMultiLanguage()) 211 { 212 GResLocalizedField gf=fieldsSel.getLocalizedField(fieldname); 213 if(gf==null) 214 { 215 return fd.getDefaultVal(l.toString()); 217 } 218 else 219 { 220 String s=gf.getValue(l); 221 if(s==null) return fd.getDefaultVal(l.toString()); 222 return s; 223 } 224 } else 226 { 227 GResField gf=fieldsSel.getField(fieldname); 228 229 if(gf==null) 230 { 231 return fd.getDefaultVal(l.toString()); 233 } 234 else 235 { 236 if(gf.getFieldType()==Constants.FIELDTYPE_NUMBER) 238 { 239 Double d=gf.getNumberValue(); 240 if(d==null) return ""; 241 return d.toString(); 242 } 243 if(gf.getFieldType()==Constants.FIELDTYPE_TEXT) return gf.getTextValue(); 244 if(gf.getFieldType()==Constants.FIELDTYPE_DATE) 245 { 246 Date d=gf.getDateValue(); 247 if(d==null) return ""; 248 SimpleDateFormat df=(SimpleDateFormat)DateFormat.getDateInstance(DateFormat.SHORT,activeLocale); 249 df.applyPattern(Constants.DEFAULT_DATEPATTERN); 250 return df.format(d); 251 } 252 if(gf.getFieldType()==Constants.FIELDTYPE_PRICE) return gf.getPriceValue(); 253 254 } 255 } 257 return ""; 258 } 259 260 263 public String getFieldLabel(String fieldname) 264 { 265 FieldDefinition fd=(FieldDefinition)fieldDefList.get(fieldname); 266 if(fd==null) 267 { 268 LoggingManager.log("Unknown field name:"+fieldname+" !",this); 269 return ""; 270 } 271 else 272 { 273 return fd.getLabel(getCurrLocale().toString()); 274 } } 276 277 280 public String getFieldErrorMsg(String fieldname) 281 { 282 String s=(String )fieldErrorMsgs.get(fieldname); 283 if(s==null) return ""; 284 return s; 285 } 286 287 290 public void setFieldErrorMsg(String fieldname,String errorMsg) 291 { 292 fieldErrorMsgs.put(fieldname,errorMsg); 293 } 294 295 298 public void clearFieldErrorMsgs() 299 { 300 fieldErrorMsgs.clear(); 301 } 302 303 306 public int getFieldType(String fieldname) 307 { 308 FieldDefinition fd=(FieldDefinition)fieldDefList.get(fieldname); 309 if(fd==null) return -1; 310 return fd.getType(); 311 } 312 313 316 317 public boolean isWysiwyg(String fieldName) 318 { 319 FieldDefinition fd=(FieldDefinition)fieldDefList.get(fieldName); 320 if(fd==null) return false; 321 return fd.getWysiwyg(); 322 } 323 324 327 public boolean isEmptyAllowed(String fieldName) 328 { 329 FieldDefinition fd=(FieldDefinition)fieldDefList.get(fieldName); 330 if(fd==null) return false; 331 return fd.getEmpty(); 332 } 333 334 335 338 public boolean isMultiLanguage(String fieldName) 339 { 340 FieldDefinition fd=(FieldDefinition)fieldDefList.get(fieldName); 341 if(fd==null) return false; 342 return fd.getMultiLanguage(); 343 } 344 345 public String getTitleValue() 346 { 347 String s=getFieldValue(titleFieldName,getCurrLocale()); 348 if(s==null) return "No title"; 349 350 return s; 351 } 352 353 356 public Vector getComboLabels() 357 { 358 Vector ret=new Vector(); 359 FieldDefinition fd=null; 360 if(simpleFieldCount>0) ret.addElement( simpleFieldLabel.get(getCurrLocale().toString()) ); 361 362 if(fieldOrder!=null) 363 { 364 for(int i=0;i<fieldOrder.size();i++) 365 { 366 fd=(FieldDefinition)fieldDefList.get( fieldOrder.elementAt(i) ); 367 if(fd.getWysiwyg()==true) 368 { 369 ret.addElement( fd.getLabel(getCurrLocale().toString()) ); 370 } 371 } } 373 return ret; 374 } 375 376 379 public Vector getComboValues() 380 { 381 Vector ret=new Vector(); 382 FieldDefinition fd=null; 383 if(simpleFieldCount>0) ret.addElement( "all" ); 384 if(fieldOrder!=null) 386 { 387 for(int i=0;i<fieldOrder.size();i++) 388 { 389 fd=(FieldDefinition)fieldDefList.get( fieldOrder.elementAt(i) ); 390 if(fd.getWysiwyg()==true) 391 { 392 ret.addElement( fd.getName() ); 393 } 394 } } return ret; 398 } 399 400 410 411 421 } 422 423 | Popular Tags |