package net.sourceforge.fenixedu.webServices; import java.util.ArrayList; import java.util.Arrays; import java.util.Collection; import java.util.Collections; import java.util.List; import net.sourceforge.fenixedu._development.PropertiesManager; import net.sourceforge.fenixedu.dataTransferObject.externalServices.ResearcherDTO; import net.sourceforge.fenixedu.domain.Person; import net.sourceforge.fenixedu.domain.RootDomainObject; import net.sourceforge.fenixedu.domain.person.RoleType; import net.sourceforge.fenixedu.domain.research.Researcher; import net.sourceforge.fenixedu.webServices.exceptions.NotAuthorizedException; import org.apache.commons.collections.CollectionUtils; import org.apache.commons.collections.Predicate; import org.codehaus.xfire.MessageContext; public class SearchResearcher implements ISearchResearcher { private static final String storedPassword; private static final String storedUsername; static { storedUsername = PropertiesManager.getProperty("webServices.PersonManagement.getPersonInformation.username"); storedPassword = PropertiesManager.getProperty("webServices.PersonManagement.getPersonInformation.password"); } @Override public ResearcherDTO[] searchByName(String username, String password, String name, MessageContext context) throws NotAuthorizedException { checkPermissions(username, password, context); Collection result = Person.findInternalPersonByNameAndRole(name, RoleType.RESEARCHER); List results = new ArrayList(); for (Person person : result) { results.add(person.getResearcher()); } Collections.sort(results, Researcher.PUBLICATION_VOLUME_COMPARATOR); return getArrayFromResearchersList(results); } @Override public ResearcherDTO[] searchByKeyword(String username, String password, String keywords, MessageContext context) throws NotAuthorizedException { checkPermissions(username, password, context); if (keywords != null) { String[] keywordsArray = filterKeywords(keywords.split(" ")); List results = new ArrayList(); for (Researcher researcher : RootDomainObject.getInstance().getResearchers()) { if (researcher.getAllowsToBeSearched() && researcher.hasAtLeastOneKeyword(keywordsArray)) { results.add(researcher); } } Collections.sort(results, Researcher.PUBLICATION_VOLUME_COMPARATOR); return getArrayFromResearchersList(results); } return new ResearcherDTO[0]; } @Override public ResearcherDTO[] getAvailableResearchers(String username, String password, MessageContext context) throws NotAuthorizedException { checkPermissions(username, password, context); List results = new ArrayList(); for (Researcher researcher : RootDomainObject.getInstance().getResearchers()) { if (researcher.getPerson() != null && researcher.getAllowsToBeSearched()) { results.add(researcher); } } Collections.sort(results, Researcher.PUBLICATION_VOLUME_COMPARATOR); return getArrayFromResearchersList(results); } private static final int MIN_KEYWORD_LENGTH = 1; private String[] filterKeywords(String[] keywords) { Collection keywordsList = Arrays.asList(keywords); CollectionUtils.filter(keywordsList, new Predicate() { @Override public boolean evaluate(Object arg0) { return ((String) arg0).length() > MIN_KEYWORD_LENGTH; } }); return keywordsList.toArray(new String[0]); } private ResearcherDTO[] getArrayFromResearchersList(List results) { ResearcherDTO[] returnResults = new ResearcherDTO[results.size()]; int i = 0; for (Researcher researcher : results) returnResults[i++] = new ResearcherDTO(researcher); return returnResults; } private void checkPermissions(String username, String password, MessageContext context) throws NotAuthorizedException { // check user/pass if (!storedUsername.equals(username) || !storedPassword.equals(password)) { throw new NotAuthorizedException(); } // check hosts accessing this service // FIXME Anil: Its urgent to access this webservice for tests // if (!HostAccessControl.isAllowed(this, (ServletRequest) // context.getProperty("XFireServletController.httpServletRequest"))) { // throw new NotAuthorizedException(); // } } }