package org.intermine.web;

/*
 * Copyright (C) 2002-2004 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 javax.servlet.ServletContext;
import javax.servlet.http.HttpSession;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionMessages;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.ActionError;

import org.intermine.objectstore.ObjectStore;

/**
 * Action to create a new TemplateQuery from current query.
 *
 * @author Thomas Riley
 */
public class BuildTemplateAction extends Action
{
    /**
     * Handle submission of the build template form. Build a UserTemplateQuery.
     *
     * @param mapping The ActionMapping used to select this instance
     * @param form The optional ActionForm bean for this request (if any)
     * @param request The HTTP request we are processing
     * @param response The HTTP response we are creating
     * @return an ActionForward object defining where control goes next
     *
     * @exception Exception if the application business logic throws
     *  an exception
     */
    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);
        ObjectStore os = (ObjectStore) servletContext.getAttribute(Constants.OBJECTSTORE);
        PathQuery query = (PathQuery) session.getAttribute(Constants.TEMPLATE_PATHQUERY);
        BuildTemplateForm tf = (BuildTemplateForm) form;
        
        TemplateQuery template = TemplateHelper.buildTemplateQuery(tf, query);
        
        if (request.getParameter("preview") != null) {
            request.setAttribute("previewTemplate", template);
            request.setAttribute("showPreview", Boolean.TRUE);
            return mapping.findForward("templateBuilder");
        } else {
            // Create new user template
            session.removeAttribute(Constants.TEMPLATE_PATHQUERY);
            ActionMessages actionMessages = new ActionMessages();
            actionMessages.add(ActionMessages.GLOBAL_MESSAGE,
                             new ActionError("templateBuilder.templateCreated", tf.getShortName()));
            saveMessages(request, actionMessages);
            tf.reset();
            profile.saveTemplate(template.getName(), template);
            // If superuser then rebuild shared templates
            if (profile.getUsername() != null
                && profile.getUsername().equals
                    (servletContext.getAttribute(Constants.SUPERUSER_ACCOUNT))) {
                InitialiserPlugin.loadGlobalTemplateQueries(servletContext);
            }
            return mapping.findForward("finish");
        }
    }
}

@@ -2175,16 +2175,114 @@
 ) form;%0A
+        TemplateQuery editing = (TemplateQuery) session.getAttribute(Constants.EDITING_TEMPLATE);%0A
         
@@ -2274,32 +2274,32 @@
 LATE);%0A        %0A
-
         Template
@@ -2750,123 +2750,139 @@
     
-ActionMessages actionMessages = new ActionMessages();%0A            actionMessages.add(ActionMessages.GLOBAL_MESSAGE,
+session.removeAttribute(Constants.EDITING_TEMPLATE);%0A            String key = (editing == null) ? %22templateBuilder.templateCreated%22
 %0A   
@@ -2911,24 +2911,24 @@
     
-new ActionError(
+              : 
 %22tem
@@ -2952,36 +2952,100 @@
 late
-Cre
+Upd
 ated%22
-, tf.getShor
+;%0A            String msg = getResources(request).getMessage(key, template.ge
 tName())
 );%0A 
@@ -3040,17 +3040,16 @@
 tName())
-)
 ;%0A      
@@ -3059,69 +3059,226 @@
    s
-aveMessages(request, actionMessages);%0A            tf.reset();
+ession.setAttribute(Constants.MESSAGE, msg);%0A            tf.reset();%0A            // Replace template if needed%0A            if (editing != null) %7B%0A                profile.deleteTemplate(editing.getName());%0A            %7D
 %0A   

support for updating (replacing) an existing template. now displays success msg after redirecting to begin.do