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:net.sourceforge.fenixedu.applicationTier.Servico.student.AddStudentToFinalDegreeWorkStudentGroup.java

@Atomic
public static Boolean run(String groupOID, String username) throws FenixServiceException {
    check(RolePredicates.STUDENT_PREDICATE);
    FinalDegreeWorkGroup group = FenixFramework.getDomainObject(groupOID);
    Registration registration = findSomeRegistration(username);
    if (group == null || registration == null || group.getGroupStudentsSet() == null
            || CollectionUtils.find(group.getGroupStudentsSet(),
                    new PREDICATE_FIND_GROUP_STUDENT_BY_STUDENT(registration)) != null) {
        return false;
    }//from www  . j ava  2 s .c o m
    Scheduleing scheduleing = group.getExecutionDegree().getScheduling();

    if (scheduleing == null || scheduleing.getMaximumNumberOfStudents() == null) {
        throw new MaximumNumberOfStudentsUndefinedException();
    } else if (scheduleing.getMinimumCompletedCreditsFirstCycle() == null) {
        throw new MinimumCompletedCreditsFirstCycleUndefinedException();
    } else if (scheduleing.getMinimumCompletedCreditsSecondCycle() == null) {
        throw new MinimumCompletedCreditsSecondCycleUndefinedException();
        // } else if (scheduleing.getMinimumNumberOfCompletedCourses() ==
        // null) {
        // throw new MinimumNumberOfCompletedCoursesUndefinedException();
    } else if (scheduleing.getMaximumNumberOfStudents().intValue() <= group.getGroupStudentsSet().size()) {
        throw new MaximumNumberOfStudentsReachedException(scheduleing.getMaximumNumberOfStudents().toString());
    } else {
        final Integer maximumCurricularYearToCountCompletedCourses = scheduleing
                .getMaximumCurricularYearToCountCompletedCourses();
        final Integer minimumCompletedCurricularYear = scheduleing.getMinimumCompletedCurricularYear();
        // final Integer minimumNumberOfCompletedCourses =
        // scheduleing.getMinimumNumberOfCompletedCourses();
        final Integer minimumCompletedCreditsFirstCycle = scheduleing.getMinimumCompletedCreditsFirstCycle();
        final Integer minimumCompletedCreditsSecondCycle = scheduleing.getMinimumCompletedCreditsSecondCycle();

        final StudentCurricularPlan studentCurricularPlan = registration.getActiveStudentCurricularPlan();
        final DegreeCurricularPlan degreeCurricularPlan = studentCurricularPlan.getDegreeCurricularPlan();
        final Collection<CurricularCourseScope> degreesActiveCurricularCourseScopes = degreeCurricularPlan
                .getActiveCurricularCourseScopes();
        final StringBuilder notCompletedCurricularCourses = new StringBuilder();
        final Set<CurricularCourse> notCompletedCurricularCoursesForMinimumCurricularYear = new HashSet<CurricularCourse>();
        final Set<CurricularCourse> completedCurricularCourses = new HashSet<CurricularCourse>();
        // int numberCompletedCurricularCourses = 0;
        for (final CurricularCourseScope curricularCourseScope : degreesActiveCurricularCourseScopes) {
            final CurricularCourse curricularCourse = curricularCourseScope.getCurricularCourse();
            final boolean isCurricularCourseApproved = studentCurricularPlan
                    .isCurricularCourseApproved(curricularCourse);

            final CurricularSemester curricularSemester = curricularCourseScope.getCurricularSemester();
            final CurricularYear curricularYear = curricularSemester.getCurricularYear();

            if (minimumCompletedCurricularYear != null
                    && curricularYear.getYear() <= minimumCompletedCurricularYear) {
                if (!isCurricularCourseApproved) {
                    notCompletedCurricularCoursesForMinimumCurricularYear.add(curricularCourse);
                }
            }

            if (maximumCurricularYearToCountCompletedCourses == null || curricularYear.getYear()
                    .intValue() <= maximumCurricularYearToCountCompletedCourses.intValue()) {
                if (isCurricularCourseApproved) {
                    completedCurricularCourses.add(curricularCourseScope.getCurricularCourse());
                    // numberCompletedCurricularCourses++;
                } else {
                    if (notCompletedCurricularCourses.length() > 0) {
                        notCompletedCurricularCourses.append(", ");
                    }
                    notCompletedCurricularCourses.append(curricularCourseScope.getCurricularCourse().getName());
                }
            }
        }

        if (!notCompletedCurricularCoursesForMinimumCurricularYear.isEmpty()) {
            final StringBuilder stringBuilder = new StringBuilder();
            for (final CurricularCourse curricularCourse : notCompletedCurricularCoursesForMinimumCurricularYear) {
                if (stringBuilder.length() > 0) {
                    stringBuilder.append(", ");
                }
                stringBuilder.append(curricularCourse.getName());
            }
            final String[] args = { minimumCompletedCurricularYear.toString(), stringBuilder.toString() };
            throw new NotCompletedCurricularYearException(null, args);
        }

        // if (minimumNumberOfCompletedCourses != null) {
        // int numberCompletedCurricularCourses =
        // completedCurricularCourses.size();
        // if (numberCompletedCurricularCourses <
        // minimumNumberOfCompletedCourses) {
        // final int numberMissingCurricularCourses =
        // minimumNumberOfCompletedCourses -
        // numberCompletedCurricularCourses;
        // final String[] args = {
        // Integer.toString(numberMissingCurricularCourses),
        // notCompletedCurricularCourses.toString()};
        // throw new
        // MinimumNumberOfCompletedCoursesNotReachedException(null, args);
        // }
        // }

        if (minimumCompletedCreditsFirstCycle != null) {
            final Double completedCredits = studentCurricularPlan.getFirstCycle().getAprovedEctsCredits();
            if (minimumCompletedCreditsFirstCycle > completedCredits) {
                final String[] args = { completedCredits.toString(),
                        minimumCompletedCreditsFirstCycle.toString() };
                throw new MinimumCompletedCreditsFirstCycleNotReachedException(null, args);
            }
        }

        if (minimumCompletedCreditsSecondCycle != null) {
            final Double completedCredits = studentCurricularPlan.getSecondCycle().getAprovedEctsCredits();
            if (minimumCompletedCreditsSecondCycle > completedCredits) {
                final String[] args = { completedCredits.toString(),
                        minimumCompletedCreditsSecondCycle.toString() };
                throw new MinimumCompletedCreditsSecondCycleNotReachedException(null, args);
            }
        }
    }

    GroupStudent groupStudent = new GroupStudent();
    groupStudent.setRegistration(registration);
    groupStudent.setFinalDegreeDegreeWorkGroup(group);
    return true;
}

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:net.sourceforge.fenixedu.domain.reimbursementGuide.ReimbursementGuide.java

public ReimbursementGuideSituation getActiveReimbursementGuideSituation() {
    return (ReimbursementGuideSituation) CollectionUtils.find(getReimbursementGuideSituationsSet(),
            new Predicate() {
                @Override/*from w ww.j  a va2s.c  om*/
                public boolean evaluate(Object obj) {
                    ReimbursementGuideSituation situation = (ReimbursementGuideSituation) obj;
                    return situation.getState().getState().equals(State.ACTIVE);
                }
            });
}

From source file:de.forsthaus.backend.dao.impl.LanguageDAOImpl.java

@Override
public Language getLanguageByLocale(final String lanLocale) {
    return (Language) CollectionUtils.find(LANGUAGES, new Predicate() {
        @Override//from  w  ww .  j  a  va2s.  c  o  m
        public boolean evaluate(Object object) {
            return StringUtils.equals(lanLocale, ((Language) object).getLanLocale());
        }
    });
}

From source file:com.redhat.rhn.frontend.xmlrpc.preferences.locale.PreferencesLocaleHandler.java

/**
 * Set the TimeZone for the given user./*from   w w w .  jav  a 2  s  .c o m*/
 * @param loggedInUser The current user
 * in user.
 * @param login The login of the user whose timezone will be changed.
 * @param tzid TimeZone id
 * @return Returns 1 if successful (exception otherwise)
 *
 * @xmlrpc.doc Set a user's timezone.
 * @xmlrpc.param #param("string", "sessionKey")
 * @xmlrpc.param #param_desc("string", "login", "User's login name.")
 * @xmlrpc.param #param_desc("int", "tzid" "Timezone ID. (from listTimeZones)")
 * @xmlrpc.returntype #return_int_success()
 */
public int setTimeZone(User loggedInUser, String login, Integer tzid) {
    List tzs = UserManager.lookupAllTimeZones();
    Object o = CollectionUtils.find(tzs, new TzPredicate(tzid));
    if (o == null) {
        throw new InvalidTimeZoneException(tzid);
    }

    User target = XmlRpcUserHelper.getInstance().lookupTargetUser(loggedInUser, login);

    target.setTimeZone((RhnTimeZone) o);
    UserManager.storeUser(target);

    return 1;
}

From source file:de.hybris.platform.b2b.services.impl.DefaultB2BCustomerServiceTest.java

@Test
public void testAddMember() throws Exception {
    final B2BCustomerModel user = login("GC CEO");
    final B2BUnitModel unitToBeAssigned = b2bUnitService.getUnitForUid("GC Sales UK");
    Assert.assertNotNull(unitToBeAssigned);
    b2bCustomerService.addMember(user, unitToBeAssigned);
    Assert.assertNotNull(CollectionUtils.find(user.getGroups(), new Predicate() {
        @Override/*from w w  w  . j  a  v a2 s.c o m*/
        public boolean evaluate(final Object result) {
            return ((PrincipalGroupModel) result).getUid().equals("GC Sales UK");
        }
    }));
}

From source file:eionet.meta.service.VocabularyImportServiceTestBase.java

/**
 * Utility code to make test code more readable. Finds DataElement with given name in a list
 *
 * @param elems//from w  w w.  j a va 2  s . com
 *            DataElements to be searched
 * @param attrValue
 *            Value for comparison
 * @return First found DataElement
 */
public static DataElement findDataElemByAttrValue(List<DataElement> elems, String attrValue) {
    return (DataElement) CollectionUtils.find(elems,
            new DataElementEvaluateOnAttributeValuePredicate(attrValue));
}

From source file:net.sourceforge.fenixedu.applicationTier.Servico.teacher.services.EditTeacherAdviseService.java

protected void run(Teacher teacher, String executionPeriodID, final Integer studentNumber, Double percentage,
        AdviseType adviseType, RoleType roleType) throws FenixServiceException {

    ExecutionSemester executionSemester = FenixFramework.getDomainObject(executionPeriodID);

    Collection<Registration> students = Bennu.getInstance().getRegistrationsSet();
    Registration registration = (Registration) CollectionUtils.find(students, new Predicate() {
        @Override/*ww  w. j  a v  a  2s.c o  m*/
        public boolean evaluate(Object arg0) {
            Registration tempStudent = (Registration) arg0;
            return tempStudent.getNumber().equals(studentNumber);
        }
    });

    if (registration == null) {
        throw new FenixServiceException("errors.invalid.student-number");
    }

    TeacherService teacherService = teacher.getTeacherServiceByExecutionPeriod(executionSemester);
    if (teacherService == null) {
        teacherService = new TeacherService(teacher, executionSemester);
    }
    List<Advise> advises = registration.getAdvisesByTeacher(teacher);
    Advise advise = null;
    if (advises == null || advises.isEmpty()) {
        advise = new Advise(teacher, registration, adviseType, executionSemester, executionSemester);
    } else {
        advise = advises.iterator().next();
    }

    TeacherAdviseService teacherAdviseService = advise
            .getTeacherAdviseServiceByExecutionPeriod(executionSemester);
    if (teacherAdviseService == null) {
        teacherAdviseService = new TeacherAdviseService(teacherService, advise, percentage, roleType);
    } else {
        teacherAdviseService.updatePercentage(percentage, roleType);
    }
}

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:de.hybris.platform.b2bacceleratorfacades.company.impl.DefaultB2BCommerceB2BUserGroupFacade.java

@Override
public SearchPageData<UserData> getPagedCustomersForUserGroup(final PageableData pageableData,
        final String usergroupUID) {
    final SearchPageData<UserData> searchPageData = this.getPagedUserData(pageableData);
    // update the results with users that already have been selected.
    final UserGroupModel userGroupModel = getB2BCommerceB2BUserGroupService().getUserGroupForUID(usergroupUID,
            UserGroupModel.class);
    validateParameterNotNull(userGroupModel, String.format("No usergroup found for uid %s", usergroupUID));
    for (final UserData userData : searchPageData.getResults()) {
        final UserModel user = getUserService().getUserForUID(userData.getUid());
        userData.setSelected(CollectionUtils.find(user.getGroups(),
                new BeanPropertyValueEqualsPredicate(UserModel.UID, userGroupModel.getUid())) != null);
    }//from www. j  a v  a 2 s.c o m
    return searchPageData;
}