From: <yo...@us...> - 2010-04-03 14:52:27
|
Revision: 672 http://treebase.svn.sourceforge.net/treebase/?rev=672&view=rev Author: youjun Date: 2010-04-03 14:52:13 +0000 (Sat, 03 Apr 2010) Log Message: ----------- import oai-pmh-provider Modified Paths: -------------- trunk/treebase-web/pom.xml Added Paths: ----------- trunk/treebase-web/src/main/java/org/cipres/treebase/web/controllers/OAIPMHController.java trunk/treebase-web/src/main/java/org/cipres/treebase/web/model/Identify.java trunk/treebase-web/src/main/java/org/cipres/treebase/web/model/OAIPMHCommand.java trunk/treebase-web/src/main/java/org/cipres/treebase/web/util/IdentifyUtil.java trunk/treebase-web/src/main/java/org/cipres/treebase/web/validators/OAIPMHValidator.java trunk/treebase-web/src/main/webapp/WEB-INF/identify.properties Modified: trunk/treebase-web/pom.xml =================================================================== --- trunk/treebase-web/pom.xml 2010-04-03 14:08:25 UTC (rev 671) +++ trunk/treebase-web/pom.xml 2010-04-03 14:52:13 UTC (rev 672) @@ -326,5 +326,20 @@ <version>3.0.4</version> </dependency> + <dependency> + <groupId>velocity-tools</groupId> + <artifactId>velocity-tools</artifactId> + <version>1.1</version> + </dependency> + <dependency> + <groupId>velocity-tools</groupId> + <artifactId>velocity-tools-generic</artifactId> + <version>1.1</version> + </dependency> + <dependency> + <groupId>velocity-tools</groupId> + <artifactId>velocity-tools-view</artifactId> + <version>1.1</version> + </dependency> </dependencies> </project> Added: trunk/treebase-web/src/main/java/org/cipres/treebase/web/controllers/OAIPMHController.java =================================================================== --- trunk/treebase-web/src/main/java/org/cipres/treebase/web/controllers/OAIPMHController.java (rev 0) +++ trunk/treebase-web/src/main/java/org/cipres/treebase/web/controllers/OAIPMHController.java 2010-04-03 14:52:13 UTC (rev 672) @@ -0,0 +1,275 @@ +package org.treebase.oai.web.controller; + +import java.lang.reflect.Method; +import java.text.ParseException; +import java.util.ArrayList; +import java.util.HashMap; +import java.util.List; +import java.util.Map; + +import javax.servlet.http.HttpServletRequest; +import javax.servlet.http.HttpServletResponse; + +import org.springframework.validation.BindException; +import org.springframework.web.servlet.ModelAndView; +import org.springframework.web.servlet.mvc.AbstractCommandController; +import org.treebase.oai.web.command.Identify; +import org.treebase.oai.web.command.OAIPMHCommand; +import org.treebase.oai.web.util.IdentifyUtil; +import org.cipres.treebase.domain.admin.Person; +import org.cipres.treebase.domain.study.ArticleCitation; +import org.cipres.treebase.domain.study.BookCitation; +import org.cipres.treebase.domain.study.Citation; +import org.cipres.treebase.domain.study.Study; +import org.cipres.treebase.domain.study.Submission; +import org.cipres.treebase.domain.study.SubmissionService; +import org.cipres.treebase.domain.study.StudyService; +/** + * OAIPMHController.java + * + * Created on Nov. 1, 2009 + * + * @author Youjun Guo + * + */ +public class OAIPMHController extends AbstractCommandController{ + + private SubmissionService submissionService; + private StudyService studyService; + private Identify identify; + + public OAIPMHController(){ + setCommandClass(OAIPMHCommand.class); + setCommandName("requestParams"); + } + + public SubmissionService getSubmissionService() { + return submissionService; + } + + public void setSubmissionService(SubmissionService submissionService) { + this.submissionService = submissionService; + } + + public StudyService getStudyService() { + return studyService; + } + + public void setStudyService(StudyService studyService) { + this.studyService = studyService; + } + + public Identify getIdentify() { + return identify; + } + + public void setIdentify(Identify identify) { + this.identify = identify; + } + + + + @Override + protected ModelAndView handle(HttpServletRequest request, + HttpServletResponse response, Object command, BindException errors) + throws Exception { + // TODO Auto-generated method stub + + OAIPMHCommand params = (OAIPMHCommand) command; + + Map model=errors.getModel(); + model.put("requestParams", params); + model.put("identify", this.identify); + + if(errors.getErrorCount()>0){ + if(errors.hasFieldErrors("metadataPrefix")){ + model.put("error_code", "cannotDisseminateFormat"); + model.put("error", "bad metadataPrefix"); + return new ModelAndView("error",model); + } + if(errors.hasFieldErrors("verb")){ + model.put("error_code", "badVerb"); + model.put("error", "no verb was found"); + return new ModelAndView("error",model); + } + if(errors.hasFieldErrors("identifier")){ + model.put("error_code", "idDoesNotExist"); + model.put("error", "no id was found"); + return new ModelAndView("error",model); + } + } + + Method method=null; + try{ + method=this.getClass().getMethod(params.getVerb(), new Class[]{OAIPMHCommand.class, Map.class}); + }catch(NoSuchMethodException nsme){ + model.put("error_code", "badVerb"); + model.put("error", "invalid verb"); + return new ModelAndView("error",model); + }catch(NullPointerException e){ + model.put("error_code", "badVerb"); + model.put("error", "missing verb"); + return (new ModelAndView("error",model)); + } + + return (ModelAndView) method.invoke(this, params, model); + + } + + public ModelAndView ListRecords(OAIPMHCommand params, Map model){ + + List<Submission> list=null; + try { + list = (List)submissionService.findSubmissionByCreateDateRange(IdentifyUtil.parseGranularity(identify.getGranularityPattern(),params.getModifiedFrom()), + IdentifyUtil.parseGranularity(identify.getGranularityPattern(),params.getModifiedUntil())); + } catch (ParseException e) { + model.put("error_code", "badArgument"); + model.put("error", "invalid from or until format"); + return (new ModelAndView("error",model)); + } + model.put("recordList", getRecordList(list)); + return (new ModelAndView("ListRecords",model)); + + } + + + public ModelAndView ListIdentifiers( OAIPMHCommand params, Map model){ + + List<Submission> list=null; + try { + list = (List)submissionService.findSubmissionByCreateDateRange(IdentifyUtil.parseGranularity(identify.getGranularityPattern(),params.getModifiedFrom()), + IdentifyUtil.parseGranularity(identify.getGranularityPattern(), params.getModifiedUntil())); + } catch (ParseException e) { + model.put("error_code", "badArgument"); + model.put("error", "invalid from or until format"); + return (new ModelAndView("error",model)); + } + model.put("recordList", getRecordList(list)); + return (new ModelAndView(params.getMetadataPrefix()+"_ListIdentifiers",model)); + + } + + public ModelAndView GetRecord( OAIPMHCommand params, Map model){ + + Submission submission = null; + + try{ + long id = IdentifyUtil.parseID(params); + submission = studyService.findByID(id).getSubmission(); + }catch(NumberFormatException nfe){ + model.put("error_code", "badArgument"); + model.put("error", "invalid id format"); + return (new ModelAndView("error",model)); + + } + catch (NullPointerException e){ + + model.put("error_code", "idDoesNotExist"); + model.put("error", "invalid id"); + return (new ModelAndView("error",model)); + + } + model.put("record", getRecordMap(submission)); + return (new ModelAndView("GetRecord",model)); + + } + + public ModelAndView Identify(OAIPMHCommand params, Map model){ + + + return (new ModelAndView("Identify",model)); + + } + + public ModelAndView ListSets(OAIPMHCommand params, Map model){ + model.put("error_code", "noSetHierarchy"); + model.put("error", "This repository does not support sets"); + return (new ModelAndView("error",model)); + + } + + public ModelAndView ListMetadataFormats(OAIPMHCommand params, Map model){ + + Submission submission = null; + + try{ + long id = IdentifyUtil.parseID(params); + submission = studyService.findByID(id).getSubmission(); + }catch(NumberFormatException nfe){ + model.put("error_code", "badArgument"); + model.put("error", "invalid id format"); + return (new ModelAndView("error",model)); + + } + catch (NullPointerException e){ + //id is optional for ListMetadataFormats + //return (new ModelAndView("error",model)); + } + + + return (new ModelAndView("ListMetadataFormats",model)); + + } + + + private Map getRecordMap(Submission submission){ + + Map map= new HashMap(); + + Study study=submission.getStudy(); + Citation citation=study.getCitation(); + String publisher=null; + if(!study.isPublished())return null; + + //System.out.println("ctype: "+citation.getCitationType()); + try{ + if(citation.getCitationType().toUpperCase().contains("BOOK")) + publisher=((BookCitation)citation).getPublisher(); + else publisher=((ArticleCitation)citation).getJournal(); + + + List<Person> authors=citation.getAuthors(); + + + map.put("title", citation.getTitle()); + map.put("creator", authors); + map.put("subject", citation.getKeywords()); + if(study.getName()!=null&study.getNotes()!=null) + map.put("description", study.getName()+" "+study.getNotes()); + else if(study.getNotes()==null) + map.put("description",study.getName()); + else + map.put("description",study.getNotes()); + map.put("publisher", publisher); + map.put("date", "published on "+citation.getPublishYear()); + map.put("identifier", "TreeBASE.org/study/TB2:s"+study.getId()); + map.put("datestamp", study.getReleaseDate()); + + }catch(NullPointerException e){ + //study 253 citation= null, data should be fixed + System.err.println("study "+study.getId()+ + " citation= "+e.getMessage()); + return null; + } + + //map.put("type", "text"); + //map.put("language", "en"); + + //map.put("issued", citation.getPublishYear()); + //map.put("abstract", citation.getAbstract()); + + + return map; + } + + private List getRecordList(List<Submission> sList) + { + List recordList=new ArrayList<Map>(); + + for(int i =0; i< sList.size(); i++){ + Map map=getRecordMap(sList.get(i)); + if(map!=null)recordList.add(map); + } + return recordList; + } +} Added: trunk/treebase-web/src/main/java/org/cipres/treebase/web/model/Identify.java =================================================================== --- trunk/treebase-web/src/main/java/org/cipres/treebase/web/model/Identify.java (rev 0) +++ trunk/treebase-web/src/main/java/org/cipres/treebase/web/model/Identify.java 2010-04-03 14:52:13 UTC (rev 672) @@ -0,0 +1,125 @@ +package org.treebase.oai.web.command; + +import java.text.SimpleDateFormat; +import java.util.Calendar; +import java.util.Date; +import java.util.GregorianCalendar; +import java.util.SimpleTimeZone; +import java.util.TimeZone; +/** + * + * @author youjun + * class hold identify data about the service + */ + +public class Identify { + + + private String baseURL; + private String repositoryName; + private String protocolVersion; + private String adminEmail; + private String earliestDatestamp; + private String deletedRecord; + private String granularity; + private String granularityPattern; + private String repositoryIdentifier; + private String sampleIdentifier; + private String dryadPerfix; + private String identifierDelimiter; + private String dublinPerfix; + + + public String getGranularityPattern() { + return granularityPattern; + } + public void setGranularityPattern(String granularityPattern) { + this.granularityPattern = granularityPattern; + } + + + public String getIdentifierDelimiter() { + return identifierDelimiter; + } + public void setIdentifierDelimiter(String identifierDelimiter) { + this.identifierDelimiter = identifierDelimiter; + } + public String getDublinPerfix() { + return dublinPerfix; + } + public void setDublinPerfix(String dublinPerfix) { + this.dublinPerfix = dublinPerfix; + } + public String getDryadPerfix() { + return dryadPerfix; + } + public void setDryadPerfix(String dryadPerfix) { + this.dryadPerfix = dryadPerfix; + } + + public String getRepositoryIdentifier() { + return repositoryIdentifier; + } + public void setRepositoryIdentifier(String repositoryIdentifier) { + this.repositoryIdentifier = repositoryIdentifier; + } + public String getSampleIdentifier() { + return sampleIdentifier; + } + public void setSampleIdentifier(String sampleIdentifier) { + this.sampleIdentifier = sampleIdentifier; + } + + public String getBaseURL() { + return baseURL; + } + public void setBaseURL(String baseRUL) { + this.baseURL = baseRUL; + } + public String getRepositoryName() { + return repositoryName; + } + public void setRepositoryName(String repositoryName) { + this.repositoryName = repositoryName; + } + public String getProtocolVersion() { + return protocolVersion; + } + public void setProtocolVersion(String protocolVersion) { + this.protocolVersion = protocolVersion; + } + public String getAdminEmail() { + return adminEmail; + } + public void setAdminEmail(String adminEmail) { + this.adminEmail = adminEmail; + } + public String getEarliestDatestamp() { + return earliestDatestamp; + } + public void setEarliestDatestamp(String earliestDatestamp) { + this.earliestDatestamp = earliestDatestamp; + } + public String getDeletedRecord() { + return deletedRecord; + } + public void setDeletedRecord(String deletedRecord) { + this.deletedRecord = deletedRecord; + } + public String getGranularity() { + return granularity; + } + public void setGranularity(String granularity) { + this.granularity = granularity; + } + + // return a current date object in UTC + public String getResponseDate(){ + SimpleDateFormat format = new SimpleDateFormat(granularityPattern); + Calendar cal = Calendar.getInstance(new SimpleTimeZone(0, "GMT")); + format.setCalendar(cal); + return format.format(new Date()); + + } + +} Added: trunk/treebase-web/src/main/java/org/cipres/treebase/web/model/OAIPMHCommand.java =================================================================== --- trunk/treebase-web/src/main/java/org/cipres/treebase/web/model/OAIPMHCommand.java (rev 0) +++ trunk/treebase-web/src/main/java/org/cipres/treebase/web/model/OAIPMHCommand.java 2010-04-03 14:52:13 UTC (rev 672) @@ -0,0 +1,74 @@ +package org.treebase.oai.web.command; +/** + * + * @author youjun + * class transfer page/query input data to controller + */ +public class OAIPMHCommand { + + private String from; + private String until; + private String resumptionToken; + private String identifier; + private String metadataPrefix; + private String set; + private String verb; + + public String getVerb() { + return verb; + } + public void setVerb(String verb) { + this.verb = verb; + } + public String getModifiedFrom() { + if(from!=null&&from.trim()!="")return from; + return "1995-11-04T00:00:00Z"; + } + + public String getFrom() { + return from; + } + + public void setFrom(String from) { + this.from = from; + } + public String getModifiedUntil() { + if(until != null && until.trim()!="") return until; + return ""; + } + + public String getUntil() { + return until; + } + + public void setUntil(String until) { + this.until = until; + } + public String getResumptionToken() { + return resumptionToken; + } + public void setResumptionToken(String resumptionToken) { + this.resumptionToken = resumptionToken; + } + public String getIdentifier() { + return identifier; + } + public void setIdentifier(String identifier) { + this.identifier = identifier; + } + public String getMetadataPrefix() { + return metadataPrefix; + } + public void setMetadataPrefix(String metadataPrefix) { + this.metadataPrefix = metadataPrefix; + } + public String getSet() { + return set; + } + public void setSet(String set) { + this.set = set; + } + + + +} Added: trunk/treebase-web/src/main/java/org/cipres/treebase/web/util/IdentifyUtil.java =================================================================== --- trunk/treebase-web/src/main/java/org/cipres/treebase/web/util/IdentifyUtil.java (rev 0) +++ trunk/treebase-web/src/main/java/org/cipres/treebase/web/util/IdentifyUtil.java 2010-04-03 14:52:13 UTC (rev 672) @@ -0,0 +1,51 @@ +package org.treebase.oai.web.util; + +import java.text.ParseException; +import java.text.SimpleDateFormat; +import java.util.Calendar; +import java.util.Date; +import java.util.GregorianCalendar; +import java.util.TimeZone; + +import org.treebase.oai.web.command.Identify; +import org.treebase.oai.web.command.OAIPMHCommand; +/* + * + * @author youjun + * + */ +public class IdentifyUtil { + + // return a studyID by parsing params.identifier + + public static long parseID(OAIPMHCommand params) + { + String [] ids = params.getIdentifier().split("[/:.]"); + + return Long.parseLong(ids[ids.length-1].replaceAll("[a-zA-Z]","")); + } + + + // convert a time String to date object based on granularity + + public static Date parseGranularity(String granularity, String time ) throws ParseException + { + if(time=="")return new Date(); + SimpleDateFormat sdf = new SimpleDateFormat(granularity); + Date utcDate = sdf.parse(time); + return utcToLocal(utcDate); + + + } + + //convert a date object from utc to local + + protected static Date utcToLocal(Date utcDate) + { + long utcMiliseconds = utcDate.getTime(); + + Calendar cal = new GregorianCalendar(); + cal.setTimeInMillis(utcMiliseconds); + return new Date(utcMiliseconds + cal.get(Calendar.ZONE_OFFSET) + cal.get(Calendar.DST_OFFSET)); + } +} Added: trunk/treebase-web/src/main/java/org/cipres/treebase/web/validators/OAIPMHValidator.java =================================================================== --- trunk/treebase-web/src/main/java/org/cipres/treebase/web/validators/OAIPMHValidator.java (rev 0) +++ trunk/treebase-web/src/main/java/org/cipres/treebase/web/validators/OAIPMHValidator.java 2010-04-03 14:52:13 UTC (rev 672) @@ -0,0 +1,40 @@ +package org.treebase.oai.web.validator; + +import org.springframework.validation.Errors; +import org.springframework.validation.ValidationUtils; +import org.springframework.validation.Validator; +import org.treebase.oai.web.command.OAIPMHCommand; + +public class OAIPMHValidator implements Validator { + + public boolean supports(Class pClass) { + return OAIPMHCommand.class.equals(pClass); + } + + public void validate(Object pValidatable, Errors pError) { + // ValidationUtils.rejectIfEmpty(e, "name", "name.empty"); + OAIPMHCommand command = (OAIPMHCommand) pValidatable; + + String verb = command.getVerb(); + String fPrefix = command.getMetadataPrefix(); + + ValidationUtils.rejectIfEmptyOrWhitespace(pError, "verb", "empyt verb"); + + + if(verb!=null && verb.equals("GetRecord")) + ValidationUtils.rejectIfEmptyOrWhitespace(pError, "identifier", "empyt id"); + + // check format prefix + if(verb!=null){ + if(verb.equals("GetRecord")||verb.equals("ListIdentifiers")||verb.equals("ListRecords")){ + + ValidationUtils.rejectIfEmptyOrWhitespace(pError, "metadataPrefix", "cannotDisseminateFormat"); + + if(fPrefix!=null) + if(!(fPrefix.equals("oai_dc")||fPrefix.equals("dryad"))) + pError.rejectValue("metadataPrefix","cannotDisseminateFormat"); + } + } + } + +} Added: trunk/treebase-web/src/main/webapp/WEB-INF/identify.properties =================================================================== --- trunk/treebase-web/src/main/webapp/WEB-INF/identify.properties (rev 0) +++ trunk/treebase-web/src/main/webapp/WEB-INF/identify.properties 2010-04-03 14:52:13 UTC (rev 672) @@ -0,0 +1,13 @@ +identify.baseURL=www.***.***:port/top +identify.repositoryName=TreeBASE repository +identify.protocolVersion=2.0 +identify.adminEmail=you...@ya... +identify.earliestDatestamp=1995-11-04T00:00:00Z +identify.deletedRecord=transient +identify.granularity=YYYY-MM-DDThh:mm:ssZ +identify.granularityPattern=yyyy-MM-dd'T'hh:mm:ss'Z' +identify.sampleIdentifier=TreeBASE.org/study/TB2:s6705 +identify.repositoryIdentifier=TreeBASE.org +identify.identifierDelimiter=: +identify.dublinPerfix=oai_dc +identify.dryadPerfix=dryad \ No newline at end of file This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |