Example #1
0
 /**
  * The field tag is a helper, based on the spirit of Don't Repeat Yourself.
  *
  * @param args tag attributes
  * @param body tag inner body
  * @param out the output writer
  * @param template enclosing template
  * @param fromLine template line number where the tag is defined
  */
 public static void _field(
     Map<?, ?> args, Closure body, PrintWriter out, ExecutableTemplate template, int fromLine) {
   Map<String, Object> field = new HashMap<String, Object>();
   String _arg = args.get("arg").toString();
   field.put("name", _arg);
   field.put("id", _arg.replace('.', '_'));
   field.put("flash", Flash.current().get(_arg));
   field.put(
       "flashArray",
       field.get("flash") != null && !StringUtils.isEmpty(field.get("flash").toString())
           ? field.get("flash").toString().split(",")
           : new String[0]);
   field.put("error", Validation.error(_arg));
   field.put("errorClass", field.get("error") != null ? "hasError" : "");
   String[] pieces = _arg.split("\\.");
   Object obj = body.getProperty(pieces[0]);
   if (obj != null) {
     if (pieces.length > 1) {
       try {
         String path = _arg.substring(_arg.indexOf(".") + 1);
         Object value = PropertyUtils.getProperty(obj, path);
         field.put("value", value);
       } catch (Exception e) {
         // if there is a problem reading the field we dont set any value
       }
     } else {
       field.put("value", obj);
     }
   }
   body.setProperty("field", field);
   body.call();
 }
 private static void processUpdate(String type, String id, String att, String value) {
   if (type == null || type.trim().length() == 0) return;
   if ("Order".equals(type)) {
     PurchaseOrder order = PurchaseOrder.findById(new Long(id));
     if ("paid".equals(att)) {
       Boolean oldValue = order.paid;
       if (oldValue == null) oldValue = Boolean.FALSE;
       order.paid = !oldValue;
       if (oldValue) setCurrentUseCaseName("Order unpaid");
       else setCurrentUseCaseName("Order paid");
     } else if ("customer".equals(att)) {
       if (value == null
           || value.startsWith(
               "- ")) // Do nothing if somehow the "- select a customer -" item was selected
       return;
       Customer customer = Customer.findById(new Long(value));
       order.customer = customer;
       Flash.current().success("The order has been reassigned to customer " + customer.name);
       setCurrentUseCaseName("Order reassigned");
     } else if ("notes".equals(att)) {
       order.notes = value;
       setCurrentUseCaseName("Order notes updated");
     }
     order.save();
   } else if ("Customer".equals(type)) {
     Customer customer = Customer.findById(new Long(id));
     if ("creditLimit".equals(att)) {
       BigDecimal val = NumberFormat.parseMoney(value);
       customer.creditLimit = val;
       setCurrentUseCaseName("Customer credit limit updated");
     }
     customer.save();
   } else if ("LineItem".equals(type)) {
     LineItem lineitem = LineItem.findById(new Long(id));
     if ("quantity".equals(att)) {
       Integer val = NumberFormat.parseNumber(value);
       lineitem.qtyOrdered = val;
       setCurrentUseCaseName("Line Item quantity updated");
     } else if ("unitPrice".equals(att)) {
       BigDecimal val = NumberFormat.parseMoney(value);
       lineitem.productPrice = val;
       setCurrentUseCaseName("Line Item unit price updated");
     } else if ("product".equals(att)) {
       Product product = Product.findById(new Long(value));
       lineitem.product = product;
       setCurrentUseCaseName("Line Item product changed");
     }
     lineitem.save();
   }
 }