1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605
|
package com.mtsa.ebonus.core.gui.product;
import java.util.ArrayList;
import java.util.List;
import javax.faces.component.html.HtmlDataTable;
import javax.faces.component.html.HtmlInputText;
import javax.faces.context.FacesContext;
import javax.faces.event.ValueChangeEvent;
import javax.faces.model.SelectItem;
import com.mtsa.common.control.ErrorController;
import com.mtsa.common.error.ErrorLevel;
import com.mtsa.ebonus.core.business.CurrencyService;
import com.mtsa.ebonus.core.business.LanguageServiceImpl;
import com.mtsa.ebonus.core.business.product.*;
import com.mtsa.ebonus.core.gui.UtilGui;
public class ProductGui {
/* Error, logs */
private static ErrorController ec = ErrorController.getErrorController(ProductGui.class.getName(), ErrorLevel.DEBUG);
private static final String ERR_SERVICE_IS_NULL = "GUI2010";
private static final String ERR_GET_ENTITIES = "GUI2020";
private static final String ERR_SEARCH = "GUI2030";
private static final String ERR_DETAIL = "GUI2040";
private static final String ERR_CREATE = "GUI2050";
private static final String ERR_SAVE = "GUI2060";
private static final String ERR_DELETE = "GUI2070";
private static final String ERR_DELETE_LIST = "GUI2080";
private String brand;
private String currency;
private String saleType;
private String customerType;
private String budgetFamily;
private String intColor;
private String outColor;
private String marketId;
/* Services */
private ProductService productService;
private CurrencyService currencyService;
private CustomerTypeService customerTypeService;
private BudgetFamilyService budgetFamilyService;
private IntColorService intColorService;
private OutColorService outColorService;
private SaleTypeService saleTypeService;
private BrandService brandService;
/* Business Objects */
private List<Product> dataList;
private Product selectedData;
/* General attribute */
private String txtSearch;
private int txtDataTableSize;
private int txtResultSetSize;
private int txtResultSetPosition;
private HtmlInputText txtId = new HtmlInputText();
private String lblShowHide;
private boolean isMutliLanguageDisplay = false;
//Does have we to hide a field text ?
private boolean disabled;
/* Binding */
// private javax.faces.component.html.HtmlDataTable jsfDataList = new
// javax.faces.component.html.HtmlDataTable(); // for Core
private org.apache.myfaces.component.html.ext.HtmlDataTable jsfDataList = new org.apache.myfaces.component.html.ext.HtmlDataTable();
private HtmlInputText jsfDescription = new HtmlInputText();
private HtmlDataTable jsfMessageList = new HtmlDataTable();
// Navigation rules
private final String GO_LIST = "goProductList";
private final String GO_DETAIL = "goProductDetail";
private final String GO_ERROR = "goError";
// TODO initialize with project constants
private String CURRENT_USER = "alja";
private String FROM = "ProductGui";
public BrandService getBrandService() {
return brandService;
}
public void setBrandService(BrandService brandService) {
this.brandService = brandService;
}
public BudgetFamilyService getBudgetFamilyService() {
return budgetFamilyService;
}
public void setBudgetFamilyService(BudgetFamilyService budgetFamilyService) {
this.budgetFamilyService = budgetFamilyService;
}
public CurrencyService getCurrencyService() {
return currencyService;
}
public void setCurrencyService(CurrencyService currencyService) {
this.currencyService = currencyService;
}
public CustomerTypeService getCustomerTypeService() {
return customerTypeService;
}
public void setCustomerTypeService(CustomerTypeService customerTypeService) {
this.customerTypeService = customerTypeService;
}
public IntColorService getIntColorService() {
return intColorService;
}
public void setIntColorService(IntColorService intColorService) {
this.intColorService = intColorService;
}
public OutColorService getOutColorService() {
return outColorService;
}
public void setOutColorService(OutColorService outColorService) {
this.outColorService = outColorService;
}
public SaleTypeService getSaleTypeService() {
return saleTypeService;
}
public void setSaleTypeService(SaleTypeService saleTypeService) {
this.saleTypeService = saleTypeService;
}
public String getBudgetFamily() {
return budgetFamily;
}
public void setBudgetFamily(String budgetFamily) {
this.selectedData.setBudgetFamily(budgetFamily, this.getMarketId(), budgetFamilyService);
this.budgetFamily = budgetFamily;
}
public String getCurrency() {
return currency;
}
public void setCurrency(String currency) {
this.selectedData.setCurrency(currency, currencyService);
this.currency = currency;
}
public String getCustomerType() {
return customerType;
}
public void setCustomerType(String customerType) {
this.selectedData.setCustomerType(customerType, this.getMarketId(), customerTypeService);
this.customerType = customerType;
}
public String getIntColor() {
return intColor;
}
public void setIntColor(String intColor) {
this.selectedData.setIntColor(intColor, this.getMarketId(), intColorService);
this.intColor = intColor;
}
public String getOutColor() {
return outColor;
}
public void setOutColor(String outColor) {
this.selectedData.setOutColor(outColor, this.getMarketId(), outColorService);
this.outColor = outColor;
}
public String getSaleType() {
return saleType;
}
public void setSaleType(String saleType) {
this.selectedData.setSaleType(saleType, this.getMarketId(), saleTypeService);
this.saleType = saleType;
}
public String getBrand() {
return brand;
}
public void setBrand(String brand) {
this.selectedData.setBrand(brand, this.getMarketId(), brandService);
this.brand = brand;
}
public String getMarketId() {
return marketId;
}
public void setMarketId(String marketId) {
//this.selectedData.setMarketId(marketId);
this.marketId = marketId;
}
// ------------------- CONSTRUCTOR(S) -----------------------------//
// This part is used to initialized some value, at the first page load.
public ProductGui() {
// ATTENTION !! Here is "productService" always NULL (due to Spring) !!
// init variables
// TODO initialize with user constants
txtDataTableSize = 10;
txtResultSetSize = 55;
marketId = "3104";
txtResultSetPosition = 0;
lblShowHide = UtilGui.getTranslationText("common_multiLanguage");
isMutliLanguageDisplay = false;
}
// ----------- CLASS ATTRIBUTES GETTER and SETTER -----------------//
public ProductService getProductService() {
return this.productService;
}
public void setProduct(ProductService productService) {
this.productService = productService;
}
public int getTxtResultSetPosition() {
return this.txtResultSetPosition;
}
public void setTxtResultSetPosition(int txtResultSetPosition) {
this.txtResultSetPosition = txtResultSetPosition;
}
public int getTxtResultSetSize() {
return this.txtResultSetSize;
}
public void setTxtResultSetSize(int txtResultSetSize) {
this.txtResultSetSize = txtResultSetSize;
}
public int getTxtDataTableSize() {
return this.txtDataTableSize;
}
public void setTxtDataTableSize(int txtDataTableSize) {
this.txtDataTableSize = txtDataTableSize;
}
public String getTxtSearch() {
return this.txtSearch;
}
public void setTxtSearch(String txtSearch) {
this.txtSearch = txtSearch;
}
public String getLblShowHide() {
return this.lblShowHide;
}
public void setLblShowHide(String lblShowHide) {
this.lblShowHide = lblShowHide;
}
/* Data specific */
public List<Product> getDataList() {
ec.log("Begin - getDataList()");
if (this.txtSearch == null || this.txtSearch.length() == 0) {
try {
this.dataList = productService.getProducts(marketId, this.txtResultSetPosition, this.txtResultSetSize);
ec.log("getDataList(), dataList size: " + dataList.size());
} catch (RuntimeException ex) {
ec.handleError(ERR_GET_ENTITIES, ex);
UtilGui.setJSFErrorMessage("getDataList");
}
}else {
this.dataList = productService.getProducts(marketId,this.txtSearch, this.txtResultSetPosition, this.txtResultSetSize);
}
// }
ec.log("End - getDataList()");
return this.dataList;
}
public void setDataList(List<Product> dataList) {
ec.log("Begin - setDataList()");
this.dataList = dataList;
ec.log("End - setDataList()");
}
public Product getSelectedData() {
return this.selectedData;
}
public void setSelectedData(Product selectedData) {
this.selectedData = selectedData;
}
// ------------------- BINDING -----------------------------//
public HtmlDataTable getJsfDataList() {
return this.jsfDataList;
}
public void setJsfDataList(org.apache.myfaces.component.html.ext.HtmlDataTable jsfDataList) {
this.jsfDataList = jsfDataList;
}
public HtmlInputText getJsfDescription() {
return this.jsfDescription;
}
public void setJsfDescription(HtmlInputText jsfDescription) {
this.jsfDescription = jsfDescription;
}
public HtmlDataTable getJsfMessageList() {
return this.jsfMessageList;
}
public void setJsfMessageList(HtmlDataTable jsfMessageList) {
this.jsfMessageList = jsfMessageList;
}
// ------------------- OPERATIONS -----------------------------//
/**
* Use binding on "txtSearch" for searching entities that have "txtSearch"
* for part of his description
*
* @return action used by JSF (go...) => GO_LIST
*/
public String search() {
ec.log("Begin - search(), txtSearch:" + txtSearch);
if (productService == null) {
ec.handleError(ERR_SERVICE_IS_NULL);
} else {
try {
if (txtSearch != null && txtSearch.trim().length() > 0) {
txtSearch = txtSearch.trim();
this.dataList = productService.getProducts(marketId,txtSearch, this.txtResultSetPosition,
this.txtResultSetSize);
} else {
this.dataList = productService.getProducts(marketId,this.txtResultSetPosition,
this.txtResultSetSize);
}
ec.log("End - search(), dataList size: " + dataList.size());
return GO_LIST;
} catch (RuntimeException ex) {
ec.handleError(ERR_SEARCH, ex);
UtilGui.setJSFErrorMessage("error_search", ex);
return GO_LIST;
}
}
// If any other error...
return GO_ERROR;
}
/**
*
* Diplay details of entity actually in "selectedData"
*
* @return action used by JSF (go...) => GO_DETAIL
*/
public String detail() {
ec.log("Begin - detail()");
if (productService == null) {
ec.handleError(ERR_SERVICE_IS_NULL);
} else {
try {
String brandId = UtilGui.retrieveStringFromContext("brandId");
String marketId = UtilGui.retrieveStringFromContext("marketId");
ec.log("detail(), selectedProductId:" + brandId + " selected marketId " + marketId);
selectedData = productService.getProduct(brandId, marketId);
ec.log("End - detail(), selectedData:" + selectedData);
txtId.setDisabled(true);
this.setDisabled(true);
return GO_DETAIL;
} catch (RuntimeException ex) {
ec.handleError(ERR_DETAIL, ex);
UtilGui.setJSFErrorMessage("error_detail", ex);
return GO_LIST;
}
}
// If any other error...
return GO_ERROR;
}
/**
*
* Create a new entity and set it as "selectedData"
*
* @return action used by JSF (go...) => GO_DETAIL
*/
public String create() {
ec.log("Begin - create()");
if (productService == null) {
ec.handleError(ERR_SERVICE_IS_NULL);
} else {
try {
selectedData = productService.getNewProduct();
dataList.add(selectedData);
selectedData.trace(CURRENT_USER, FROM);
txtId.setDisabled(false);
this.setDisabled(false);
ec.log("End - create(), selectedData:" + selectedData);
return GO_DETAIL;
} catch (RuntimeException ex) {
ec.handleError(ERR_CREATE, ex);
UtilGui.setJSFErrorMessage("error_create", ex);
return GO_LIST;
}
}
// If any other error...
return GO_ERROR;
}
/**
*
* Save entity actually in "selectedData"
*
* @return action used by JSF (go...) => GO_LIST
*/
public String save() {
ec.log("Begin - save(), selectedData:" + selectedData);
if (productService == null) {
ec.handleError(ERR_SERVICE_IS_NULL);
} else {
try {
productService.saveProduct(selectedData, CURRENT_USER, FROM);
this.updateSelectedFromDataList();
ec.log("End - save()");
return GO_LIST;
} catch (RuntimeException ex) {
ec.handleError(ERR_SAVE, ex);
UtilGui.setJSFErrorMessage("error_save", ex);
return GO_DETAIL;
}
}
// If any other error...
return GO_ERROR;
}
private void updateSelectedFromDataList() {
ec.log("Begin - updateSelectedFromDataList(), dataList size: " + dataList.size());
for (int i = 0; i < dataList.size(); i++) {
Product brand = dataList.get(i);
if (brand.equals(selectedData))
dataList.set(i, selectedData);
}
ec.log("End - updateSelectedFromDataList(), dataList size: " + dataList.size());
}
/**
*
* Delete entity actually in "selectedData"
*
* @return action used by JSF (go...) => GO_LIST
*/
public String delete() {
ec.log("Begin - delete(), selectedData:" + selectedData);
if (productService == null) {
ec.handleError(ERR_SERVICE_IS_NULL);
} else {
try {
if (selectedData == null) {
String brandId = UtilGui.retrieveStringFromContext("brandId");
String marketId = UtilGui.retrieveStringFromContext("marketId");
selectedData = productService.getProduct(brandId, marketId);
ec.log("delete(), selectedData:" + selectedData);
}
productService.deleteProduct(selectedData, CURRENT_USER, FROM);
this.deleteSelectedFromDataList();
selectedData = null;
ec.log("End - delete()");
return GO_LIST;
} catch (RuntimeException ex) {
ec.handleError(ERR_DELETE, ex);
UtilGui.setJSFErrorMessage("error_delete", ex);
return GO_DETAIL;
}
}
// If any other error...
return GO_ERROR;
}
/**
*
* Delete entities with "isSelected" (BU attribute) to true
*
* @return action used by JSF (go...) => GO_LIST
*/
public String deleteList() {
ec.log("Begin - deleteList()");
if (productService == null) {
ec.handleError(ERR_SERVICE_IS_NULL);
} else {
try {
for (int i = 0; i < dataList.size(); i++) {
selectedData = dataList.get(i);
if (selectedData.getIsSelected()) {
productService.deleteProduct(selectedData, CURRENT_USER, FROM);
this.deleteSelectedFromDataList();
ec.log("deleteList(), selectedData to delete:" + selectedData);
}
}
selectedData = null;
ec.log("End - deleteList()");
return GO_LIST;
} catch (RuntimeException ex) {
ec.handleError(ERR_DELETE_LIST, ex);
UtilGui.setJSFErrorMessage("error_delete_list", ex);
return GO_LIST;
}
}
// If any other error...
return GO_ERROR;
}
// ------------------- SPECIAL FEATURES -----------------------------//
/**
* Get current language (no ISO code, but int value from the DB)
*/
public Integer getCurrentLanguageId() {
ec.log("Begin - getCurrentLanguage()");
int result;
String language = FacesContext.getCurrentInstance().getViewRoot().getLocale().getLanguage();
ec.log("getCurrentLanguage(), language:" + language);
result = LanguageServiceImpl.convertFromIso(language);
ec.log("End - getCurrentLanguage(), result:" + result);
return result;
}
/**
* Delete entity actually in "selectedData" from "dataList" object
*/
private void deleteSelectedFromDataList() {
ec.log("Begin - deleteSelectedFromDataList(), dataList size: " + dataList.size());
for (int i = 0; i < dataList.size(); i++) {
Product module = dataList.get(i);
if (module.equals(selectedData))
dataList.remove(i);
}
ec.log("End - deleteSelectedFromDataList(), dataList size: " + dataList.size());
}
/**
* Hide show multi-langue table
*/
public void showHideMessageList() {
String multi = UtilGui.getTranslationText("common_multiLanguage");
String mono = UtilGui.getTranslationText("common_monoLanguage");
lblShowHide = isMutliLanguageDisplay ? mono : multi;
jsfDescription.setRendered(!isMutliLanguageDisplay);
jsfMessageList.setRendered(isMutliLanguageDisplay);
// set opposite value
isMutliLanguageDisplay = (!isMutliLanguageDisplay);
}
public void setProductService(ProductService productService) {
this.productService = productService;
}
public HtmlInputText getTxtId() {
return txtId;
}
public void setTxtId(HtmlInputText txtId) {
this.txtId = txtId;
}
public boolean isDisabled() {
return disabled;
}
public void setDisabled(boolean disabled) {
this.disabled = disabled;
}
public List<SelectItem> getProducts()
{
List<SelectItem> result = new ArrayList<SelectItem>();
List<Product> products = this.productService.getProducts();
for (Product product : products) {
SelectItem item = new SelectItem(product.getId().getProductId(), product.getDescription());
result.add(item);
}
return result;
}
public void change(ValueChangeEvent event) {
this.marketId = (String) event.getNewValue();
}
} |
Partager