package org.intermine.api.template;

import org.intermine.api.search.WebSearchable;
import org.intermine.pathquery.PathQuery;
import org.intermine.model.userprofile.SavedTemplateQuery;
import org.intermine.template.TemplateQuery;

/**
 * This class extends TemplateQuery to provide the features needed by
 * the API - Lucene indexing and linking to Saved-Queries.
 * @author Alex Kalderimis
 *
 */
public class ApiTemplate extends TemplateQuery implements WebSearchable {

    /** SavedTemplateQuery object in the UserProfile database, so we can update summaries. */
    protected SavedTemplateQuery savedTemplateQuery = null;

    public ApiTemplate(String name, String title, String comment,
            PathQuery query) {
        super(name, title, comment, query);
    }

    public ApiTemplate(TemplateQuery template) {
        super(template);
    }

    /**
     * Clone this ApiQuery
     */
    @Override
    public synchronized ApiTemplate clone() {
        return new ApiTemplate(this);
    }

    /**
     * Sets the saved template query object.
     *
     * @param savedTemplateQuery the database object
     */
    public void setSavedTemplateQuery(SavedTemplateQuery savedTemplateQuery) {
        this.savedTemplateQuery = savedTemplateQuery;
    }

    /**
     * Gets the saved template query object.
     *
     * @return a SavedTemplateQuery object that represents this TemplateQuery in the userprofile
     * database
     */
    public SavedTemplateQuery getSavedTemplateQuery() {
        return savedTemplateQuery;
    }

    // ApiTemplates should compare with strict object equality, to avoid one user's templates clobbering
    // another's.
    @Override
    public boolean equals(Object other) {
        return this == other;
    }

}

@@ -30,16 +30,524 @@
 plate;%0A%0A
+%0A/*%0A * Copyright (C) 2002-2012 FlyMine%0A *%0A * This code may be freely distributed and modified under the%0A * terms of the GNU Lesser General Public Licence.  This should%0A * be distributed with the code.  See the LICENSE file for more%0A * information or http://www.gnu.org/copyleft/lesser.html.%0A *%0A */%0A%0Aimport java.util.ArrayList;%0Aimport java.util.Collection;%0Aimport java.util.HashSet;%0Aimport java.util.Set;%0A%0Aimport org.intermine.api.search.OriginatingEvent;%0Aimport org.intermine.api.search.PropertyChangeEvent;%0A
 import o
@@ -579,20 +579,23 @@
 ebSearch
-able
+Watcher
 ;%0Aimport
@@ -613,27 +613,71 @@
 ine.
-pathquery.PathQuery
+api.search.WebSearchable;%0Aimport org.intermine.api.tag.TagTypes
 ;%0Aim
@@ -733,16 +733,58 @@
 eQuery;%0A
+import org.intermine.pathquery.PathQuery;%0A
 import o
@@ -932,32 +932,30 @@
 and 
-linking to Saved-Queries
+database serialisation
 .%0A *
@@ -1360,24 +1360,227 @@
 ry);%0A    %7D%0A%0A
+    /** Construct a new API template that has all the same properties as the TemplateQuery%0A     * passed into the constructor.%0A     *%0A     * @param template The prototypical template to be like.%0A     */%0A
     public A
@@ -2399,16 +2399,23 @@
 quality,
+%0A    //
  to avoi
@@ -2447,23 +2447,16 @@
 obbering
-%0A    //
  another
@@ -2552,10 +2552,1236 @@
 %0A    %7D%0A%0A
+    // WebSearchable Implementation //%0A    private final Set%3CWebSearchWatcher%3E observers = new HashSet%3CWebSearchWatcher%3E();%0A%0A    @Override%0A    public void addObserver(WebSearchWatcher wsw) %7B%0A        observers.add(wsw);%0A    %7D%0A%0A    @Override%0A    public void removeObserver(WebSearchWatcher wsw) %7B%0A        observers.remove(wsw);%0A    %7D%0A%0A    @Override%0A    public String getTagType() %7B%0A        return TagTypes.TEMPLATE;%0A    %7D%0A%0A    @Override%0A    public void fireEvent(OriginatingEvent e) %7B%0A        if (observers != null) %7B // Due to the order of member initialisation.%0A            Collection%3CWebSearchWatcher%3E watchers = new ArrayList%3CWebSearchWatcher%3E(observers);%0A            for (WebSearchWatcher wsw: watchers) %7B%0A                wsw.receiveEvent(e);%0A            %7D%0A        %7D%0A    %7D%0A%0A    @Override%0A    public void setTitle(String title) %7B%0A        super.setTitle(title);%0A        fireEvent(new PropertyChangeEvent(this));%0A    %7D%0A%0A    @Override%0A    public void setName(String name) %7B%0A        super.setName(name);%0A        fireEvent(new PropertyChangeEvent(this));%0A    %7D%0A%0A    @Override%0A    public void setDescription(String description) %7B%0A        super.setDescription(description);%0A        fireEvent(new PropertyChangeEvent(this));%0A    %7D%0A%0A
 %7D%0A

Support for events