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

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

Introduction

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

Prototype

public static Collection collect(Iterator inputIterator, Transformer transformer) 

Source Link

Document

Transforms all elements from the inputIterator with the given transformer and adds them to the outputCollection.

Usage

From source file:org.fenixedu.academic.service.services.commons.ReadActiveDegreeCurricularPlansByDegreeType.java

private static Collection<InfoDegreeCurricularPlan> getActiveDegreeCurricularPlansByDegreeType(
        Predicate<DegreeType> degreeType, AccessControlPredicate<Object> permission) {
    List<DegreeCurricularPlan> degreeCurricularPlans = new ArrayList<DegreeCurricularPlan>();
    for (DegreeCurricularPlan dcp : DegreeCurricularPlan.readByDegreeTypeAndState(degreeType,
            DegreeCurricularPlanState.ACTIVE)) {
        if (permission != null) {
            if (!permission.evaluate(dcp.getDegree())) {
                continue;
            }//from   w  w w  .j av a  2 s. co m
        }
        degreeCurricularPlans.add(dcp);
    }

    return CollectionUtils.collect(degreeCurricularPlans, new Transformer() {

        @Override
        public Object transform(Object arg0) {
            DegreeCurricularPlan degreeCurricularPlan = (DegreeCurricularPlan) arg0;
            return InfoDegreeCurricularPlan.newInfoFromDomain(degreeCurricularPlan);
        }

    });
}

From source file:org.fenixedu.academic.service.services.commons.student.ReadStudentsFromDegreeCurricularPlan.java

protected List run(String degreeCurricularPlanID) throws FenixServiceException {
    // Read the Students
    DegreeCurricularPlan degreeCurricularPlan = FenixFramework.getDomainObject(degreeCurricularPlanID);

    Collection students = degreeCurricularPlan.getStudentCurricularPlansSet();

    if ((students == null) || (students.isEmpty())) {
        throw new NonExistingServiceException();
    }/*  w w w  . j a  va 2  s  . com*/

    return (List) CollectionUtils.collect(students, new Transformer() {
        @Override
        public Object transform(Object arg0) {
            StudentCurricularPlan studentCurricularPlan = (StudentCurricularPlan) arg0;
            return InfoStudentCurricularPlan.newInfoFromDomain(studentCurricularPlan);
        }

    });
}

From source file:org.fenixedu.academic.service.services.resourceAllocationManager.SearchExecutionCourses.java

private List<InfoExecutionCourse> fillInfoExecutionCourses(final AcademicInterval academicInterval,
        List<ExecutionCourse> executionCourses) {
    List<InfoExecutionCourse> result;
    result = (List<InfoExecutionCourse>) CollectionUtils.collect(executionCourses, new Transformer() {
        @Override/*from ww  w.  j  av  a  2 s .  c  om*/
        public Object transform(Object arg0) {
            InfoExecutionCourse infoExecutionCourse = null;
            infoExecutionCourse = getOccupancyLevels(arg0);
            return infoExecutionCourse;
        }

        private InfoExecutionCourse getOccupancyLevels(Object arg0) {

            InfoExecutionCourse infoExecutionCourse;
            ExecutionCourse executionCourse = (ExecutionCourse) arg0;

            Integer theoreticalCapacity = Integer.valueOf(0);
            Integer theoPraticalCapacity = Integer.valueOf(0);
            Integer praticalCapacity = Integer.valueOf(0);
            Integer labCapacity = Integer.valueOf(0);
            Integer doubtsCapacity = Integer.valueOf(0);
            Integer reserveCapacity = Integer.valueOf(0);

            Integer semCapacity = Integer.valueOf(0);
            Integer probCapacity = Integer.valueOf(0);
            Integer fieldCapacity = Integer.valueOf(0);
            Integer trainCapacity = Integer.valueOf(0);
            Integer tutCapacity = Integer.valueOf(0);

            Set<Shift> shifts = executionCourse.getAssociatedShifts();
            Iterator<Shift> iterator = shifts.iterator();

            while (iterator.hasNext()) {

                Shift shift = iterator.next();

                if (shift.containsType(ShiftType.TEORICA)) {
                    theoreticalCapacity = Integer
                            .valueOf(theoreticalCapacity.intValue() + shift.getLotacao().intValue());

                } else if (shift.containsType(ShiftType.TEORICO_PRATICA)) {
                    theoPraticalCapacity = Integer
                            .valueOf(theoPraticalCapacity.intValue() + shift.getLotacao().intValue());

                } else if (shift.containsType(ShiftType.DUVIDAS)) {
                    doubtsCapacity = Integer.valueOf(doubtsCapacity.intValue() + shift.getLotacao().intValue());

                } else if (shift.containsType(ShiftType.LABORATORIAL)) {
                    labCapacity = Integer.valueOf(labCapacity.intValue() + shift.getLotacao().intValue());

                } else if (shift.containsType(ShiftType.PRATICA)) {
                    praticalCapacity = Integer
                            .valueOf(praticalCapacity.intValue() + shift.getLotacao().intValue());

                } else if (shift.containsType(ShiftType.RESERVA)) {
                    reserveCapacity = Integer
                            .valueOf(reserveCapacity.intValue() + shift.getLotacao().intValue());

                } else if (shift.containsType(ShiftType.SEMINARY)) {
                    semCapacity = Integer.valueOf(semCapacity.intValue() + shift.getLotacao().intValue());

                } else if (shift.containsType(ShiftType.PROBLEMS)) {
                    probCapacity = Integer.valueOf(probCapacity.intValue() + shift.getLotacao().intValue());

                } else if (shift.containsType(ShiftType.FIELD_WORK)) {
                    fieldCapacity = Integer.valueOf(fieldCapacity.intValue() + shift.getLotacao().intValue());

                } else if (shift.containsType(ShiftType.TRAINING_PERIOD)) {
                    trainCapacity = Integer.valueOf(trainCapacity.intValue() + shift.getLotacao().intValue());

                } else if (shift.containsType(ShiftType.TUTORIAL_ORIENTATION)) {
                    tutCapacity = Integer.valueOf(tutCapacity.intValue() + shift.getLotacao().intValue());
                }
            }

            infoExecutionCourse = InfoExecutionCourse.newInfoFromDomain(executionCourse);
            List<Integer> capacities = new ArrayList<Integer>();

            if (theoreticalCapacity.intValue() != 0) {
                capacities.add(theoreticalCapacity);
            }
            if (theoPraticalCapacity.intValue() != 0) {
                capacities.add(theoPraticalCapacity);
            }
            if (doubtsCapacity.intValue() != 0) {
                capacities.add(doubtsCapacity);
            }
            if (labCapacity.intValue() != 0) {
                capacities.add(labCapacity);
            }
            if (praticalCapacity.intValue() != 0) {
                capacities.add(praticalCapacity);
            }
            if (reserveCapacity.intValue() != 0) {
                capacities.add(reserveCapacity);
            }

            if (semCapacity.intValue() != 0) {
                capacities.add(semCapacity);
            }
            if (probCapacity.intValue() != 0) {
                capacities.add(probCapacity);
            }
            if (fieldCapacity.intValue() != 0) {
                capacities.add(fieldCapacity);
            }
            if (trainCapacity.intValue() != 0) {
                capacities.add(trainCapacity);
            }
            if (tutCapacity.intValue() != 0) {
                capacities.add(tutCapacity);
            }

            int total = 0;

            if (!capacities.isEmpty()) {
                total = (Collections.min(capacities)).intValue();
            }

            if (total == 0) {
                infoExecutionCourse.setOccupancy(Double.valueOf(-1));
            } else {
                infoExecutionCourse.setOccupancy(NumberUtils.formatNumber(Double.valueOf(
                        (Double.valueOf(executionCourse.getAttendsSet().size()).floatValue() * 100 / total)),
                        1));
            }
            return infoExecutionCourse;
        }
    });

    return result;
}

From source file:org.geoserver.notification.geonode.GeoNodeJsonEncoder.java

@Override
public byte[] encode(Notification notification) throws Exception {
    byte[] ret = null;

    ObjectMapper mapper = new ObjectMapper();
    mapper.setDateFormat(new SimpleDateFormat("yyyy-MM-dd'T'HH:mm'Z'"));
    mapper.setVisibility(PropertyAccessor.ALL, Visibility.NONE);
    mapper.setVisibility(PropertyAccessor.FIELD, Visibility.ANY);
    mapper.setSerializationInclusion(Include.NON_NULL);
    mapper.configure(SerializationFeature.FAIL_ON_EMPTY_BEANS, false);

    KombuMessage message = new KombuMessage();

    message.setId(new UID().toString());
    message.setType(notification.getType() != null ? notification.getType().name() : null);
    message.setAction(notification.getAction() != null ? notification.getAction().name() : null);
    message.setTimestamp(new Date());
    message.setUser(notification.getUser());
    message.setOriginator(InetAddress.getLocalHost().getHostAddress());
    message.setProperties(notification.getProperties());
    if (notification.getObject() instanceof NamespaceInfo) {
        NamespaceInfo obj = (NamespaceInfo) notification.getObject();
        KombuNamespaceInfo source = new KombuNamespaceInfo();
        source.setId(obj.getId());//from  w w w  .  jav  a  2  s. c o m
        source.setType("NamespaceInfo");
        source.setName(obj.getName());
        source.setNamespaceURI(obj.getURI());
        message.setSource(source);
    }
    if (notification.getObject() instanceof WorkspaceInfo) {
        WorkspaceInfo obj = (WorkspaceInfo) notification.getObject();
        KombuWorkspaceInfo source = new KombuWorkspaceInfo();
        source.setId(obj.getId());
        source.setType("WorkspaceInfo");
        source.setName(obj.getName());
        source.setNamespaceURI("");
        message.setSource(source);
    }
    if (notification.getObject() instanceof LayerInfo) {
        LayerInfo obj = (LayerInfo) notification.getObject();
        KombuLayerInfo source = new KombuLayerInfo();
        source.setId(obj.getId());
        source.setType("LayerInfo");
        source.setName(obj.getName());
        source.setResourceType(obj.getType() != null ? obj.getType().name() : "");
        BeanToPropertyValueTransformer transformer = new BeanToPropertyValueTransformer("name");
        Collection<String> styleNames = CollectionUtils.collect(obj.getStyles(), transformer);
        source.setStyles(StringUtils.join(styleNames.toArray()));
        source.setDefaultStyle(obj.getDefaultStyle() != null ? obj.getDefaultStyle().getName() : "");
        ResourceInfo res = obj.getResource();
        source.setWorkspace(res.getStore() != null
                ? res.getStore().getWorkspace() != null ? res.getStore().getWorkspace().getName() : ""
                : "");
        if (res.getNativeBoundingBox() != null) {
            source.setBounds(new Bounds(res.getNativeBoundingBox()));
        }
        if (res.getLatLonBoundingBox() != null) {
            source.setGeographicBunds(new Bounds(res.getLatLonBoundingBox()));
        }
        message.setSource(source);
    }
    if (notification.getObject() instanceof LayerGroupInfo) {
        LayerGroupInfo obj = (LayerGroupInfo) notification.getObject();
        KombuLayerGroupInfo source = new KombuLayerGroupInfo();
        source.setId(obj.getId());
        source.setType("LayerGroupInfo");
        source.setName(obj.getName());
        source.setWorkspace(obj.getWorkspace() != null ? obj.getWorkspace().getName() : "");
        source.setMode(obj.getType().name());
        String rootStyle = obj.getRootLayerStyle() != null ? obj.getRootLayerStyle().getName() : "";
        source.setRootLayerStyle(rootStyle);
        source.setRootLayer(obj.getRootLayer() != null ? obj.getRootLayer().getPath() : "");
        for (PublishedInfo pl : obj.getLayers()) {
            KombuLayerSimpleInfo kl = new KombuLayerSimpleInfo();
            if (pl instanceof LayerInfo) {
                LayerInfo li = (LayerInfo) pl;
                kl.setName(li.getName());
                String lstyle = li.getDefaultStyle() != null ? li.getDefaultStyle().getName() : "";
                if (!lstyle.equals(rootStyle)) {
                    kl.setStyle(lstyle);
                }
                source.addLayer(kl);
            }
        }
        message.setSource(source);
    }
    if (notification.getObject() instanceof ResourceInfo) {
        ResourceInfo obj = (ResourceInfo) notification.getObject();
        KombuResourceInfo source = null;
        if (notification.getObject() instanceof FeatureTypeInfo) {
            source = new KombuFeatureTypeInfo();
            source.setType("FeatureTypeInfo");
        }
        if (notification.getObject() instanceof CoverageInfo) {
            source = new KombuCoverageInfo();
            source.setType("CoverageInfo");
        }
        if (notification.getObject() instanceof WMSLayerInfo) {
            source = new KombuWMSLayerInfo();
            source.setType("WMSLayerInfo");
        }
        if (source != null) {
            source.setId(obj.getId());
            source.setName(obj.getName());
            source.setWorkspace(obj.getStore() != null
                    ? obj.getStore().getWorkspace() != null ? obj.getStore().getWorkspace().getName() : ""
                    : "");
            source.setNativeName(obj.getNativeName());
            source.setStore(obj.getStore() != null ? obj.getStore().getName() : "");
            if (obj.getNativeBoundingBox() != null) {
                source.setGeographicBunds(new Bounds(obj.getNativeBoundingBox()));
            }
            if (obj.boundingBox() != null) {
                source.setBounds(new Bounds(obj.boundingBox()));
            }
        }
        message.setSource(source);
    }
    if (notification.getObject() instanceof StoreInfo) {
        StoreInfo obj = (StoreInfo) notification.getObject();
        KombuStoreInfo source = new KombuStoreInfo();
        source.setId(obj.getId());
        source.setType("StoreInfo");
        source.setName(obj.getName());
        source.setWorkspace(obj.getWorkspace() != null ? obj.getWorkspace().getName() : "");
        message.setSource(source);
    }
    ret = mapper.writeValueAsBytes(message);
    return ret;

}

From source file:org.geoserver.notification.NotificationCatalogListener.java

private void handleLayerGroupInfoChange(Map<String, Object> properties, final List<String> changedProperties,
        final List<Object> oldValues, final List<Object> newValues, final LayerGroupInfo lgInfo) {

    if (changedProperties.contains("layers")) {
        final int layersIndex = changedProperties.indexOf("layers");
        Object oldLayers = oldValues.get(layersIndex);
        Object newLayers = newValues.get(layersIndex);
    }// w ww .  j av  a2s .co  m

    if (changedProperties.contains("styles")) {
        final int stylesIndex = changedProperties.indexOf("styles");
        BeanToPropertyValueTransformer transformer = new BeanToPropertyValueTransformer("name");
        String oldStyles = StringUtils.join(
                CollectionUtils.collect((Set<StyleInfo>) oldValues.get(stylesIndex), transformer).toArray());
        String newStyles = StringUtils.join(
                CollectionUtils.collect((Set<StyleInfo>) newValues.get(stylesIndex), transformer).toArray());
        if (!oldStyles.equals(newStyles)) {
            properties.put("styles", newStyles);
        }
    }
}

From source file:org.geoserver.notification.NotificationCatalogListener.java

private void handleLayerInfoChange(Map<String, Object> properties, final List<String> changedProperties,
        final List<Object> oldValues, final List<Object> newValues, final LayerInfo li) {

    if (changedProperties.contains("defaultStyle")) {
        final int propIndex = changedProperties.indexOf("defaultStyle");
        final StyleInfo oldStyle = (StyleInfo) oldValues.get(propIndex);
        final StyleInfo newStyle = (StyleInfo) newValues.get(propIndex);

        final String oldStyleName = oldStyle.prefixedName();
        final String newStyleName = newStyle.prefixedName();
        if (!oldStyleName.equals(newStyleName)) {
            properties.put("defaultStyle", newStyleName);
        }//w  w  w . j  av  a2s.  c om
    }

    if (changedProperties.contains("styles")) {
        final int stylesIndex = changedProperties.indexOf("styles");
        BeanToPropertyValueTransformer transformer = new BeanToPropertyValueTransformer("name");
        String oldStyles = StringUtils.join(
                CollectionUtils.collect((Set<StyleInfo>) oldValues.get(stylesIndex), transformer).toArray());
        String newStyles = StringUtils.join(
                CollectionUtils.collect((Set<StyleInfo>) newValues.get(stylesIndex), transformer).toArray());
        if (!oldStyles.equals(newStyles)) {
            properties.put("styles", newStyles);
        }
    }

}

From source file:org.geoserver.security.iride.entity.util.Utils.java

/**
 *
 * @param invalidTokenValuesMessageTemplate
 * @param invalidTokenValues/*from w  w  w . ja v  a  2  s  . co  m*/
 * @return
 */
public static String toString(final String invalidTokenValuesMessageTemplate,
        IrideIdentityInvalidTokenValue... invalidTokenValues) {
    return StringUtils
            .join(CollectionUtils.collect(Arrays.asList(defaultToEmpty(invalidTokenValues)), new Transformer() {
                /*
                 * (non-Javadoc)
                 * @see org.apache.commons.collections.Transformer#transform(java.lang.Object)
                 */
                @Override
                public String transform(Object input) {
                    if (input == null) {
                        return "";
                    } else {
                        final IrideIdentityInvalidTokenValue invalidToken = (IrideIdentityInvalidTokenValue) input;

                        return String.format(invalidTokenValuesMessageTemplate, invalidToken.getToken(),
                                String.valueOf(invalidToken.getValue()));
                    }
                }
            }), SystemUtils.LINE_SEPARATOR);
}

From source file:org.geoserver.security.iride.util.IrideSecurityUtils.java

/**
 *
 * @param invalidTokenValuesMessageTemplate
 * @param invalidTokenValues//from   w ww.ja va 2 s. c  om
 * @return
 */
public static String toString(final String invalidTokenValuesMessageTemplate,
        IrideIdentityInvalidTokenValue... invalidTokenValues) {
    return StringUtils
            .join(CollectionUtils.collect(Arrays.asList(defaultToEmpty(invalidTokenValues)), new Transformer() {
                /*
                 * (non-Javadoc)
                 * @see org.apache.commons.collections.Transformer#transform(java.lang.Object)
                 */
                @Override
                public String transform(Object input) {
                    if (input == null) {
                        return StringUtils.EMPTY;
                    } else {
                        final IrideIdentityInvalidTokenValue invalidToken = (IrideIdentityInvalidTokenValue) input;

                        return String.format(invalidTokenValuesMessageTemplate, invalidToken.getToken(),
                                String.valueOf(invalidToken.getValue()));
                    }
                }
            }), SystemUtils.LINE_SEPARATOR);
}

From source file:org.geoserver.web.data.resource.FeatureResourceConfigurationPanel.java

private void validateCqlFilter(FeatureTypeInfo typeInfo, String cqlFilterString) throws Exception {
    Filter cqlFilter = null;/*  ww  w.  j  ava  2  s .  c  om*/
    if (cqlFilterString != null && !cqlFilterString.isEmpty()) {
        cqlFilter = ECQL.toFilter(cqlFilterString);
        FeatureType ft = typeInfo.getFeatureType();
        if (ft instanceof SimpleFeatureType) {

            SimpleFeatureType sft = (SimpleFeatureType) ft;
            BeanToPropertyValueTransformer transformer = new BeanToPropertyValueTransformer("localName");
            Collection<String> featureAttributesNames = CollectionUtils.collect(sft.getAttributeDescriptors(),
                    transformer);

            FilterAttributeExtractor filterAttriubtes = new FilterAttributeExtractor(null);
            cqlFilter.accept(filterAttriubtes, null);
            Set<String> filterAttributesNames = filterAttriubtes.getAttributeNameSet();
            for (String filterAttributeName : filterAttributesNames) {
                if (!featureAttributesNames.contains(filterAttributeName)) {
                    throw new ResourceConfigurationException(
                            ResourceConfigurationException.CQL_ATTRIBUTE_NAME_NOT_FOUND_$1,
                            new Object[] { filterAttributeName });
                }
            }
        }
    }
}

From source file:org.intermine.api.profile.StorableBag.java

/**
 * Delete a given set of bag values from the bag value table. If an empty list is passed in,
 * no values will be deleted. If null is passed in all values will be deleted.
 * @param values The values to delete. <code>null</code> is understood
 *               as <code>ALL VALUES.</code>.
 *///ww w. j  a  v  a 2 s  .  c  o  m
protected void deleteSomeBagValues(final List<String> values) {
    Connection conn = null;
    PreparedStatement stm = null;
    ObjectStoreWriter uosw = getUserProfileWriter();
    List<String> clauses = new ArrayList<String>(Arrays.asList("savedBagId = ?"));

    if (values != null) {
        Collection<String> placeHolders = CollectionUtils.collect(values, new ConstantTransformer("?"));
        String valuesList = StringUtils.join(placeHolders, ", ");
        if (!valuesList.isEmpty()) {
            clauses.add("value IN (" + valuesList + ")");
        }
    }

    try {
        conn = ((ObjectStoreWriterInterMineImpl) uosw).getConnection();
        String sql = "DELETE FROM " + InterMineBag.BAG_VALUES + " WHERE " + StringUtils.join(clauses, " AND ");
        stm = conn.prepareStatement(sql);
        stm.setInt(1, savedBagId);
        for (int i = 0; values != null && i < values.size(); i++) {
            stm.setString(i + 2, values.get(i));
        }
        stm.executeUpdate();
    } catch (SQLException sqle) {
        throw new RuntimeException("Error deleting the " + (values == null ? "" : values.size() + " ")
                + "bagvalues of bag : " + savedBagId, sqle);
    } finally {
        if (stm != null) {
            try {
                stm.close();
            } catch (SQLException e) {
                throw new RuntimeException("Problem closing resources", e);
            }
        }
        ((ObjectStoreWriterInterMineImpl) uosw).releaseConnection(conn);
    }
}