Example usage for org.apache.commons.collections CollectionUtils find

List of usage examples for org.apache.commons.collections CollectionUtils find

Introduction

In this page you can find the example usage for org.apache.commons.collections CollectionUtils find.

Prototype

public static Object find(Collection collection, Predicate predicate) 

Source Link

Document

Finds the first element in the given collection which matches the given predicate.

Usage

From source file:org.opentestsystem.shared.permissions.dao.xml.XmlPermissionsPersister.java

public void deleteComponent(final String componentId) throws PermissionsPersistException {
    Permissions permissions = getPermissions();
    // step 1: delete the component.
    Component component = (Component) CollectionUtils.find(permissions.getComponents(), new Predicate() {
        @Override/*from   w w  w .  j ava2s  .  com*/
        public boolean evaluate(Object object) {
            if (StringUtils.equals(componentId, ((Component) object).getComponent()))
                return true;
            return false;
        }
    });
    List<Component> components = permissions.getComponents();
    components.remove(component);

    Iterator<PermissionMapping> it = this.getPermissions().getPermissionMapping().iterator();
    for (; it.hasNext();) {
        PermissionMapping mapping = it.next();
        if (StringUtils.equals(componentId, mapping.getComponent()))
            it.remove();
    }
    // step 2: persist this modified permissions object back to disk.
    persist();
}

From source file:org.opentestsystem.shared.permissions.dao.xml.XmlPermissionsPersister.java

@Override
public void addPermission(String componentId, final String permissionId)
        throws PermissionsPersistException, PermissionsRetrievalException {
    Component component = getComponentByComponentName(componentId);
    if (component == null)
        throw new PermissionsPersistException("Component does not exist.");
    Permission permission = (Permission) CollectionUtils.find(component.getPermissions(), new Predicate() {
        @Override/*ww  w.j  av a 2  s. c o  m*/
        public boolean evaluate(Object object) {
            if (StringUtils.equals(permissionId, ((Permission) object).getName()))
                return true;
            return false;
        }
    });

    if (permission == null) {
        permission = new Permission();
        permission.setName(permissionId);
        if (component.getPermissions() == null)
            component.setPermissions(new ArrayList<Permission>());
        component.getPermissions().add(permission);
        persist();
    } else
        throw new PermissionsPersistException("Permission already exists.");
}

From source file:org.opentestsystem.shared.permissions.dao.xml.XmlPermissionsPersister.java

@Override
public void deletePermission(final String componentId, final String permissionId)
        throws PermissionsPersistException, PermissionsRetrievalException {
    Component component = getComponentByComponentName(componentId);

    Permission permission = (Permission) CollectionUtils.find(component.getPermissions(), new Predicate() {
        @Override//  w  ww.  java  2s.  c  o  m
        public boolean evaluate(Object object) {
            if (StringUtils.equals(permissionId, ((Permission) object).getName()))
                return true;
            return false;
        }
    });
    List<Permission> permissionList = component.getPermissions();
    permissionList.remove(permission);
    // delete from permissionMapping
    List<PermissionMapping> mappings = this.getPermissions().getPermissionMapping();
    Iterator<PermissionMapping> it = mappings.iterator();
    for (; it.hasNext();) {
        PermissionMapping mapping = it.next();
        if (StringUtils.equals(componentId, mapping.getComponent())
                && StringUtils.equals(permissionId, mapping.getPermission()))
            it.remove();
    }
    // step 2: persist this modified permissions object back to disk.
    persist();
}

From source file:org.opentestsystem.shared.permissions.dao.xml.XmlPermissionsPersister.java

public void deleteEntity(final String roleId, final String entity)
        throws PermissionsPersistException, PermissionsRetrievalException {
    Role role = getRoleByRoleName(roleId);
    final String entityKey = this.getEntityByDescription(getAllAllowableEntities(), entity).getEntity();
    AllowableEntityType allowableEntity = (AllowableEntityType) CollectionUtils
            .find(role.getAllowableEntities(), new Predicate() {

                @Override/*from w  w  w.  j a va2 s . c o m*/
                public boolean evaluate(Object object) {
                    if (StringUtils.equals(entityKey.toLowerCase(),
                            ((AllowableEntityType) object).getEntity().toLowerCase()))
                        return true;
                    return false;
                }

            });

    if (allowableEntity != null)
        role.getAllowableEntities().remove(allowableEntity);

    persist();
}

From source file:org.opentestsystem.shared.permissions.dao.xml.XmlPermissionsPersister.java

public AllowableEntityType getEntityByDescription(List<AllowableEntityType> entities,
        final String description) {
    AllowableEntityType allowableEntity = (AllowableEntityType) CollectionUtils.find(entities, new Predicate() {

        @Override//from  w w  w .  j  a v  a  2 s .  c o m
        public boolean evaluate(Object object) {
            if (StringUtils.equals(description.toLowerCase(),
                    ((AllowableEntityType) object).getDescription().toLowerCase()))
                return true;
            return false;
        }
    });
    return allowableEntity;
}

From source file:org.opentestsystem.shared.permissions.dao.xml.XmlPermissionsPersister.java

public Permission getPermissionByName(List<Permission> list, final String permissionName) {
    Permission permission = (Permission) CollectionUtils.find(list, new Predicate() {
        @Override//from w  w  w. j  a v a2s . c o  m
        public boolean evaluate(Object object) {
            if (StringUtils.equals(permissionName.toLowerCase(), ((Permission) object).getName().toLowerCase()))
                return true;
            return false;
        }
    });
    return permission;
}

From source file:org.opentestsystem.shared.permissions.data.Role.java

public boolean hasEntity(final String entityName) {
    List<AllowableEntityType> entities = this.getAllowableEntities();
    if (entities == null)
        return false;
    AllowableEntityType entityType = (AllowableEntityType) CollectionUtils.find(entities, new Predicate() {
        @Override//from w  ww . jav  a2 s  .c o  m
        public boolean evaluate(Object object) {
            String entity = ((AllowableEntityType) object).getEntity().toLowerCase();
            if (StringUtils.equals(entityName.toLowerCase(), entity))
                return true;
            return false;
        }

    });

    if (entityType == null)
        return false;
    return true;
}

From source file:org.opentestsystem.shared.permissions.data.Role.java

public boolean exists(List<Role> roles) {
    if (roles == null)
        return false;
    final Role r = this;
    Role role = (Role) CollectionUtils.find(roles, new Predicate() {
        @Override/*from w  w  w  .  j  av  a 2s .  c om*/
        public boolean evaluate(Object object) {
            if (StringUtils.equals(r.getRole().toLowerCase(), ((Role) object).getRole().toLowerCase()))
                return true;
            return false;
        }

    });

    if (role == null)
        return false;
    return true;
}

From source file:org.openvpms.web.component.util.CollectionHelper.java

/**
 * Finds the first element in the given collection which matches the given
 * predicate./*from ww w  .j av  a2  s.  co m*/
 * <p/>
 * If the input collection or predicate is null, or no element of the
 * collection matches the predicate, null is returned.
 *
 * @param collection the collection to search. May be <tt>null</tt>
 * @param predicate  the predicate to use. May be <tt>null</tt>
 * @return the first element of the collection which matches the predicate
 *         or <Tt>null</tt> if none could be found
 */
@SuppressWarnings("unchecked")
public static <T> T find(Collection<T> collection, Predicate predicate) {
    return (T) CollectionUtils.find(collection, predicate);
}

From source file:org.oscarehr.PMmodule.exporter.DATISAgencyInformation.java

@Override
protected String exportData() throws ExportException {
    List<IntakeNode> intakeNodes = intake.getNode().getChildren();
    StringBuilder buf = new StringBuilder();

    IntakeNode agInfoNode = null;//from w  ww  .  j a  v  a2s  . c o m

    for (IntakeNode inode : intakeNodes) {
        if (inode.getLabelStr().startsWith(FILE_PREFIX)) {
            agInfoNode = inode;
            break;
        }
    }

    Set<IntakeAnswer> answers = intake.getAnswers();

    int counter = 0;
    for (IntakeAnswer ans : answers) {
        if (counter == fields.size()) {
            break;
        }
        if (ans.getNode().getParent().equals(agInfoNode)) {
            final String lbl = ans.getNode().getLabelStr().toUpperCase();
            DATISField found = (DATISField) CollectionUtils.find(fields, new Predicate() {

                public boolean evaluate(Object arg0) {
                    DATISField field = (DATISField) arg0;
                    if (lbl.startsWith(field.getName())) {
                        return true;
                    }
                    return false;
                }

            });

            if (found != null) {
                writeCSV(buf, ans, found);
                //writeData(buf, ans, found);
                counter++;
            }
        }
    }

    if (buf.lastIndexOf(",") == -1) {
        return buf.toString();
    }

    return buf.substring(0, buf.lastIndexOf(",")).toString();
}