@Override public <T> T provide( Class<T> objectType, AnnotationProvider annotationProvider, ObjectLocator locator) { Value annotation = annotationProvider.getAnnotation(Value.class); if (annotation == null) return null; String value = annotation.value(); Object expanded = symbolSource.expandSymbols(value); IntermediateType intermediate = annotationProvider.getAnnotation(IntermediateType.class); if (intermediate != null) expanded = typeCoercer.coerce(expanded, intermediate.value()); return typeCoercer.coerce(expanded, objectType); }
@Override public Product parseClient(Field field, String clientValue, String message) throws ValidationException { if (clientValue == null) { return null; } Long id = coercer.coerce(clientValue, Long.class); return repository.find(id); }
private Asset getLocaleAsset(Locale locale, String jQueryUIPath) { final String prefix = String.format("%s/i18n/jquery.ui.datepicker-%s", jQueryUIPath, locale.getLanguage()); final Resource withCountryExtension = typeCoercer.coerce(String.format("%s-%s.js", prefix, locale.getCountry()), Resource.class); if (withCountryExtension.exists()) { return assetSource.getClasspathAsset(withCountryExtension.getPath()); } final Resource withLanguageExtension = typeCoercer.coerce(String.format("%s.js", prefix), Resource.class); if (withLanguageExtension.exists()) { return assetSource.getClasspathAsset(withLanguageExtension.getPath()); } return null; }
public List<Object> convertAsListObject(Object value) { Iterable iterable = coercer.coerce(value, Iterable.class); Iterator it = iterable.iterator(); List<Object> list = new ArrayList<Object>(); while (it.hasNext()) { Object obj = it.next(); // is persistence object if (entityService.getEntityClass(obj).isAnnotationPresent(Entity.class)) { list.add(propertyAccess.get(obj, EntityConstants.ID_PROPERTY_NAME)); } else { list.add(obj); } } return list; }
/** * 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; }