package org.intermine.task;

/*
 * 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.FileReader;
import java.io.Reader;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;

import org.apache.log4j.Logger;
import org.apache.tools.ant.BuildException;
import org.apache.tools.ant.Task;
import org.intermine.objectstore.ObjectStore;
import org.intermine.objectstore.ObjectStoreFactory;
import org.intermine.web.Profile;
import org.intermine.web.ProfileManager;
import org.intermine.web.RequestPasswordAction;
import org.intermine.web.TemplateQuery;
import org.intermine.web.TemplateQueryBinding;

/**
 * Load template queries form an XML file into a given user profile.
 * 
 * @author Thomas Riley
 */

public class LoadDefaultTemplatesTask extends Task
{
    private static final Logger LOG = Logger.getLogger(LoadDefaultTemplatesTask.class);
    
    protected String xmlFile;
    protected String username;
    protected String alias;
    
    /**
     * Set the templates xml file.
     * @param file to xml file
     */
    public void setTemplatesXml(String file) {
        xmlFile = file;
    }
    
    /**
     * Set the account name to laod template to.
     * @param user username to load templates into
     */
    public void setUsername(String user) {
        username = user;
    }

    /**
     * Load templates from an xml file into a userprofile account.
     * 
     * @see Task#execute
     */
    public void execute() throws BuildException {
        // Needed so that STAX can find it's implementation classes
        Thread.currentThread().setContextClassLoader(getClass().getClassLoader());
        
        try {
            ObjectStore os = ObjectStoreFactory.getObjectStore();
            ProfileManager pm = new ProfileManager(os);
            Reader templateQueriesReader = new FileReader(xmlFile);
            Map templateQueries = new TemplateQueryBinding().unmarshal(templateQueriesReader);
            Profile profile = null;
            if (!pm.hasProfile(username)) {
                LOG.info("Creating profile for " + username);
                profile = new Profile(pm, username, new HashMap(), new HashMap(), new HashMap());
                String password = RequestPasswordAction.generatePassword();
                pm.saveProfile(profile);
                pm.setPassword(username, password);
            } else {
                LOG.warn("Profile for " + username + ", clearing template queries");
                profile = pm.getProfile(username, pm.getPassword(username));
                Map tmpls = new HashMap(profile.getSavedTemplates());
                Iterator iter = tmpls.keySet().iterator();
                while (iter.hasNext()) {
                    profile.deleteTemplate((String) iter.next());
                }
            }
            if (profile.getSavedTemplates().size() == 0) {
                Iterator iter = templateQueries.values().iterator();
                while (iter.hasNext()) {
                    TemplateQuery template = (TemplateQuery) iter.next();
                    String append = "";
                    if (!template.isValid()) {
                        append = " [invalid]";
                    }
                    log("Adding template \"" + template.getName() + "\"" + append);
                    profile.saveTemplate(template.getName(), template);
                }
            }
        } catch (Exception e) {
            throw new BuildException(e);
        }
    }
}

@@ -1801,16 +1801,98 @@
 classes%0A
+        ClassLoader cl = Thread.currentThread().getContextClassLoader();%0A        %0A
         
@@ -3790,24 +3790,106 @@
 ception(e);%0A
+        %7D finally %7B%0A            Thread.currentThread().setContextClassLoader(cl);%0A
         %7D%0A  

Set the ContextClassLoader back to its original value at the end of execute() to prevent ClassLoader problems later.