Example usage for org.apache.commons.beanutils BeanPredicate BeanPredicate

List of usage examples for org.apache.commons.beanutils BeanPredicate BeanPredicate

Introduction

In this page you can find the example usage for org.apache.commons.beanutils BeanPredicate BeanPredicate.

Prototype

public BeanPredicate(String propertyName, Predicate predicate) 

Source Link

Document

Constructs a BeanPredicate that applies the given Predicate to the named property value.

Usage

From source file:BeanUtilsCollectionsV2.java

 public static void main(String args[]) {
   BeanPredicate predicate =//  w w  w.  jav a  2 s . c  o m
     new BeanPredicate("title", PredicateUtils.uniquePredicate());

   Movie movie = new Movie();
   movie.setTitle("The Italian Job");

   Movie movie1 = new Movie();
   movie1.setTitle("The Italian Job");

   System.err.println(predicate.evaluate(movie)); // evaluates true
   System.err.println(predicate.evaluate(movie1)); // evaluates false

}

From source file:com.doculibre.constellio.utils.EqualityUtils.java

public static <T extends Object> boolean contains(Collection<T> collection, T bean, String propertyName) {
    Object propertyValue;/*from w  w  w . j  av  a  2s. c om*/
    try {
        propertyValue = PropertyUtils.getProperty(bean, propertyName);
    } catch (IllegalAccessException e) {
        throw new RuntimeException(e);
    } catch (InvocationTargetException e) {
        throw new RuntimeException(e);
    } catch (NoSuchMethodException e) {
        throw new RuntimeException(e);
    }
    EqualPredicate equalityPredicate = new EqualPredicate(propertyValue);
    BeanPredicate beanPredicate = new BeanPredicate(propertyName, equalityPredicate);
    return CollectionUtils.exists(collection, beanPredicate);
}

From source file:eionet.gdem.services.db.dao.SchemaDaoTest.java

@Test
public void getSchemasWithRelations() {
    List<Schema> schemas = schemaDao.getSchemasWithRelations();

    Schema schema = (Schema) CollectionUtils.find(schemas, new BeanPredicate("id", new EqualPredicate("1")));

    assertEquals("http://dd.eionet.europa.eu/GetSchema?id=TBL4564", schema.getSchema());
    assertEquals("Groundwater schema", schema.getDescription());
    assertEquals("seed-gw-schema.xsd", schema.getUplSchemaFileName());
    assertEquals(2, schema.getCountQaScripts());
    assertEquals(2, schema.getCountStylesheets());

}

From source file:com.googelcode.jpractices.common.SalaryHighPredicate.java

 void filterCollection(int salary)
{
   SalaryHighPredicate nameEqlPredicate = new SalaryHighPredicate(salary);
   BeanPredicate beanPredicate = new BeanPredicate("salary", nameEqlPredicate);
   Collection<Person> filteredCollection = CollectionUtils
            .select(personList, beanPredicate);
   System.out.println("Shows the person object whose salary is >= 26,000 ");
   for (Person person : filteredCollection) {
      System.out.println(person);
   }/*from w  w w. ja v a2  s.  co  m*/
}

From source file:com.googelcode.jpractices.Person.java

/**
     * select's the Person object's from collection of person objects based on arg
     * @param propertyName - Person's attribute (firstName (or) lastName (or) salary )
     * @param value - Value to be compared to propertyName
     *//*www .  ja  va2  s  .com*/
    void selectObjectsByName(String propertyName, String value) {
        EqualPredicate nameEqlPredicate = new EqualPredicate(value);
        BeanPredicate beanPredicate = new BeanPredicate(propertyName, nameEqlPredicate);
        Collection<Person> filteredCollection = CollectionUtils.select(personList, beanPredicate);
        System.out.println("Below are person object(s) whose " + propertyName + " is " + value);
        System.out
                .println("Matches for entered criteria " + CollectionUtils.countMatches(personList, beanPredicate));
        for (Person person : filteredCollection) {
            System.out.println(person);
        }
    }

From source file:com.googelcode.jpractices.Person.java

/**
     */*from   w  w  w.j  av a  2 s.  co  m*/
     * Here we are adding multiple predicate
     * filters the collection so that final person object will contain
     * firstName as "ganesh" & lastName as "gowtham"
     */
    void filterDataUsingMultipleCriteria() {
        EqualPredicate firstNameEqlPredicate = new EqualPredicate("ganesh");
        BeanPredicate firtsNameBeanPredicate = new BeanPredicate("firstName", firstNameEqlPredicate);
        EqualPredicate lastNameEqlPredicate2 = new EqualPredicate("gowtham");
        BeanPredicate lastNameBeanPredicate2 = new BeanPredicate("lastName", lastNameEqlPredicate2);
        Predicate[] allPredicateArray = { firtsNameBeanPredicate, lastNameBeanPredicate2 };
        Predicate allPredicate = PredicateUtils.allPredicate(allPredicateArray);
        Collection<Person> filteredCollection = CollectionUtils.select(personList, allPredicate);

        for (Person person : filteredCollection) {
            System.out.println(person);
        }
    }

From source file:eionet.gdem.services.db.dao.StylesheetDaoTest.java

@Test
public void getAllStylesheets() {
    List<Stylesheet> stylesheets = stylesheetDao.getStylesheets();

    Stylesheet stylesheet = (Stylesheet) CollectionUtils.find(stylesheets,
            new BeanPredicate("convId", new EqualPredicate("180")));

    assertTrue(stylesheets.size() > 10);
    assertEquals("stylesheet", stylesheet.getDescription());
    assertEquals("HTML", stylesheet.getType());
    assertEquals("file.xsl", stylesheet.getXslFileName());

}

From source file:eionet.gdem.services.db.dao.SchemaDaoTest.java

@Test
public void getSchemasWithNoRelations() {
    List<Schema> schemas = schemaDao.getSchemasWithRelations();

    Schema schema = (Schema) CollectionUtils.find(schemas, new BeanPredicate("id", new EqualPredicate("6")));

    assertEquals("http://dd.eionet.europa.eu/GetSchema?id=TBL112", schema.getSchema());
    assertEquals("No relations", schema.getDescription());
    assertNull(schema.getUplSchemaFileName());
    assertEquals(0, schema.getCountQaScripts());
    assertEquals(0, schema.getCountStylesheets());

}

From source file:com.googelcode.jpractices.Person.java

/**
     * select's the person object from collcetion based on propetyName and value
     * @param propertyName - Person's attribute (firstName (or) lastName (or) salary )
     * @param value - Value to be compared to propertyName
     *///from w  w  w.j a v a2s  .  c  o m
    void selectObjectFromCollection(String propertyName, String value) {
        EqualPredicate nameEqlPredicate = new EqualPredicate(value);
        BeanPredicate beanPredicate = new BeanPredicate(propertyName, nameEqlPredicate);
        System.out.println("Below are person object whose " + propertyName + " is " + value);
        System.out.println(CollectionUtils.find(personList, beanPredicate));
    }

From source file:eionet.gdem.web.struts.stylesheet.EditStylesheetFormAction.java

@Override
public ActionForward execute(ActionMapping actionMapping, ActionForm actionForm,
        HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse) {

    ActionMessages errors = new ActionMessages();

    StylesheetForm form = (StylesheetForm) actionForm;
    String stylesheetId = httpServletRequest.getParameter("stylesheetId");

    if (stylesheetId == null || stylesheetId.equals("")) {
        stylesheetId = (String) httpServletRequest.getAttribute("stylesheetId");
    }/*from   w w  w .  ja v  a2s  .c o  m*/

    ConvTypeHolder ctHolder = new ConvTypeHolder();
    StylesheetManager stylesheetManager = new StylesheetManager();

    try {
        Stylesheet stylesheet = stylesheetManager.getStylesheet(stylesheetId);

        if (stylesheet == null) {
            try {
                httpServletResponse.sendError(HttpServletResponse.SC_NOT_FOUND);
            } catch (IOException ex) {
                LOGGER.error("Failed to set 404 response status", ex);
            }

            return actionMapping.findForward(null);
        }

        form.setDescription(stylesheet.getDescription());
        form.setOutputtype(stylesheet.getType());
        form.setStylesheetId(stylesheet.getConvId());
        form.setXsl(stylesheet.getXsl());
        form.setXslContent(stylesheet.getXslContent());
        form.setXslFileName(stylesheet.getXslFileName());
        form.setModified(stylesheet.getModified());
        form.setChecksum(stylesheet.getChecksum());
        form.setSchemas(stylesheet.getSchemas());
        // set empty string if dependsOn is null to avoid struts error in define tag:
        // Define tag cannot set a null value
        form.setDependsOn(stylesheet.getDependsOn() == null ? "" : stylesheet.getDependsOn());

        if (stylesheet.getSchemas().size() > 0) {
            //set first schema for Run Conversion link
            form.setSchema(stylesheet.getSchemas().get(0).getSchema());
            // check if any related schema has type=EXCEL, if yes, then depends on info should be visible
            List<Schema> relatedSchemas = new ArrayList<Schema>(stylesheet.getSchemas());
            CollectionUtils.filter(relatedSchemas,
                    new BeanPredicate("schemaLang", new EqualPredicate("EXCEL")));
            if (relatedSchemas.size() > 0) {
                form.setShowDependsOnInfo(true);
                List<Stylesheet> existingStylesheets = new ArrayList<Stylesheet>();
                for (Schema relatedSchema : relatedSchemas) {
                    CollectionUtils.addAll(existingStylesheets, stylesheetManager
                            .getSchemaStylesheets(relatedSchema.getId(), stylesheetId).toArray());
                }
                form.setExistingStylesheets(existingStylesheets);
            }
        }
        ctHolder = stylesheetManager.getConvTypes();

        /** FIXME - do we need the list of DD XML Schemas on the page
        StylesheetListHolder stylesheetList = StylesheetListLoader.getGeneratedList(httpServletRequest);
        List<Schema> schemas = stylesheetList.getDdStylesheets();
        httpServletRequest.setAttribute("stylesheet.DDSchemas", schemas);
        */

        /*
        String schemaId = schema.getSchemaId(stylesheet.getSchema());
        if (!Utils.isNullStr(schemaId)) {
        httpServletRequest.setAttribute("schemaInfo", schema.getSchema(schemaId));
        httpServletRequest.setAttribute("existingStylesheets", stylesheetManager.getSchemaStylesheets(schemaId, stylesheetId));
        }
        */
        //httpServletRequest.setAttribute(StylesheetListLoader.STYLESHEET_LIST_ATTR, StylesheetListLoader.getStylesheetList(httpServletRequest));

    } catch (DCMException e) {
        e.printStackTrace();
        LOGGER.error("Edit stylesheet error", e);
        errors.add(ActionMessages.GLOBAL_MESSAGE, new ActionMessage(e.getErrorCode()));
        saveErrors(httpServletRequest, errors);
    }
    //TODO why is it needed to update session attribute in each request
    httpServletRequest.getSession().setAttribute("stylesheet.outputtype", ctHolder);

    return actionMapping.findForward("success");
}