Example usage for org.springframework.beans BeanWrapper getPropertyValue

List of usage examples for org.springframework.beans BeanWrapper getPropertyValue

Introduction

In this page you can find the example usage for org.springframework.beans BeanWrapper getPropertyValue.

Prototype

@Nullable
Object getPropertyValue(String propertyName) throws BeansException;

Source Link

Document

Get the current value of the specified property.

Usage

From source file:de.tudarmstadt.ukp.clarin.webanno.api.dao.RepositoryServiceDbData.java

@Override
public <T> void saveHelpContents(T aConfigurationObject) throws IOException {
    BeanWrapper wrapper = PropertyAccessorFactory.forBeanPropertyAccess(aConfigurationObject);
    Properties property = new Properties();
    for (PropertyDescriptor value : wrapper.getPropertyDescriptors()) {
        if (wrapper.getPropertyValue(value.getName()) == null) {
            continue;
        }/*from  w w  w  .java 2s .  c o m*/
        property.setProperty(value.getName(), wrapper.getPropertyValue(value.getName()).toString());
    }
    File helpFile = new File(dir.getAbsolutePath() + HELP_FILE);
    if (helpFile.exists()) {
        FileUtils.forceDeleteOnExit(helpFile);
    } else {
        helpFile.createNewFile();
    }
    property.store(new FileOutputStream(helpFile), null);

}

From source file:edu.cmu.cs.lti.discoursedb.api.browsing.controller.BrowsingRestController.java

@RequestMapping(value = "/action/downloadQueryCsv/discoursedb_data.csv", method = RequestMethod.GET)
@ResponseBody//from  w  ww  . j  av a  2  s.com
String downloadQueryCsv(HttpServletResponse response, @RequestParam("query") String query,
        HttpServletRequest hsr, HttpSession session) throws IOException {

    securityUtils.authenticate(hsr, session);
    response.setContentType("application/csv; charset=utf-8");
    response.setHeader("Content-Disposition", "attachment");

    try {
        logger.info("Got query for csv: " + query);

        DdbQuery q = new DdbQuery(selector, discoursePartService, query);

        Page<BrowsingContributionResource> lbcr = q.retrieveAllContributions()
                .map(c -> new BrowsingContributionResource(c, annoService));

        StringBuilder output = new StringBuilder();
        ArrayList<String> headers = new ArrayList<String>();
        for (PropertyDescriptor pd : BeanUtils.getPropertyDescriptors(BrowsingContributionResource.class)) {
            String name = pd.getName();
            if (!name.equals("class") && !name.equals("id") && !name.equals("links")) {
                headers.add(name);
            }
        }
        output.append(String.join(",", headers));
        output.append("\n");

        for (BrowsingContributionResource bcr : lbcr.getContent()) {
            String comma = "";
            BeanWrapper wrap = PropertyAccessorFactory.forBeanPropertyAccess(bcr);
            for (String hdr : headers) {

                String item = "";
                try {
                    item = wrap.getPropertyValue(hdr).toString();
                    item = item.replaceAll("\"", "\"\"");
                    item = item.replaceAll("^\\[(.*)\\]$", "$1");
                } catch (Exception e) {
                    logger.info(e.toString() + " For header " + hdr + " item " + item);
                    item = "";
                }
                if (hdr.equals("annotations") && item.length() > 0) {
                    logger.info("Annotation is " + item);
                }
                output.append(comma + "\"" + item + "\"");
                comma = ",";
            }
            output.append("\n");
        }
        return output.toString();
    } catch (Exception e) {
        return "ERROR:" + e.getMessage();
    }
}

From source file:edu.duke.cabig.c3pr.web.ajax.CommonAjaxFacade.java

@SuppressWarnings("unchecked")
private <T> T buildReduced(T src, List<String> properties) {
    T dst = null;//  w w  w .ja  v  a 2s.  c  o  m
    try {
        dst = (T) src.getClass().newInstance();
    } catch (InstantiationException e) {
        throw new RuntimeException("Failed to instantiate " + src.getClass().getName(), e);
    } catch (IllegalAccessException e) {
        throw new RuntimeException("Failed to instantiate " + src.getClass().getName(), e);
    }

    BeanWrapper source = new BeanWrapperImpl(src);
    BeanWrapper destination = new BeanWrapperImpl(dst);
    for (String property : properties) {
        // only for nested props
        String[] individualProps = property.split("\\.");
        String temp = "";
        for (int i = 0; i < individualProps.length - 1; i++) {
            temp += (i != 0 ? "." : "") + individualProps[i];
            Object o = source.getPropertyValue(temp);
            if (destination.getPropertyValue(temp) == null) {
                try {
                    destination.setPropertyValue(temp, o.getClass().newInstance());
                } catch (BeansException e) {
                    log.error(e.getMessage());
                } catch (InstantiationException e) {
                    log.error(e.getMessage());
                } catch (IllegalAccessException e) {
                    log.error(e.getMessage());
                }
            }
        }
        destination.setPropertyValue(property, source.getPropertyValue(property));
    }
    return dst;
}

From source file:edu.duke.cabig.c3pr.web.ajax.InvestigatorAjaxFacade.java

@SuppressWarnings("unchecked")
private <T> T buildReduced(T src, List<String> properties) {
    T dst = null;/*ww w.jav a 2 s .c  o  m*/
    try {
        // it doesn't seem like this cast should be necessary
        dst = (T) src.getClass().newInstance();
    } catch (InstantiationException e) {
        throw new RuntimeException("Failed to instantiate " + src.getClass().getName(), e);
    } catch (IllegalAccessException e) {
        throw new RuntimeException("Failed to instantiate " + src.getClass().getName(), e);
    }

    BeanWrapper source = new BeanWrapperImpl(src);
    BeanWrapper destination = new BeanWrapperImpl(dst);
    for (String property : properties) {
        destination.setPropertyValue(property, source.getPropertyValue(property));
    }
    return dst;
}

From source file:edu.duke.cabig.c3pr.web.ajax.ResearchStaffAjaxFacade.java

/**
 * Builds the reduced version of the passed in objects in order to pas a light weight version to the UI.
 * Used for auto-completers in general.//from  w  w w.j  ava2  s .  c om
 *
 * @param <T> the generic type
 * @param src the src
 * @param properties the properties
 * @return the t
 */
@SuppressWarnings("unchecked")
private <T> T buildReduced(T src, List<String> properties) {
    T dst = null;
    try {
        // it doesn't seem like this cast should be necessary
        dst = (T) src.getClass().newInstance();
    } catch (InstantiationException e) {
        throw new RuntimeException("Failed to instantiate " + src.getClass().getName(), e);
    } catch (IllegalAccessException e) {
        throw new RuntimeException("Failed to instantiate " + src.getClass().getName(), e);
    }

    BeanWrapper source = new BeanWrapperImpl(src);
    BeanWrapper destination = new BeanWrapperImpl(dst);
    for (String property : properties) {
        destination.setPropertyValue(property, source.getPropertyValue(property));
    }
    return dst;
}

From source file:edu.duke.cabig.c3pr.web.ajax.StudyAjaxFacade.java

@SuppressWarnings("unchecked")
private <T> T buildReduced(T src, List<String> properties) {
    T dst = null;//from   ww  w  . j a  v a  2  s.  c  o m
    try {
        // it doesn't seem like this cast should be necessary
        dst = (T) src.getClass().newInstance();
    } catch (InstantiationException e) {
        throw new RuntimeException("Failed to instantiate " + src.getClass().getName(), e);
    } catch (IllegalAccessException e) {
        throw new RuntimeException("Failed to instantiate " + src.getClass().getName(), e);
    }

    BeanWrapper source = new BeanWrapperImpl(src);
    BeanWrapper destination = new BeanWrapperImpl(dst);
    for (String property : properties) {
        // only for nested props
        String[] individualProps = property.split("\\.");
        String temp = "";

        for (int i = 0; i < individualProps.length - 1; i++) {
            temp += (i != 0 ? "." : "") + individualProps[i];
            Object o = source.getPropertyValue(temp);
            if (destination.getPropertyValue(temp) == null) {
                try {
                    destination.setPropertyValue(temp, o.getClass().newInstance());
                } catch (BeansException e) {
                    log.error(e.getMessage());
                } catch (InstantiationException e) {
                    log.error(e.getMessage());
                } catch (IllegalAccessException e) {
                    log.error(e.getMessage());
                }
            }
        }
        // for single and nested props
        destination.setPropertyValue(property, source.getPropertyValue(property));
    }
    return dst;
}

From source file:edu.duke.cabig.c3pr.web.ajax.UserAjaxFacade.java

@SuppressWarnings("unchecked")
private <T> T buildReduced(T src, List<String> properties) {
    T dst = null;//from   w w w  .ja  va2 s . c  o m
    try {
        // it doesn't seem like this cast should be necessary
        dst = (T) src.getClass().newInstance();
    } catch (InstantiationException e) {
        throw new RuntimeException("Failed to instantiate " + src.getClass().getName(), e);
    } catch (IllegalAccessException e) {
        throw new RuntimeException("Failed to instantiate " + src.getClass().getName(), e);
    }

    BeanWrapper source = new BeanWrapperImpl(src);
    BeanWrapper destination = new BeanWrapperImpl(dst);
    for (String property : properties) {
        // only for nested props
        String[] individualProps = property.split("\\.");
        String temp = "";
        String myTemp = "";
        try {
            for (int i = 0; i < individualProps.length - 1; i++) {
                temp += (i != 0 ? "." : "") + individualProps[i];
                Object o = source.getPropertyValue(temp);
                if (temp.charAt(temp.length() - 1) == ']') {
                    myTemp = temp.substring(0, temp.length() - 3);
                    if (destination.getPropertyValue(myTemp) == null) {
                        destination.setPropertyValue(myTemp,
                                ArrayList.class.newInstance().add(o.getClass().newInstance()));
                    }
                    if (((ArrayList) destination.getPropertyValue(myTemp)).size() == 0) {
                        ((ArrayList) destination.getPropertyValue(myTemp)).add(o.getClass().newInstance());
                    }
                } else {
                    if (destination.getPropertyValue(temp) == null) {
                        destination.setPropertyValue(temp, o.getClass().newInstance());
                    }
                }
            }
        } catch (BeansException e) {
            log.error(e.getMessage());
        } catch (InstantiationException e) {
            log.error(e.getMessage());
        } catch (IllegalAccessException e) {
            log.error(e.getMessage());
        }
        // for single and nested props
        destination.setPropertyValue(property, source.getPropertyValue(property));
    }
    /*for (String property : properties) {
    destination.setPropertyValue(property, source.getPropertyValue(property));
    }*/
    return dst;
}

From source file:edu.northwestern.bioinformatics.studycalendar.tools.BeanPropertyListComparator.java

public int compare(T o1, T o2) {
    BeanWrapper first = new BeanWrapperImpl(o1);
    BeanWrapper second = new BeanWrapperImpl(o2);

    for (Map.Entry<String, Comparator> entry : comparators.entrySet()) {
        Object value1 = first.getPropertyValue(entry.getKey());
        Object value2 = second.getPropertyValue(entry.getKey());

        int result = entry.getValue().compare(value1, value2);
        if (result != 0)
            return result;
    }//from   w  w  w.  j  a  va 2  s . c om

    return 0;
}

From source file:edu.northwestern.bioinformatics.studycalendar.web.admin.AdministerUserCommand.java

private void copyBoundProperties(User src, User dst) {
    BeanWrapper srcW = new BeanWrapperImpl(src);
    BeanWrapper dstW = new BeanWrapperImpl(dst);

    for (PropertyDescriptor srcProp : srcW.getPropertyDescriptors()) {
        if (srcProp.getReadMethod() == null || srcProp.getWriteMethod() == null) {
            continue;
        }/*from  w  ww.  j  a v  a2  s .  com*/
        Object srcValue = srcW.getPropertyValue(srcProp.getName());
        if (srcValue != null) {
            dstW.setPropertyValue(srcProp.getName(), srcValue);
        }
    }
}

From source file:es.pode.soporte.auditoria.registrar.BeanDescripcion.java

/** 
 * Mtodo para recuperar informacin de un objeto a travs de reflexin 
 *   /*from  www.  j  a  v a  2s  .c  o m*/
 * @param objeto Objeto al cual se le realiza la reflexin 
 * @param atributo Valor que se recupera del objeto
 * @return valor Se devuelve el valor del atributo buscado
 */
public static String describe(Object objeto, String atributo) {

    if (objeto == null)
        return null;

    Object valor = null;

    Class clase = objeto.getClass();
    if (clase.isArray() || java.util.Collection.class.isAssignableFrom(clase))
        log.warn("El atributo es un array y debera ser un String");
    else {
        log("Reflexin del objeto: " + objeto);

        BeanWrapper wrapper = new BeanWrapperImpl(objeto);
        PropertyDescriptor descriptors[] = wrapper.getPropertyDescriptors();

        for (int i = 0; i < descriptors.length; i++) {
            PropertyDescriptor pd = descriptors[i];

            if (pd.getReadMethod() != null && pd.getWriteMethod() != null) {
                String name = pd.getName();

                /* Capturamos el valor del atributo que nos interesa */
                if (name.equals(atributo)) {
                    log("Nombre atributo: " + name);
                    valor = wrapper.getPropertyValue(name);

                    /* Si el valor es nulo registramos un "" */
                    if (valor == null) {
                        log("El valor del atributo interceptado es nulo");
                        return null;
                    } else
                        return valor.toString();
                }
            }
        }
    }

    return null;
}