=== modified file 'dhis-2/dhis-web/dhis-web-api/pom.xml' --- dhis-2/dhis-web/dhis-web-api/pom.xml 2011-11-03 16:43:51 +0000 +++ dhis-2/dhis-web/dhis-web-api/pom.xml 2011-11-12 15:46:34 +0000 @@ -39,6 +39,10 @@ jackson-mapper-asl + org.codehaus.jackson + jackson-xc + + javax.xml.bind jaxb-api === added file 'dhis-2/dhis-web/dhis-web-api/src/main/java/org/hisp/dhis/api/controller/ChartController.java' --- dhis-2/dhis-web/dhis-web-api/src/main/java/org/hisp/dhis/api/controller/ChartController.java 1970-01-01 00:00:00 +0000 +++ dhis-2/dhis-web/dhis-web-api/src/main/java/org/hisp/dhis/api/controller/ChartController.java 2011-11-12 15:46:34 +0000 @@ -0,0 +1,68 @@ +package org.hisp.dhis.api.controller; + +import org.hisp.dhis.api.listener.IdentifiableObjectListener; +import org.hisp.dhis.api.resources.XChart; +import org.hisp.dhis.api.resources.XCharts; +import org.hisp.dhis.chart.Chart; +import org.hisp.dhis.chart.ChartService; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.stereotype.Controller; +import org.springframework.web.bind.annotation.PathVariable; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RequestMethod; +import org.springframework.web.bind.annotation.ResponseBody; + +import javax.servlet.http.HttpServletRequest; +import java.util.Collection; + +@Controller +@RequestMapping( value = "/charts" ) +public class ChartController +{ + @Autowired + private ChartService chartService; + + @RequestMapping( method = RequestMethod.GET ) + public XCharts getCharts( HttpServletRequest request ) + { + XCharts charts = new XCharts(); + + Collection allCharts = chartService.getAllCharts(); + + for ( Chart chart : allCharts ) + { + charts.getCharts().add( new XChart( chart ) ); + } + + new IdentifiableObjectListener( request ).beforeMarshal( charts ); + + return charts; + } + + @RequestMapping( value = "/{uid}", method = RequestMethod.GET ) + public XChart getChart( @PathVariable( "uid" ) Integer uid, HttpServletRequest request ) + { + XChart chart = new XChart( chartService.getChart( uid ) ); + + new IdentifiableObjectListener( request ).beforeMarshal( chart ); + + return chart; + } + + @RequestMapping( method = RequestMethod.POST ) + @ResponseBody + public void postChart( XChart chart ) + { + + } + + /* + @RequestMapping( method = RequestMethod.POST ) + public void postChart( InputStream inputStream ) throws IOException + { + StringWriter writer = new StringWriter(); + IOUtils.copy( inputStream, writer, "UTF-8" ); + System.err.println( writer.toString() ); + } + */ +} === removed file 'dhis-2/dhis-web/dhis-web-api/src/main/java/org/hisp/dhis/api/controller/ElementController.java' --- dhis-2/dhis-web/dhis-web-api/src/main/java/org/hisp/dhis/api/controller/ElementController.java 2011-11-03 16:43:51 +0000 +++ dhis-2/dhis-web/dhis-web-api/src/main/java/org/hisp/dhis/api/controller/ElementController.java 1970-01-01 00:00:00 +0000 @@ -1,40 +0,0 @@ -package org.hisp.dhis.api.controller; - -import org.hisp.dhis.api.resources.Element; -import org.hisp.dhis.api.resources.Elements; -import org.springframework.stereotype.Controller; -import org.springframework.ui.Model; -import org.springframework.web.bind.annotation.PathVariable; -import org.springframework.web.bind.annotation.RequestMapping; - -@Controller -@RequestMapping( value = "/elements" ) -public class ElementController -{ - private Elements elements = new Elements(); - - public ElementController() - { - elements.getElements().add( new Element( 1, "Element #1" ) ); - elements.getElements().add( new Element( 2, "Element #2" ) ); - elements.getElements().add( new Element( 3, "Element #3" ) ); - elements.getElements().add( new Element( 4, "Element #4" ) ); - elements.getElements().add( new Element( 5, "Element #5" ) ); - } - - @RequestMapping - public String getElements( Model model ) - { - model.addAttribute( "elements", elements ); - - return "elements"; - } - - @RequestMapping( value = "/{uid}" ) - public String getElement( @PathVariable( "uid" ) Integer uid, Model model ) - { - model.addAttribute( "element", elements.getElement( uid ) ); - - return "element"; - } -} === removed file 'dhis-2/dhis-web/dhis-web-api/src/main/java/org/hisp/dhis/api/controller/IndexController.java' --- dhis-2/dhis-web/dhis-web-api/src/main/java/org/hisp/dhis/api/controller/IndexController.java 2011-11-02 11:14:35 +0000 +++ dhis-2/dhis-web/dhis-web-api/src/main/java/org/hisp/dhis/api/controller/IndexController.java 1970-01-01 00:00:00 +0000 @@ -1,18 +0,0 @@ -package org.hisp.dhis.api.controller; - -import org.springframework.stereotype.Controller; -import org.springframework.web.bind.annotation.RequestMapping; -import org.springframework.web.bind.annotation.RequestMethod; -import org.springframework.web.bind.annotation.ResponseBody; - -@Controller -@RequestMapping( value = "/" ) -public class IndexController -{ - @RequestMapping( method = RequestMethod.GET ) - @ResponseBody - public String index() - { - return "Nothing to see here yet."; - } -} === added directory 'dhis-2/dhis-web/dhis-web-api/src/main/java/org/hisp/dhis/api/converter' === added file 'dhis-2/dhis-web/dhis-web-api/src/main/java/org/hisp/dhis/api/converter/Jaxb2HttpMessageConverter.java' --- dhis-2/dhis-web/dhis-web-api/src/main/java/org/hisp/dhis/api/converter/Jaxb2HttpMessageConverter.java 1970-01-01 00:00:00 +0000 +++ dhis-2/dhis-web/dhis-web-api/src/main/java/org/hisp/dhis/api/converter/Jaxb2HttpMessageConverter.java 2011-11-12 15:46:34 +0000 @@ -0,0 +1,49 @@ +package org.hisp.dhis.api.converter; + +import org.hisp.dhis.api.resources.XIdentifiableObject; +import org.springframework.http.HttpInputMessage; +import org.springframework.http.HttpOutputMessage; +import org.springframework.http.MediaType; +import org.springframework.http.converter.HttpMessageConverter; +import org.springframework.http.converter.HttpMessageNotReadableException; +import org.springframework.http.converter.HttpMessageNotWritableException; + +import java.io.IOException; +import java.util.List; + +public class Jaxb2HttpMessageConverter implements HttpMessageConverter +{ + @Override + public boolean canRead( Class clazz, MediaType mediaType ) + { + System.err.println("canread"); + return false; //To change body of implemented methods use File | Settings | File Templates. + } + + @Override + public boolean canWrite( Class clazz, MediaType mediaType ) + { + System.err.println("canwriter"); + return false; //To change body of implemented methods use File | Settings | File Templates. + } + + @Override + public List getSupportedMediaTypes() + { + System.err.println("getsupportedmediatypes"); + return null; //To change body of implemented methods use File | Settings | File Templates. + } + + @Override + public XIdentifiableObject read( Class clazz, HttpInputMessage inputMessage ) throws IOException, HttpMessageNotReadableException + { + System.err.println("read"); + return null; //To change body of implemented methods use File | Settings | File Templates. + } + + @Override + public void write( XIdentifiableObject identifiableObject, MediaType contentType, HttpOutputMessage outputMessage ) throws IOException, HttpMessageNotWritableException + { + System.err.println("write"); + } +} === added directory 'dhis-2/dhis-web/dhis-web-api/src/main/java/org/hisp/dhis/api/listener' === added file 'dhis-2/dhis-web/dhis-web-api/src/main/java/org/hisp/dhis/api/listener/IdentifiableObjectListener.java' --- dhis-2/dhis-web/dhis-web-api/src/main/java/org/hisp/dhis/api/listener/IdentifiableObjectListener.java 1970-01-01 00:00:00 +0000 +++ dhis-2/dhis-web/dhis-web-api/src/main/java/org/hisp/dhis/api/listener/IdentifiableObjectListener.java 2011-11-12 15:46:34 +0000 @@ -0,0 +1,46 @@ +package org.hisp.dhis.api.listener; + +import org.hisp.dhis.api.resources.XChart; +import org.hisp.dhis.api.resources.XCharts; +import org.hisp.dhis.api.resources.XIdentifiableObject; +import org.springframework.util.StringUtils; + +import javax.servlet.http.HttpServletRequest; +import javax.xml.bind.Marshaller; + +public class IdentifiableObjectListener extends Marshaller.Listener { + private HttpServletRequest request; + + public IdentifiableObjectListener(HttpServletRequest request) { + this.request = request; + } + + @Override + public void beforeMarshal(Object source) { + if (source instanceof XCharts) { + XCharts charts = (XCharts) source; + + for (XChart chart : charts.getCharts()) { + addHref(chart, request); + } + } else if (source instanceof XChart) { + XChart chart = (XChart) source; + addHref(chart, request); + } + } + + public void addHref(XIdentifiableObject identifiableObject, HttpServletRequest request) { + String path = request.getRequestURL().toString(); + path = StringUtils.stripFilenameExtension(path); + + while (path.lastIndexOf("/") == path.length() - 1) { + path = path.substring(0, path.length() - 1); + } + + identifiableObject.setHref(path + "/" + identifiableObject.getId()); + } + + @Override + public void afterMarshal(Object source) { + } +} === removed file 'dhis-2/dhis-web/dhis-web-api/src/main/java/org/hisp/dhis/api/resources/Element.java' --- dhis-2/dhis-web/dhis-web-api/src/main/java/org/hisp/dhis/api/resources/Element.java 2011-11-03 16:43:51 +0000 +++ dhis-2/dhis-web/dhis-web-api/src/main/java/org/hisp/dhis/api/resources/Element.java 1970-01-01 00:00:00 +0000 @@ -1,53 +0,0 @@ -package org.hisp.dhis.api.resources; - -import javax.xml.bind.annotation.XmlAccessType; -import javax.xml.bind.annotation.XmlAccessorType; -import javax.xml.bind.annotation.XmlAttribute; -import javax.xml.bind.annotation.XmlRootElement; - -@XmlRootElement -@XmlAccessorType( XmlAccessType.FIELD ) -public class Element -{ - @XmlAttribute - private Integer uid; - - @XmlAttribute - private String name; - - public Element() - { - } - - public Element( String name ) - { - this(); - this.name = name; - } - - public Element( Integer uid, String name ) - { - this.uid = uid; - this.name = name; - } - - public Integer getUid() - { - return uid; - } - - public void setUid( Integer uid ) - { - this.uid = uid; - } - - public String getName() - { - return name; - } - - public void setName( String name ) - { - this.name = name; - } -} === removed file 'dhis-2/dhis-web/dhis-web-api/src/main/java/org/hisp/dhis/api/resources/Elements.java' --- dhis-2/dhis-web/dhis-web-api/src/main/java/org/hisp/dhis/api/resources/Elements.java 2011-11-03 16:43:51 +0000 +++ dhis-2/dhis-web/dhis-web-api/src/main/java/org/hisp/dhis/api/resources/Elements.java 1970-01-01 00:00:00 +0000 @@ -1,40 +0,0 @@ -package org.hisp.dhis.api.resources; - -import java.util.ArrayList; -import java.util.List; - -import javax.xml.bind.annotation.XmlAccessType; -import javax.xml.bind.annotation.XmlAccessorType; -import javax.xml.bind.annotation.XmlElement; -import javax.xml.bind.annotation.XmlRootElement; - -@XmlRootElement -@XmlAccessorType( XmlAccessType.FIELD ) -public class Elements -{ - @XmlElement( name = "element" ) - private List elements = new ArrayList(); - - public List getElements() - { - return elements; - } - - public void setElements( List elements ) - { - this.elements = elements; - } - - public Element getElement( Integer uid ) - { - for ( Element el : elements ) - { - if ( el.getUid().equals( uid ) ) - { - return el; - } - } - - return null; - } -} === added file 'dhis-2/dhis-web/dhis-web-api/src/main/java/org/hisp/dhis/api/resources/XChart.java' --- dhis-2/dhis-web/dhis-web-api/src/main/java/org/hisp/dhis/api/resources/XChart.java 1970-01-01 00:00:00 +0000 +++ dhis-2/dhis-web/dhis-web-api/src/main/java/org/hisp/dhis/api/resources/XChart.java 2011-11-12 15:46:34 +0000 @@ -0,0 +1,23 @@ +package org.hisp.dhis.api.resources; + +import javax.xml.bind.annotation.XmlRootElement; + +import org.hisp.dhis.chart.Chart; + +@XmlRootElement( name = "chart" ) +public class XChart + extends XIdentifiableObject +{ + private Chart chart; + + public XChart() + { + + } + + public XChart( Chart chart ) + { + super( chart ); + this.chart = chart; + } +} === added file 'dhis-2/dhis-web/dhis-web-api/src/main/java/org/hisp/dhis/api/resources/XCharts.java' --- dhis-2/dhis-web/dhis-web-api/src/main/java/org/hisp/dhis/api/resources/XCharts.java 1970-01-01 00:00:00 +0000 +++ dhis-2/dhis-web/dhis-web-api/src/main/java/org/hisp/dhis/api/resources/XCharts.java 2011-11-12 15:46:34 +0000 @@ -0,0 +1,24 @@ +package org.hisp.dhis.api.resources; + +import java.util.ArrayList; +import java.util.List; + +import javax.xml.bind.annotation.XmlElement; +import javax.xml.bind.annotation.XmlRootElement; + +@XmlRootElement( name = "charts" ) +public class XCharts +{ + private List charts = new ArrayList(); + + public XCharts() + { + + } + + @XmlElement( name = "chart" ) + public List getCharts() + { + return charts; + } +} === added file 'dhis-2/dhis-web/dhis-web-api/src/main/java/org/hisp/dhis/api/resources/XIdentifiableObject.java' --- dhis-2/dhis-web/dhis-web-api/src/main/java/org/hisp/dhis/api/resources/XIdentifiableObject.java 1970-01-01 00:00:00 +0000 +++ dhis-2/dhis-web/dhis-web-api/src/main/java/org/hisp/dhis/api/resources/XIdentifiableObject.java 2011-11-12 15:46:34 +0000 @@ -0,0 +1,73 @@ +package org.hisp.dhis.api.resources; + +import org.hisp.dhis.common.IdentifiableObject; + +import javax.xml.bind.annotation.XmlAttribute; +import javax.xml.bind.annotation.XmlElement; +import javax.xml.bind.annotation.XmlRootElement; +import java.util.Date; + +@XmlRootElement +public class XIdentifiableObject +{ + private IdentifiableObject identifiableObject; + + private String href; + + public XIdentifiableObject() + { + + } + + public XIdentifiableObject( IdentifiableObject identifiableObject ) + { + this.identifiableObject = identifiableObject; + } + + @XmlAttribute + public int getId() + { + return identifiableObject.getId(); + } + + @XmlAttribute + public String getUuid() + { + return identifiableObject.getUuid(); + } + + @XmlAttribute + public String getUid() + { + return identifiableObject.getUid(); + } + + @XmlAttribute + public String getName() + { + return identifiableObject.getName(); + } + + @XmlAttribute + public String getCode() + { + return identifiableObject.getCode(); + } + + @XmlAttribute + public Date getLastUpdated() + { + return identifiableObject.getLastUpdated(); + } + + @XmlAttribute + public String getHref() + { + return href; + } + + public void setHref( String href ) + { + this.href = href; + } +} === added file 'dhis-2/dhis-web/dhis-web-api/src/main/java/org/hisp/dhis/api/resources/XNameableObject.java' --- dhis-2/dhis-web/dhis-web-api/src/main/java/org/hisp/dhis/api/resources/XNameableObject.java 1970-01-01 00:00:00 +0000 +++ dhis-2/dhis-web/dhis-web-api/src/main/java/org/hisp/dhis/api/resources/XNameableObject.java 2011-11-12 15:46:34 +0000 @@ -0,0 +1,43 @@ +package org.hisp.dhis.api.resources; + +import javax.xml.bind.annotation.XmlElement; +import javax.xml.bind.annotation.XmlRootElement; + +import org.hisp.dhis.common.NameableObject; + +@XmlRootElement +public class XNameableObject + extends XIdentifiableObject +{ + private NameableObject nameableObject; + + public XNameableObject() + { + + } + + public XNameableObject( NameableObject nameableObject ) + { + super( nameableObject ); + this.nameableObject = nameableObject; + } + + @XmlElement + public String getAlternativeName() + { + return nameableObject.getAlternativeName(); + } + + @XmlElement + public String getShortName() + { + return nameableObject.getShortName(); + } + + @XmlElement + public String getDescription() + { + return nameableObject.getDescription(); + } + +} === added directory 'dhis-2/dhis-web/dhis-web-api/src/main/java/org/hisp/dhis/api/view' === added file 'dhis-2/dhis-web/dhis-web-api/src/main/java/org/hisp/dhis/api/view/ExtendedMappingJacksonView.java' --- dhis-2/dhis-web/dhis-web-api/src/main/java/org/hisp/dhis/api/view/ExtendedMappingJacksonView.java 1970-01-01 00:00:00 +0000 +++ dhis-2/dhis-web/dhis-web-api/src/main/java/org/hisp/dhis/api/view/ExtendedMappingJacksonView.java 2011-11-12 15:46:34 +0000 @@ -0,0 +1,97 @@ +package org.hisp.dhis.api.view; + +import org.codehaus.jackson.JsonEncoding; +import org.codehaus.jackson.JsonFactory; +import org.codehaus.jackson.JsonGenerator; +import org.codehaus.jackson.map.ObjectMapper; +import org.codehaus.jackson.map.util.JSONPObject; +import org.springframework.web.servlet.view.json.MappingJacksonJsonView; + +import javax.servlet.http.HttpServletRequest; +import javax.servlet.http.HttpServletResponse; +import java.util.Map; + +/** + * @author mortenoh + */ +public class ExtendedMappingJacksonView extends MappingJacksonJsonView +{ + + private boolean includeRootElement = false; + + private boolean withPadding = false; + + private String callbackParameter = "callback"; + + private String paddingFunction = "callback"; + + public ExtendedMappingJacksonView() + { + + } + + public ExtendedMappingJacksonView( boolean withPadding ) + { + this.withPadding = withPadding; + + if ( withPadding ) + { + setContentType( "application/javascript" ); + } + } + + public void setIncludeRootElement( boolean includeRootElement ) + { + this.includeRootElement = includeRootElement; + } + + public void setWithPadding( boolean withPadding ) + { + this.withPadding = withPadding; + } + + public void setCallbackParameter( String callbackParameter ) + { + this.callbackParameter = callbackParameter; + } + + public void setPaddingFunction( String paddingFunction ) + { + this.paddingFunction = paddingFunction; + } + + @Override + protected void renderMergedOutputModel( Map model, HttpServletRequest request, HttpServletResponse response ) throws Exception + { + Object value = filterModel( model ); + ObjectMapper objectMapper = new ObjectMapper(); + JsonFactory jf = objectMapper.getJsonFactory(); + JsonGenerator jg = jf.createJsonGenerator( response.getOutputStream(), JsonEncoding.UTF8 ); + + if ( !includeRootElement && value instanceof Map ) + { + Map map = ( Map ) value; + + if ( map.size() == 1 ) + { + value = map.values().toArray()[0]; + } + } + + if ( withPadding ) + { + String callback = request.getParameter( callbackParameter ); + + if ( callback == null || callback.length() == 0 ) + { + callback = paddingFunction; + } + + JSONPObject valueWithPadding = new JSONPObject( callback, value ); + jg.writeObject( valueWithPadding ); + } else + { + jg.writeObject( value ); + } + } +} === added file 'dhis-2/dhis-web/dhis-web-api/src/main/java/org/hisp/dhis/api/view/Jaxb2View.java' --- dhis-2/dhis-web/dhis-web-api/src/main/java/org/hisp/dhis/api/view/Jaxb2View.java 1970-01-01 00:00:00 +0000 +++ dhis-2/dhis-web/dhis-web-api/src/main/java/org/hisp/dhis/api/view/Jaxb2View.java 2011-11-12 15:46:34 +0000 @@ -0,0 +1,69 @@ +package org.hisp.dhis.api.view; + +import org.springframework.validation.BindingResult; +import org.springframework.web.servlet.view.AbstractView; + +import javax.servlet.http.HttpServletRequest; +import javax.servlet.http.HttpServletResponse; +import javax.xml.bind.JAXBContext; +import javax.xml.bind.Marshaller; +import java.io.OutputStream; +import java.util.HashMap; +import java.util.Map; + +/** + * @author mortenoh + */ +public class Jaxb2View extends AbstractView { + public static final String DEFAULT_CONTENT_TYPE = "application/xml"; + + public Jaxb2View() { + setContentType(DEFAULT_CONTENT_TYPE); + } + + @Override + protected void renderMergedOutputModel(Map model, HttpServletRequest request, HttpServletResponse response) throws Exception { + response.setContentType(getContentType()); + + Object value = filterModel(model); + + if (value instanceof Map) { + Map map = (Map) value; + + if (map.size() == 1) { + value = map.values().toArray()[0]; + } + } + + OutputStream outputStream = response.getOutputStream(); + JAXBContext context = JAXBContext.newInstance(value.getClass()); + Marshaller marshaller = context.createMarshaller(); + marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE); + marshaller.setProperty(Marshaller.JAXB_ENCODING, "UTF-8"); + +// Marshaller.Listener listener = new IdentifiableObjectListener(request); +// marshaller.setListener(listener); + + /* TODO This will only work on JAXB RI (and crash on others!) please fixme.. */ + + /* + marshaller.setProperty("com.sun.xml.internal.bind.xmlHeaders", + "\n\n"); + */ + + marshaller.marshal(value, outputStream); + } + + protected Object filterModel(Map model) { + Map result = new HashMap(model.size()); + + for (Map.Entry entry : model.entrySet()) { + if (!(entry.getValue() instanceof BindingResult)) { + result.put(entry.getKey(), entry.getValue()); + } + } + + return result; + } + +} === modified file 'dhis-2/dhis-web/dhis-web-api/src/main/resources/META-INF/dhis/servlet.xml' --- dhis-2/dhis-web/dhis-web-api/src/main/resources/META-INF/dhis/servlet.xml 2011-11-03 16:43:51 +0000 +++ dhis-2/dhis-web/dhis-web-api/src/main/resources/META-INF/dhis/servlet.xml 2011-11-12 15:46:34 +0000 @@ -1,61 +1,42 @@ - - - - - - org.hisp.dhis.api.resources.Elements - org.hisp.dhis.api.resources.Element - - - - + - + + - - - - + + + + + + - - - - - - - - - - - - - - - - - - - + + + + + === removed directory 'dhis-2/dhis-web/dhis-web-api/src/main/webapp/WEB-INF/views' === removed file 'dhis-2/dhis-web/dhis-web-api/src/main/webapp/WEB-INF/views/element.vm' --- dhis-2/dhis-web/dhis-web-api/src/main/webapp/WEB-INF/views/element.vm 2011-11-03 16:43:51 +0000 +++ dhis-2/dhis-web/dhis-web-api/src/main/webapp/WEB-INF/views/element.vm 1970-01-01 00:00:00 +0000 @@ -1,6 +0,0 @@ - -All elements - -

-$element.name -

=== removed file 'dhis-2/dhis-web/dhis-web-api/src/main/webapp/WEB-INF/views/elements.vm' --- dhis-2/dhis-web/dhis-web-api/src/main/webapp/WEB-INF/views/elements.vm 2011-11-03 16:43:51 +0000 +++ dhis-2/dhis-web/dhis-web-api/src/main/webapp/WEB-INF/views/elements.vm 1970-01-01 00:00:00 +0000 @@ -1,10 +0,0 @@ - -

-Elements [JSON, XML] -

- - === modified file 'dhis-2/pom.xml' --- dhis-2/pom.xml 2011-11-10 23:04:32 +0000 +++ dhis-2/pom.xml 2011-11-12 15:46:34 +0000 @@ -1,5 +1,5 @@ + xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd"> 4.0.0 org.hisp.dhis dhis @@ -753,7 +753,13 @@ org.codehaus.jackson jackson-mapper-asl - 1.6.4 + 1.7.1 + + + + org.codehaus.jackson + jackson-xc + 1.7.1