package org.intermine.web;

/*
 * Copyright (C) 2002-2005 FlyMine
 *
 * This code may be freely distributed and modified under the
 * terms of the GNU Lesser General Public Licence.  This should
 * be distributed with the code.  See the LICENSE file for more
 * information or http://www.gnu.org/copyleft/lesser.html.
 *
 */

import java.io.PrintStream;
import java.util.Map;
import javax.servlet.ServletContext;
import javax.servlet.http.HttpSession;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

import org.intermine.util.XmlUtil;

import org.apache.commons.lang.StringUtils;

/**
 * Exports templates to XML.
 *
 * @author Thomas Riley
 */
public class TemplatesExportAction extends InterMineAction
{
    /**
     * @see InterMineAction#execute(ActionMapping, ActionForm, HttpServletRequest,
     *  HttpServletResponse)
     */
    public ActionForward execute(ActionMapping mapping,
                                 ActionForm form,
                                 HttpServletRequest request,
                                 HttpServletResponse response)
        throws Exception {
        HttpSession session = request.getSession();
        ServletContext servletContext = session.getServletContext();
        Profile profile = (Profile) session.getAttribute(Constants.PROFILE);
        String type = request.getParameter("type");
        Map templates = null;
        
        response.setContentType("text/plain");
        response.setHeader("Content-Disposition ", "inline; filename=template-queries.xml");
        
        if (type == null || type.equals("user")) {
            templates = profile.getSavedTemplates();
        } else if (type.equals("global")) {
            templates = SessionMethods.getSuperUserProfile(servletContext).getSavedTemplates();
        } else {
            return null;
        }
        
        String name = request.getParameter("name");
        
        PrintStream out = new PrintStream(response.getOutputStream());
        String xml = null;
        
        if (StringUtils.isNotEmpty(name)) {
            xml = ((TemplateQuery) templates.get(name)).toXml();
        } else {
            xml = TemplateHelper.templateMapToXml(templates);
        }
        xml = XmlUtil.indentXmlSimple(xml);
        out.print(xml);
        out.flush();
        return null;
    }
}

@@ -1482,19 +1482,19 @@
  String 
-typ
+nam
 e = requ
@@ -1511,19 +1511,19 @@
 ameter(%22
-typ
+nam
 e%22);%0A   
@@ -1531,188 +1531,121 @@
     
-Map templates = null;%0A        %0A        response.setContentType(%22text/plain%22);%0A        response.setHeader(%22Content-Disposition %22, %22inline; filename=template-queries.xml%22);%0A    
+String type = request.getParameter(%22type%22);%0A        %0A        String xml = null;%0A%0A        if (name == null) %7B%0A
     
-%0A
     
@@ -1703,28 +1703,58 @@
         
-templates = 
+    xml = TemplateHelper.templateMapToXml(
 profile.
@@ -1764,32 +1764,33 @@
 SavedTemplates()
+)
 ;%0A        %7D else
@@ -1779,24 +1779,28 @@
 ));%0A        
+   
+ 
 %7D else if (t
@@ -1839,34 +1839,89 @@
     
-templates = SessionMethods
+    xml = TemplateHelper.templateMapToXml(SessionMethods%0A                        
 .get
@@ -1972,18 +1972,23 @@
 plates()
+)
 ;%0A
+    
         
@@ -2012,29 +2012,107 @@
     
-return null;%0A
+    throw new IllegalArgumentException(%22Cannot export all templates for type %22 + type);%0A   
         
 %7D%0A  
@@ -2099,32 +2099,33 @@
 pe);%0A           
+ 
 %7D%0A        %0A     
@@ -2110,32 +2110,40 @@
       %7D%0A        
+%7D else %7B
 %0A        String 
@@ -2139,51 +2139,82 @@
     
-String name = request.getParameter(%22name%22);
+    TemplateQuery t = TemplateHelper.findTemplate(servletContext, session,
 %0A   
@@ -2218,25 +2218,24 @@
         
-%0A
         
 PrintStr
@@ -2230,97 +2230,76 @@
     
-PrintStream out = new PrintStream(response.getOutputStre
+    profile.getUsern
 am
+e
 ()
-);%0A        String xml 
+, name, type);%0A            if (t !
 = null
-;
+) %7B
 %0A   
@@ -2303,25 +2303,24 @@
         
-%0A
         
 if (Stri
@@ -2315,106 +2315,175 @@
     
-if (StringUtils.isNotEmpty(name)) %7B%0A            xml = ((TemplateQuery) templates.get(name)).toXml(
+xml = t.toXml();%0A            %7D else %7B%0A                throw new IllegalArgumentException(%22Cannot find template %22 + name + %22 in context %22%0A                        + type
 );%0A 
@@ -2485,37 +2485,40 @@
 e);%0A        
-%7D else %7B%0A
+    %7D%0A    
     
+%7D%0A
         xml 
@@ -2523,49 +2523,184 @@
 l = 
-TemplateHelper.templateMapToXml(templates
+XmlUtil.indentXmlSimple(xml);%0A        %0A        response.setContentType(%22text/plain%22);%0A        response.setHeader(%22Content-Disposition %22, %22inline; filename=template-queries.xml%22
 );%0A 
@@ -2698,33 +2698,32 @@
 .xml%22);%0A        
-%7D
 %0A        xml = X
@@ -2719,41 +2719,68 @@
     
-xml = XmlUtil.indentXmlSimple(xml
+PrintStream out = new PrintStream(response.getOutputStream()
 );%0A 

Fix for template export from favourites bug