/**
   * Adds the field values from the service item container to the map.
   *
   * @param service the service.
   * @param container the service item container.
   * @param values the map holding the values.
   * @param locked all locked values for the service.
   */
  private static void addFieldValues(
      CatalogServiceRestRep service,
      Collection<? extends ServiceItemRestRep> items,
      Map<String, String> values,
      Map<String, String> locked) {
    for (ServiceItemRestRep item : items) {
      if (item instanceof ServiceFieldTableRestRep) {
        addColumnValues(service, (ServiceFieldTableRestRep) item, values, locked);
      } else if (item instanceof ServiceFieldGroupRestRep) {
        addFieldValues(service, ((ServiceFieldGroupRestRep) item).getItems(), values, locked);
      } else if (item instanceof ServiceFieldRestRep) {
        ServiceFieldRestRep field = (ServiceFieldRestRep) item;
        String value = getFieldValue(field);
        if (locked.containsKey(field.getName())) {
          value = locked.get(field.getName());
        }
        if (value != null) {
          values.put(field.getName(), value);
        }

        List<String> fieldValues = TextUtils.parseCSV(value);
        if (fieldValues.isEmpty() && field.isRequired()) {
          Validation.required(field.getName(), null);
        }
        for (String fieldValue : fieldValues) {
          ServiceFieldValidator.validateField(service, field, fieldValue);
        }
      }
    }
  }
 private static String getFieldValue(ServiceFieldRestRep field) {
   if (params._contains(field.getName())) {
     String[] values = params.getAll(field.getName());
     if (values != null) {
       return TextUtils.formatCSV(values);
     }
   }
   return null;
 }
  /**
   * Adds all column values for the given table.
   *
   * @param service the catalog service.
   * @param table the table of fields.
   * @param values the map holding the values.
   * @param locked all locked values for the service.
   */
  private static void addColumnValues(
      CatalogServiceRestRep service,
      ServiceFieldTableRestRep table,
      Map<String, String> values,
      Map<String, String> locked) {

    List<ServiceFieldRestRep> fields = ServiceDescriptorUtils.getAllFieldList(table.getItems());

    int rowCount = 0;
    for (ServiceFieldRestRep field : fields) {
      if (!locked.containsKey(field.getName())) {
        String[] columns = getColumnValue(table, field);
        rowCount = Math.max(rowCount, columns.length);
      }
    }

    for (ServiceFieldRestRep field : fields) {
      String[] columns = new String[rowCount];
      if (locked.containsKey(field.getName())) {
        String lockedValue = locked.get(field.getName());
        for (int i = 0; i < columns.length; i++) {
          columns[i] = lockedValue;
        }
      } else {
        String[] col = getColumnValue(table, field);
        System.arraycopy(col, 0, columns, 0, col.length);
      }

      for (int i = 0; i < columns.length; i++) {
        String prefix = table.getName() + "[" + i + "]";
        ServiceFieldValidator.validateField(service, prefix, field, columns[i]);
      }

      values.put(field.getName(), TextUtils.formatCSV(columns));
    }
  }
 private static Parameter createOrderParameter(ServiceFieldRestRep field, String value) {
   Parameter parameter = new Parameter();
   parameter.setLabel(field.getName());
   if (value != null) {
     parameter.setValue(value.trim());
   } else {
     parameter.setValue(value); // NOSONAR
     // ("Suppressing Sonar violation of Load of known null value. Value can be null and it needs
     // to be set when null")
   }
   parameter.setUserInput(true);
   if (StringUtils.equals(field.getType(), ServiceField.TYPE_PASSWORD)) {
     parameter.setEncrypted(true);
   }
   return parameter;
 }
 /**
  * Gets the submitted value for the column field from the HTTP params. The parameters are named:
  * <tt>&lt;<i>table.name</i>&gt;[<i>i</i>].&lt;<i>field.name</i>&gt;</tt>
  *
  * @param table the table containing the field.
  * @param field the field.
  * @return the values for the column.
  */
 private static String[] getColumnValue(
     ServiceFieldTableRestRep table, ServiceFieldRestRep field) {
   List<String> values = Lists.newArrayList();
   Pattern pattern = Pattern.compile(table.getName() + "\\[(\\d+)\\]." + field.getName());
   for (String name : params.data.keySet()) {
     Matcher match = pattern.matcher(name);
     if (match.matches()) {
       int index = Integer.valueOf(match.group(1));
       for (int i = values.size(); i <= index; i++) {
         values.add(null);
       }
       values.set(index, params.get(name));
     }
   }
   return values.toArray(new String[values.size()]);
 }
  @Util
  public static OrderCreateParam createOrder(
      CatalogServiceRestRep service,
      ServiceDescriptorRestRep descriptor,
      Map<String, String> parameters) {

    OrderCreateParam order = new OrderCreateParam();
    order.setTenantId(uri(Models.currentAdminTenant()));
    order.setCatalogService(service.getId());

    List<Parameter> orderParameters = Lists.newArrayList();
    List<ServiceFieldRestRep> fields =
        ServiceDescriptorUtils.getAllFieldList(descriptor.getItems());
    for (ServiceFieldRestRep field : fields) {
      String value = parameters.get(field.getName());
      if (StringUtils.isNotBlank(value)) {
        orderParameters.add(createOrderParameter(field, value));
      }
    }
    order.setParameters(orderParameters);
    return order;
  }