Project

General

Profile

Download (4.35 KB) Statistics
| Branch: | Tag: | Revision:
1
/**
2
* Copyright (C) 2007 EDIT
3
* European Distributed Institute of Taxonomy
4
* http://www.e-taxonomy.eu
5
*
6
* The contents of this file are subject to the Mozilla Public License Version 1.1
7
* See LICENSE.TXT at the top of this package for the full license terms.
8
*/
9
package eu.etaxonomy.cdm.remote.view;
10

    
11

    
12

    
13
import java.util.Collection;
14
import java.util.HashMap;
15
import java.util.Iterator;
16
import java.util.Map;
17

    
18
import javax.servlet.http.HttpServletRequest;
19
import javax.servlet.http.HttpServletResponse;
20
import javax.xml.transform.stream.StreamResult;
21

    
22
import org.joda.time.DateTime;
23
import org.springframework.beans.factory.annotation.Autowired;
24
import org.springframework.beans.factory.annotation.Qualifier;
25
import org.springframework.oxm.Marshaller;
26
import org.springframework.web.servlet.view.AbstractView;
27

    
28
import com.github.dozermapper.core.Mapper;
29
import com.ibm.lsid.http.HTTPConstants;
30

    
31
import eu.etaxonomy.cdm.remote.dto.common.RemoteResponse;
32
import eu.etaxonomy.cdm.remote.dto.tdwg.BaseThing;
33
import eu.etaxonomy.remote.dto.rdf.Rdf;
34

    
35
/**
36
 * This class handles rdf views by mapping data objects to corresponding rdf objects, which are later
37
 * run through a marshaller to produce xml or json
38
 *
39
 * @author b.clarke,c.mathew
40
 * @version 1.0.0
41
 * @since 25-Nov-2012
42
 */
43

    
44
public class RdfView extends AbstractView {
45

    
46
	//FIXME : the standard marshaller as defined in remote.xml is not used in web service views so it
47
	//is commented aout for the moment.
48

    
49
//	private Marshaller marshaller;
50

    
51
	private Marshaller rdfMarshaller;
52

    
53
	private Mapper mapper;
54

    
55
//	private Map<Class<? extends CdmBase>,Class<? extends BaseThing>> classMap = new HashMap<Class<? extends CdmBase>,Class<? extends BaseThing>>();
56
	private Map<Class<? extends RemoteResponse>,Class<? extends BaseThing>> remoteClassMap = new HashMap<Class<? extends RemoteResponse>,Class<? extends BaseThing>>();
57

    
58
	private Integer expiresPlus;
59

    
60
    public enum Type{
61
        RDFXML("application/rdf+xml"),
62
        RDFJSON("application/rdf+json");
63

    
64
        private final String contentType;
65

    
66
        Type(String contentType){
67
            this.contentType = contentType;
68
        }
69

    
70
        public String getContentType(){
71
            return contentType;
72
        }
73
    }
74

    
75
    private Type type = Type.RDFXML;
76

    
77
    public Type getType() {
78
        return type;
79
    }
80

    
81
    public void setType(Type type) {
82
        this.type = type;
83
    }
84

    
85
    @Override
86
    public String getContentType() {
87
        return type.getContentType();
88
    }
89

    
90
	public RdfView() {
91
//		classMap.put(Taxon.class, TaxonConcept.class);
92
//		classMap.put(Synonym.class, TaxonConcept.class);
93
//		classMap.put(TaxonDescription.class, SpeciesProfileModel.class);
94

    
95
		remoteClassMap.put(eu.etaxonomy.cdm.remote.dto.namecatalogue.NameInformation.class,
96
				eu.etaxonomy.cdm.remote.dto.cdm.NameInformationRdf.class);
97
	}
98

    
99
	@Autowired
100
	@Qualifier("rdfMarshaller")
101
	public void setRdfMarshaller(Marshaller rdfMarshaller) {
102
		this.rdfMarshaller = rdfMarshaller;
103
	}
104

    
105
	@Autowired
106
	public void setMapper(Mapper mapper) {
107
		this.mapper = mapper;
108
	}
109

    
110
	public void setExpiresPlus(Integer expiresPlus) {
111
		this.expiresPlus = expiresPlus;
112
	}
113

    
114
	@Override
115
	protected void renderMergedOutputModel(Map<String, Object> model,HttpServletRequest request, HttpServletResponse response)
116
			throws Exception {
117
		if(expiresPlus != null) {
118
		    DateTime expires = new DateTime();
119
	        response.setHeader(HTTPConstants.EXPIRES_HEADER, HTTPConstants.HTTP_DATE_FORMAT.format(expires.plusDays(expiresPlus).toDate()));
120
	    }
121

    
122
		rdfMarshaller.marshal(buildRdf(model), new StreamResult(response.getOutputStream()));
123
	}
124

    
125
	public Rdf buildRdf(Map<String,? extends Object> model) {
126
		Rdf rdf = new Rdf();
127
		for(Object object : model.values()) {
128
//		    if(object instanceof IdentifiableEntity) {
129
//		        IdentifiableEntity identifiableEntity = (IdentifiableEntity)object;
130
//		        Class clazz = classMap.get(identifiableEntity.getClass());
131
//		        if(clazz != null) {
132
//		          rdf.addThing((BaseThing)mapper.map(identifiableEntity, clazz));
133
//		        }
134
//		    } else
135
		    if(object instanceof Collection) {
136
		    	Collection<?> c = (Collection<?>)object;
137
		    	Iterator<?> itr = c.iterator();
138
		    	while(itr.hasNext()) {
139
		    		Object obj = itr.next();
140
		    		Class<?> clazz = remoteClassMap.get(obj.getClass());
141
		    		if(clazz != null) {
142
		    			rdf.addThing((BaseThing)mapper.map(obj, clazz));
143

    
144
		    		}
145
		    	}
146
		    }
147
		}
148
		return rdf;
149
	}
150
}
(8-8/10)