List of usage examples for java.beans PropertyDescriptor PropertyDescriptor
PropertyDescriptor(PropertyDescriptor x, PropertyDescriptor y)
From source file:org.yes.cart.bulkimport.csv.impl.CsvBulkImportServiceImpl.java
/** * Fill the given entity object with line information using import column descriptions. * * @param tuple given csv line//from ww w . j av a 2 s .c o m * @param object entity object * @param importColumns particular type column collection * @throws Exception in case if something wrong with reflection (IntrospectionException, * InvocationTargetException, * IllegalAccessException) */ private void fillEntityFields(final ImportTuple tuple, final Object object, final Collection<ImportColumn> importColumns) throws Exception { final Class clz = object.getClass(); PropertyDescriptor propertyDescriptor = null; for (ImportColumn importColumn : importColumns) { try { if (StringUtils.isNotBlank(importColumn.getName())) { //can be just lookup query Object writeObject = object; if (importColumn.getName().indexOf('.') == -1) { // direct property propertyDescriptor = new PropertyDescriptor(importColumn.getName(), clz); } else { // object path final String[] chain = importColumn.getName().split("\\."); for (int i = 0; i < chain.length - 1; i++) { propertyDescriptor = new PropertyDescriptor(chain[i], writeObject.getClass()); writeObject = propertyDescriptor.getReadMethod().invoke(writeObject); } propertyDescriptor = new PropertyDescriptor(chain[chain.length - 1], writeObject.getClass()); } Object singleObjectValue = tuple.getColumnValue(importColumn, valueDataAdapter); if (importColumn.getLanguage() != null) { final I18NModel model = new StringI18NModel( (String) propertyDescriptor.getReadMethod().invoke(object)); model.putValue(importColumn.getLanguage(), String.valueOf(singleObjectValue)); singleObjectValue = model.toString(); } if (singleObjectValue != null && !singleObjectValue.getClass().equals(propertyDescriptor.getPropertyType())) { // if we have mismatch try on the fly conversion - this happens if someone omits <data-type> for non String values singleObjectValue = extendedConversionService.convert(singleObjectValue, TypeDescriptor.valueOf(singleObjectValue.getClass()), TypeDescriptor.valueOf((propertyDescriptor.getPropertyType()))); } propertyDescriptor.getWriteMethod().invoke(writeObject, singleObjectValue); } } catch (Exception exp) { final String propName = propertyDescriptor != null ? propertyDescriptor.getName() : null; final String propType = propertyDescriptor != null ? propertyDescriptor.getPropertyType().getName() : null; throw new Exception( MessageFormat.format("Failed to process property name {0} type {1} object is {2}", propName, propType, object), exp); } } }
From source file:it.webappcommon.lib.jsf.AbstractPageBase.java
/** * Metodo che permette di ordinare, in ordine crescente o decrescente in * base ad ascending, un arraylist di oggetti di qualsiasi tipo in base al * parametro column. In particolare, con la reflection, viene gestito * l'ordinamento per column se essa e' int, long, double, float, boolean, * String, Date, Integer. Se non e' di uno di questi tipi, l'ordinamento non * fa niente in quanto il compareTo del comparator definito all'interno del * metodo ritorna sempre 0. La column, va indicata, come sempre del resto, * senza il get o l'is davanti.//from w w w .ja v a2 s.com * * @param list * ArrayList da ordinare * @param column * Nome della colonna/propriet dell'oggetto in base alla quale * effettuare l'ordinamento * @param ascending * Booleano che specifica se l'ordinamento deve essere ascendente * o meno */ public void sort(List list, final String column, final boolean ascending) { // TODO: Gestione di property a pi livelli: ad esempio // ElementBL.element.nome Comparator comparator = null; if ((column != null) && (!column.equals("")) && (list != null) && (list.size() > 0)) { final Class item_class = list.get(0).getClass(); comparator = new Comparator() { public int compare(Object o1, Object o2) { PropertyDescriptor column_descriptor = null; Method read_method = null; Object obj1 = null; Object obj2 = null; Object resInvoke1 = null; Object resInvoke2 = null; Date date1 = null; Date date2 = null; String str1 = null; String str2 = null; Integer Int1 = null; Integer Int2 = null; Double Double1 = null; Double Double2 = null; Float Float1 = null; Float Float2 = null; int returnValue = 0; try { if ((column == null) || (o1 == null) || (o2 == null)) { /* * In caso di non specificazione della colonna o di * uno dei due oggetti ritorna 0, facendo in modo * che l'ordinamento non ha alcun effetto */ returnValue = 0; } else { /* * Tenta di ottenere un property descriptor a * partire dalla colonna */ try { column_descriptor = new PropertyDescriptor(column, item_class); read_method = column_descriptor.getReadMethod(); } catch (Exception e) { read_method = null; } if (read_method != null) { obj1 = item_class.cast(o1); obj2 = item_class.cast(o2); /* * Viene gestito l'ordinamento per String, * Boolean, int */ if (read_method.getReturnType() == int.class) { // <editor-fold defaultstate="collapsed" // desc="Gestione tipo primitivo int"> /* * Variabili di tipo primitivo e' * impossibile che siano NULL */ str1 = read_method.invoke(obj1).toString(); str2 = read_method.invoke(obj2).toString(); int int1 = Integer.parseInt(str1); int int2 = Integer.parseInt(str2); if (ascending) { if (int1 < int2) { returnValue = -1; } else if (int1 > int2) { returnValue = 1; } else { returnValue = 0; } } else { if (int1 > int2) { returnValue = -1; } else if (int1 < int2) { returnValue = 1; } else { returnValue = 0; } } // </editor-fold> } else if (read_method.getReturnType() == long.class) { // <editor-fold defaultstate="collapsed" // desc="Gestione tipo primitivo long"> /* * Variabili di tipo primitivo e' * impossibile che siano NULL */ str1 = read_method.invoke(obj1).toString(); str2 = read_method.invoke(obj2).toString(); long lng1 = Long.parseLong(str1); long lng2 = Long.parseLong(str2); if (ascending) { if (lng1 < lng2) { returnValue = -1; } else if (lng1 > lng2) { returnValue = 1; } else { returnValue = 0; } } else { if (lng1 > lng2) { returnValue = -1; } else if (lng1 < lng2) { returnValue = 1; } else { returnValue = 0; } } // </editor-fold> } else if (read_method.getReturnType() == double.class) { // <editor-fold defaultstate="collapsed" // desc="Gestione tipo primitivo double"> /* * Variabili di tipo primitivo e' * impossibile che siano NULL */ str1 = read_method.invoke(obj1).toString(); str2 = read_method.invoke(obj2).toString(); double dbl1 = Double.parseDouble(str1); double dbl2 = Double.parseDouble(str2); if (ascending) { if (dbl1 < dbl2) { returnValue = -1; } else if (dbl1 > dbl2) { returnValue = 1; } else { returnValue = 0; } } else { if (dbl1 > dbl2) { returnValue = -1; } else if (dbl1 < dbl2) { returnValue = 1; } else { returnValue = 0; } } // </editor-fold> } else if (read_method.getReturnType() == float.class) { // <editor-fold defaultstate="collapsed" // desc="Gestione tipo primitivo float"> /* * Variabili di tipo primitivo e' * impossibile che siano NULL */ str1 = read_method.invoke(obj1).toString(); str2 = read_method.invoke(obj2).toString(); float flt1 = Float.parseFloat(str1); float flt2 = Float.parseFloat(str2); if (ascending) { if (flt1 < flt2) { returnValue = -1; } else if (flt1 > flt2) { returnValue = 1; } else { returnValue = 0; } } else { if (flt1 > flt2) { returnValue = -1; } else if (flt1 < flt2) { returnValue = 1; } else { returnValue = 0; } } // </editor-fold> } else if (read_method.getReturnType() == Float.class) { // <editor-fold defaultstate="collapsed" // desc="Gestione tipo object Float"> /* * Variabili di tipo object e' impossibile * che siano NULL */ /* * Pu essere che la propriet degli * oggetti sia NULL. In quel caso il * toString() genera errore se non gestito. * Se resInvoke1 oppure resInvoke2 sono * NULL, anche le rispettive conversioni in * interi lo devono essere */ resInvoke1 = read_method.invoke(obj1); resInvoke2 = read_method.invoke(obj2); if (resInvoke1 != null) { Float1 = (Float) resInvoke1; } if (resInvoke2 != null) { Float2 = (Float) resInvoke2; } if (ascending) { if ((Float1 != null) && (Float2 != null)) { returnValue = Float1.compareTo(Float2); } else if ((Float1 == null) && (Float2 != null)) { returnValue = -1; } else if ((Float1 != null) && (Float2 == null)) { returnValue = 1; } else if ((Float1 == null) && (Float2 == null)) { returnValue = 0; } } else { if ((Float1 != null) && (Float2 != null)) { returnValue = Float2.compareTo(Float1); } else if ((Float1 != null) && (Float2 == null)) { returnValue = -1; } else if ((Float1 == null) && (Float2 != null)) { returnValue = 1; } else if ((Float1 == null) && (Float2 == null)) { returnValue = 0; } } // </editor-fold> } else if (read_method.getReturnType() == boolean.class) { // <editor-fold defaultstate="collapsed" // desc="Gestione tipo primitivo boolean"> /* * Variabili di tipo primitivo e' * impossibile che siano NULL */ str1 = read_method.invoke(obj1).toString(); str2 = read_method.invoke(obj2).toString(); boolean bool1 = Boolean.parseBoolean(str1); boolean bool2 = Boolean.parseBoolean(str2); if (ascending) { if ((!bool1) && (bool2)) { returnValue = -1; } else if ((bool1) && (!bool2)) { returnValue = 1; } else { returnValue = 0; } } else { if ((bool1) && (!bool2)) { returnValue = -1; } else if ((!bool1) && (bool2)) { returnValue = 1; } else { returnValue = 0; } } // </editor-fold> } else if (read_method.getReturnType() == String.class) { // <editor-fold defaultstate="collapsed" // desc="Gestione tipo object String"> /* * Pu essere che la propriet degli * oggetti sia NULL. In quel caso il * toString() genera errore se non gestito. * Se resInvoke1 * * oppure resInvoke2 sono NULL, anche le * rispettive conversioni in stringa lo * devono essere */ resInvoke1 = read_method.invoke(obj1); resInvoke2 = read_method.invoke(obj2); if (resInvoke1 != null) { str1 = resInvoke1.toString().toUpperCase(); } if (resInvoke2 != null) { str2 = resInvoke2.toString().toUpperCase(); } if (ascending) { if ((str1 != null) && (str2 != null)) { returnValue = str1.compareTo(str2); } else if ((str1 == null) && (str2 != null)) { returnValue = -1; } else if ((str1 != null) && (str2 == null)) { returnValue = 1; } else if ((str1 == null) && (str2 == null)) { returnValue = 0; } } else { if ((str1 != null) && (str2 != null)) { returnValue = str2.compareTo(str1); } else if ((str1 != null) && (str2 == null)) { returnValue = -1; } else if ((str1 == null) && (str2 != null)) { returnValue = 1; } else if ((str1 == null) && (str2 == null)) { returnValue = 0; } } // </editor-fold> } else if (read_method.getReturnType() == Date.class) { // <editor-fold defaultstate="collapsed" // desc="Gestione tipo object Date"> /* * Pu essere che la propriet degli * oggetti sia NULL. In quel caso il * toString() genera errore se non gestito. * Se resInvoke1 oppure resInvoke2 sono * NULL, anche le rispettive conversioni in * date lo devono essere */ resInvoke1 = read_method.invoke(obj1); resInvoke2 = read_method.invoke(obj2); if (resInvoke1 != null) { date1 = (Date) resInvoke1; } if (resInvoke2 != null) { date2 = (Date) resInvoke2; } if (ascending) { if ((date1 != null) && (date2 != null)) { returnValue = date1.compareTo(date2); } else if ((date1 == null) && (date2 != null)) { returnValue = -1; } else if ((date1 != null) && (date2 == null)) { returnValue = 1; } else if ((date1 == null) && (date2 == null)) { returnValue = 0; } } else { if ((date1 != null) && (date2 != null)) { returnValue = date2.compareTo(date1); } else if ((date1 != null) && (date2 == null)) { returnValue = -1; } else if ((date1 == null) && (date2 != null)) { returnValue = 1; } else if ((date1 == null) && (date2 == null)) { returnValue = 0; } } // </editor-fold> } else if (read_method.getReturnType() == Integer.class) { // <editor-fold defaultstate="collapsed" // desc="Gestione tipo object Integer"> /* * Pu essere che la propriet degli * oggetti sia NULL. In quel caso il * toString() genera errore se non gestito. * Se resInvoke1 oppure resInvoke2 sono * NULL, anche le rispettive conversioni in * interi lo devono essere */ resInvoke1 = read_method.invoke(obj1); resInvoke2 = read_method.invoke(obj2); if (resInvoke1 != null) { Int1 = (Integer) resInvoke1; } if (resInvoke2 != null) { Int2 = (Integer) resInvoke2; } if (ascending) { if ((Int1 != null) && (Int2 != null)) { returnValue = Int1.compareTo(Int2); } else if ((Int1 == null) && (Int2 != null)) { returnValue = -1; } else if ((Int1 != null) && (Int2 == null)) { returnValue = 1; } else if ((Int1 == null) && (Int2 == null)) { returnValue = 0; } } else { if ((Int1 != null) && (Int2 != null)) { returnValue = Int2.compareTo(Int1); } else if ((Int1 != null) && (Int2 == null)) { returnValue = -1; } else if ((Int1 == null) && (Int2 != null)) { returnValue = 1; } else if ((Int1 == null) && (Int2 == null)) { returnValue = 0; } } // </editor-fold> } else if (read_method.getReturnType() == Double.class) { // <editor-fold defaultstate="collapsed" // desc="Gestione tipo object Double"> /* * Pu essere che la propriet degli * oggetti sia NULL. In quel caso il * toString() genera errore se non gestito. * Se resInvoke1 oppure resInvoke2 sono * NULL, anche le rispettive conversioni in * interi lo devono essere */ resInvoke1 = read_method.invoke(obj1); resInvoke2 = read_method.invoke(obj2); if (resInvoke1 != null) { Double1 = (Double) resInvoke1; } if (resInvoke2 != null) { Double2 = (Double) resInvoke2; } if (ascending) { if ((Double1 != null) && (Double2 != null)) { returnValue = Double1.compareTo(Double2); } else if ((Double1 == null) && (Double2 != null)) { returnValue = -1; } else if ((Double1 != null) && (Double2 == null)) { returnValue = 1; } else if ((Double1 == null) && (Double2 == null)) { returnValue = 0; } } else { if ((Double1 != null) && (Double2 != null)) { returnValue = Double2.compareTo(Double1); } else if ((Double1 != null) && (Double2 == null)) { returnValue = -1; } else if ((Double1 == null) && (Double2 != null)) { returnValue = 1; } else if ((Double1 == null) && (Double2 == null)) { returnValue = 0; } } // </editor-fold> } } else { /* * Nel caso in cui non ci sia il metodo get * della colonna passata, ritorna 0, facendo in * modo che l'ordinamento non ha alcun effetto */ returnValue = 0; } } } catch (Exception e) { /* * In caso d'errore in Comparator ritorna 0, facendo in * modo che l'ordinamento non ha alcun effetto */ returnValue = 0; } finally { /* Clean-up oggetti */ column_descriptor = null; read_method = null; obj1 = null; obj2 = null; resInvoke1 = null; resInvoke2 = null; date1 = null; date2 = null; str1 = null; str2 = null; Int1 = null; Int2 = null; Float1 = null; Float2 = null; } return returnValue; } }; if (comparator != null) { Collections.sort(list, comparator); } } comparator = null; }
From source file:org.yes.cart.bulkimport.csv.impl.CsvBulkImportServiceImpl.java
/** * Fill the given entity object with line information using import column descriptions. * * @param tuple given csv line * @param object entity object * @param importColumns particular type column collection * @param masterObject master object , that set from main import in case of sub import * @param importDescriptor import descriptor * @param entityCache runtime cache * * @throws Exception in case if something wrong with reflection (IntrospectionException, * InvocationTargetException, * IllegalAccessException) *//*from w w w . j av a 2s . c om*/ private void fillEntityForeignKeys(final ImportTuple tuple, final Object object, final Collection<ImportColumn> importColumns, final Object masterObject, final ImportDescriptor importDescriptor, final Map<String, Object> entityCache) throws Exception { ImportColumn currentColumn = null; final Class clz = object.getClass(); Object singleObjectValue = null; PropertyDescriptor propertyDescriptor = null; try { for (ImportColumn importColumn : importColumns) { currentColumn = importColumn; if (importColumn.isUseMasterObject()) { singleObjectValue = masterObject; } else { singleObjectValue = getEntity(tuple, importColumn, masterObject, importDescriptor, entityCache); } propertyDescriptor = new PropertyDescriptor(importColumn.getName(), clz); final Object oldValue = propertyDescriptor.getReadMethod().invoke(object); if (oldValue instanceof Identifiable || singleObjectValue instanceof Identifiable) { final Object oldValuePK = oldValue != null ? genericDAO.getEntityIdentifier(oldValue) : null; final Object newValuePK = singleObjectValue != null ? genericDAO.getEntityIdentifier(singleObjectValue) : null; if (oldValuePK == null || !oldValuePK.equals(newValuePK)) { // Update the object only if the value has changed propertyDescriptor.getWriteMethod().invoke(object, singleObjectValue); } } else { // This is not identifiable, possibly primitive (PK) so write always propertyDescriptor.getWriteMethod().invoke(object, singleObjectValue); } } } catch (Exception exp) { final String propName = propertyDescriptor != null ? propertyDescriptor.getName() : null; final String propType = propertyDescriptor != null ? propertyDescriptor.getPropertyType().getName() : null; throw new Exception(MessageFormat.format( "Failed to process property name {0} type {1} object is {2} caused by column {0} with value {1}", propName, propType, object, currentColumn, singleObjectValue), exp); } }
From source file:org.springframework.data.document.mongodb.MongoTemplate.java
/** * Substitutes the id key if it is found in he query. Any 'id' keys will be replaced with '_id' and the value converted * to an ObjectId if possible. This conversion should match the way that the id fields are converted during read * operations./*from w ww . j a v a 2s. c o m*/ * * @param query * @param targetClass * @param reader */ protected void substituteMappedIdIfNecessary(DBObject query, Class<?> targetClass, MongoReader<?> reader) { MongoConverter converter = null; if (reader instanceof SimpleMongoConverter) { converter = (MongoConverter) reader; } else if (reader instanceof MappingMongoConverter) { converter = (MappingMongoConverter) reader; } else { return; } String idKey = null; if (query.containsField("id")) { idKey = "id"; } if (query.containsField("_id")) { idKey = "_id"; } if (idKey == null) { // no ids in this query return; } MongoPropertyDescriptor descriptor; try { MongoPropertyDescriptor mpd = new MongoPropertyDescriptor(new PropertyDescriptor(idKey, targetClass), targetClass); descriptor = mpd; } catch (IntrospectionException e) { // no property descriptor for this key - try the other try { String theOtherIdKey = "id".equals(idKey) ? "_id" : "id"; MongoPropertyDescriptor mpd2 = new MongoPropertyDescriptor( new PropertyDescriptor(theOtherIdKey, targetClass), targetClass); descriptor = mpd2; } catch (IntrospectionException e2) { // no property descriptor for this key either - bail return; } } if (descriptor.isIdProperty() && descriptor.isOfIdType()) { Object value = query.get(idKey); if (value instanceof DBObject) { DBObject dbo = (DBObject) value; if (dbo.containsField("$in")) { List<Object> ids = new ArrayList<Object>(); int count = 0; for (Object o : (Object[]) dbo.get("$in")) { count++; ObjectId newValue = convertIdValue(converter, o); if (newValue != null) { ids.add(newValue); } } if (ids.size() > 0 && ids.size() != count) { throw new InvalidDataAccessApiUsageException("Inconsistent set of id values provided " + Arrays.asList((Object[]) dbo.get("$in"))); } if (ids.size() > 0) { dbo.removeField("$in"); dbo.put("$in", ids.toArray()); } } query.removeField(idKey); query.put(MongoPropertyDescriptor.ID_KEY, value); } else { ObjectId newValue = convertIdValue(converter, value); query.removeField(idKey); if (newValue != null) { query.put(MongoPropertyDescriptor.ID_KEY, newValue); } else { query.put(MongoPropertyDescriptor.ID_KEY, value); } } } }