private Asset createAssetFromResource(Resource resource) {
    // The class of the resource is derived from the class of the base resource.
    // So we can then use the class of the resource as a key to locate the correct asset
    // factory.

    try {
      upgradeReadLockToWriteLock();

      // Check for competing thread beat us to it (not very likely!):

      Asset result = TapestryInternalUtils.getAndDeref(cache, resource);

      if (result != null) {
        return result;
      }

      Class resourceClass = resource.getClass();

      AssetFactory factory = registry.get(resourceClass);

      return factory.createAsset(resource);
    } finally {
      downgradeWriteLockToReadLock();
    }
  }
  private Asset getAssetForResource(Resource resource) {
    try {
      acquireReadLock();

      Asset result = TapestryInternalUtils.getAndDeref(cache, resource);

      if (result == null) {
        result = createAssetFromResource(resource);
        cache.put(resource, new SoftReference(result));
      }

      return result;
    } finally {
      releaseReadLock();
    }
  }
Example #3
0
    public ColumnSort getColumnSort(String columnId) {
      if (!TapestryInternalUtils.isEqual(columnId, sortColumnId)) return ColumnSort.UNSORTED;

      return getColumnSort();
    }
Example #4
0
 private boolean isSelected(String clientValue) {
   return TapestryInternalUtils.isEqual(clientValue, selectedClientValue);
 }
Example #5
0
  /**
   * Ajax event handler, form client side to get the data to display to parse it according to the
   * server-side format. see http://www.trirand.com/jqgridwiki/doku.php?id=wiki:retrieving_data for
   * more details
   */
  @OnEvent(value = "Data")
  JSONObject onData() {
    String page = request.getParameter(PAGE);

    int requestedPageNumber = Integer.parseInt(page);
    String search = request.getParameter(SEARCH);
    String searchField = request.getParameter(SEARCH_FIELD);
    String searchString = request.getParameter(SEARCH_STRING);
    String searchOper = request.getParameter(SEARCH_OPER);
    // searchField=tax&searchString=100&searchOper=gt

    if (search.equals("false")) source.resetFilter();
    else if (searchField != null && searchOper != null && searchString != null) {
      SearchOperator op = SearchOperator.valueOf(searchOper);
      Class searchType = getDataModel().get(searchField).getConduit().getPropertyType();
      Object searchValue = typeCoercer.coerce(searchString, searchType);
      SearchConstraint searchFor =
          new SearchConstraint(
              searchField, op, searchValue, getDataModel().get(searchField).getConduit());
      List<SearchConstraint> lst = new ArrayList();
      lst.add(searchFor);
      source.setFilter(lst);
    }

    String nd = request.getParameter(ND);

    String rowsSelected = request.getParameter(ROWS);
    int rowsPerPage = Integer.parseInt(rowsSelected);

    String sidx = request.getParameter(SIDX);
    String arrayString[] = sidx.split("\\s+");
    // get only the start
    sidx = arrayString[0];

    String sord = request.getParameter(SORD);

    JSONObject response = new JSONObject();
    int records = source.getAvailableRows();

    int nbPages = records / rowsPerPage;
    int modulo = records % rowsPerPage;
    if (modulo > 0) nbPages++;

    int startIndex = 0 + (requestedPageNumber - 1) * rowsPerPage;
    int endIndex = startIndex + rowsPerPage - 1;
    if (endIndex > records - 1) endIndex = records - 1;

    response.put("page", requestedPageNumber);
    response.put("total", nbPages);

    response.put("records", records);

    List<SortConstraint> sortConstraints = new ArrayList();
    if (!sidx.isEmpty()) {
      GridSortModel sortModel = getSortModel();
      ColumnSort colSort = sortModel.getColumnSort(sidx);
      if (sord.equals("asc")) setSortAscending(true);
      else setSortAscending(false);
      sortModel.updateSort(sidx);
      sortConstraints = sortModel.getSortConstraints();
    }
    source.prepare(startIndex, endIndex, sortConstraints);

    JSONArray rows = new JSONArray();

    for (int index = startIndex; index <= endIndex; index++) {
      JSONObject row = new JSONObject();
      row.put("id", index);
      JSONArray cell = new JSONArray();
      // Class c = dataSource.getRowType();
      Object obj = source.getRowValue(index);
      List<String> names = getDataModel().getPropertyNames();
      for (String name : names) {

        PropertyConduit conduit = getDataModel().get(name).getConduit();
        Class type = conduit.getPropertyType();
        // Block displayBlock =
        // defaultBeanBlockSource.getDisplayBlock(getDataModel().get(name).getDataType());

        try {
          String cellValue;
          Object val = conduit.get(obj);
          // todo use BeanBlockSource or ...
          if (type.equals(Date.class)) {
            // mimic PropertyDisplayBlock
            Date cellDate = (Date) val;
            DateFormat dateFormat = DateFormat.getDateInstance(DateFormat.MEDIUM, locale);
            cellValue = dateFormat.format(cellDate);
          } else if (type.equals(Enum.class)) {
            cellValue =
                TapestryInternalUtils.getLabelForEnum(overrides.getOverrideMessages(), (Enum) val);
          } else {
            if (val == null) cellValue = "undefined " + name;
            else cellValue = typeCoercer.coerce(val, String.class);
            // ValueEncoder valueEncoder =encoderSource.getValueEncoder(type);
            // cellValue = valueEncoder.toClient(val);
          }
          cell.put(cellValue);
        } catch (NullPointerException ex) {
          cell.put("undefined " + name);
        }
      }
      row.put("cell", cell);
      rows.put(row);
    }
    response.put("rows", rows);

    // {"page":"1","total":2,"records":"13",
    // "rows":[{"id":"13","cell":["13","2007-10-06","Client 3","1000.00","0.00","1000.00",null]},
    //		   {"id":"12","cell":["12","2007-10-06","Client 2","700.00","140.00","840.00",null]},
    //        {"id":"11","cell":["11","2007-10-06","Client 1","600.00","120.00","720.00",null]},
    //        {"id":"10","cell":["10","2007-10-06","Client 2","100.00","20.00","120.00",null]},
    //        {"id":"9","cell":["9","2007-10-06","Client 1","200.00","40.00","240.00",null]},
    //        {"id":"8","cell":["8","2007-10-06","Client 3","200.00","0.00","200.00",null]},
    //        {"id":"7","cell":["7","2007-10-05","Client 2","120.00","12.00","134.00",null]},
    //        {"id":"6","cell":["6","2007-10-05","Client 1","50.00","10.00","60.00",""]},
    //        {"id":"5","cell":["5","2007-10-05","Client 3","100.00","0.00","100.00","no tax at
    // all"]},
    ///       {"id":"4","cell":["4","2007-10-04","Client 3","150.00","0.00","150.00","no tax"]}],

    JSONObject userdata = new JSONObject();
    response.put("userdata", userdata);
    // "userdata":{"amount":3220,"tax":342,"total":3564,"name":"Totals:"}}
    return response;
  }