Subversion Repositories XServices

Rev

Rev 52 | View as "text/plain" | Blame | Compare with Previous | Last modification | View Log | Download | RSS feed

/*
 *   Mylyn Connector for Serena Business Mashups
 *       Copyright 2010 Brian Rosenberger (Brutex Network)
 *
 *   Licensed under the Apache License, Version 2.0 (the "License");
 *   you may not use this file except in compliance with the License.
 *   You may obtain a copy of the License at
 *
 *       http://www.apache.org/licenses/LICENSE-2.0
 *
 *   Unless required by applicable law or agreed to in writing, software
 *   distributed under the License is distributed on an "AS IS" BASIS,
 *   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *   See the License for the specific language governing permissions and
 *   limitations under the License.
 * 
 *   Serena, TeamTrack and Serena Business Mashup are 
 *       registered trademarks of SERENA Software Inc.
 */
package net.brutex.mylyn.sbmconnector.core;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import net.brutex.mylyn.sbmconnector.SBMConnectorPlugin;
import net.brutex.mylyn.sbmconnector.core.model.SBMStaticFields;
import net.brutex.mylyn.sbmconnector.ui.SBMQueryComposite;
import net.brutex.mylyn.sbmconnector.ui.SBMRepositoryQueryPage;
import net.brutex.mylyn.sbmconnector.ui.SBMRepositorySettingsPage;
import net.brutex.sbm.wsclient.TTItem;

import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.core.runtime.IStatus;
import org.eclipse.core.runtime.Status;
import org.eclipse.mylyn.tasks.core.AbstractRepositoryConnector;
import org.eclipse.mylyn.tasks.core.IRepositoryQuery;
import org.eclipse.mylyn.tasks.core.ITask;
import org.eclipse.mylyn.tasks.core.TaskRepository;
import org.eclipse.mylyn.tasks.core.data.AbstractTaskDataHandler;
import org.eclipse.mylyn.tasks.core.data.TaskAttribute;
import org.eclipse.mylyn.tasks.core.data.TaskData;
import org.eclipse.mylyn.tasks.core.data.TaskDataCollector;
import org.eclipse.mylyn.tasks.core.data.TaskMapper;
import org.eclipse.mylyn.tasks.core.sync.ISynchronizationSession;

public class SBMRepositoryConnector extends AbstractRepositoryConnector {

        private SBMTicketDataHandler datahandler;
        private static Map<TaskRepository, SBMClient> clients = new HashMap<TaskRepository, SBMClient>();

        public SBMRepositoryConnector() {
                this.datahandler = new SBMTicketDataHandler(this);
        }

        public static synchronized SBMClient getClient(TaskRepository repository) {
                SBMClient client = clients.get(repository);
                if (client == null) {
                        client = new SBMClient(repository);
                        clients.put(repository, client);
                }
                return client;
        }

        @Override
        public boolean canCreateNewTask(TaskRepository repository) {
                return false;
        }

        @Override
        public boolean canCreateTaskFromKey(TaskRepository repository) {
                return false;
        }

        @Override
        public boolean canSynchronizeTask(TaskRepository taskRepository, ITask task) {
                return true;
        }

        @Override
        public String getConnectorKind() {
                return SBMConnectorPlugin.CONNECTOR_KIND;
        }

        @Override
        public String getLabel() {
                return SBMConnectorPlugin.LABEL;
        }

        @Override
        public String getRepositoryUrlFromTaskUrl(String taskFullUrl) {
                // TODO Auto-generated method stub
                return null;
        }

        @Override
        public TaskData getTaskData(TaskRepository taskRepository, String taskId,
                        IProgressMonitor monitor) throws CoreException {
                monitor.beginTask("Loading SBM Ticket", IProgressMonitor.UNKNOWN);
                TaskData data = null;
                try {
                        SBMClient client = getClient(taskRepository);
                        TTItem item = client.getTTItem(taskId);
                        data = datahandler.convert(taskRepository, item, monitor, null);

                } finally {
                        monitor.done();
                }
                return data;
        }

        @Override
        public String getTaskIdFromTaskUrl(String taskFullUrl) {
                // TODO Auto-generated method stub
                return null;
        }

        @Override
        public String getTaskUrl(String repositoryUrl, String taskId) {
                // TODO Auto-generated method stub
                return "http://something/I/havent/implemented/yet?"+taskId;
        }

        @Override
        public boolean hasTaskChanged(TaskRepository taskRepository, ITask task,
                        TaskData taskData) {
                TaskAttribute attribute = taskData.getRoot().getAttribute(
                                SBMStaticFields.LASTMODIFIEDDATE.getValue());
                if (attribute != null) {
                        Date dataModificationDate = taskData.getAttributeMapper()
                                        .getDateValue(attribute);
                        if (dataModificationDate != null) {
                                Date taskModificationDate = task.getModificationDate();
                                if (taskModificationDate != null) {
                                        return !taskModificationDate.equals(dataModificationDate);
                                }
                        }
                }
                return true;
        }

        @Override
        public IStatus performQuery(TaskRepository repository,
                        IRepositoryQuery query, TaskDataCollector collector,
                        ISynchronizationSession session, IProgressMonitor monitor) {

                SBMClient client = new SBMClient(repository);
                // do we use a report or a query?
                if (query.getAttribute(SBMRepositoryQueryPage.SQL_QUERY_NAME).equals(
                                query.getAttribute(SBMRepositoryQueryPage.REPORT_OR_QUERY))) {
                        monitor.subTask("Calculating query result size ...");
                        try {

                                List<String> idlist = client.getResultCount(
                                                getQueryTable(repository),
                                                query.getAttribute("sql_where"));
                                int countItem = idlist.size();
                                monitor.beginTask("", countItem);
                                monitor.subTask("Loading " + countItem + " items ...");

                                // List<TTItem> list = client.getTTItemsByTable(
                                // getQueryTable(repository), query.getAttribute("sql_where"),
                                // true);
                                monitor.worked(1);
                                int i = 1;
                                for (String id : idlist) {
                                        if (id != null) {
                                                monitor.subTask("Loading item " + i + "/" + countItem
                                                                + " ...");
                                                TTItem ttitem = client.getTTItem("[" + id + "]");
                                                TaskData data = datahandler.convert(repository, ttitem,
                                                                monitor, query);
                                                collector.accept(data);
                                        }
                                        monitor.worked(1);
                                        if (monitor.isCanceled())
                                                return Status.CANCEL_STATUS;
                                        i++;
                                }
                        } catch (CoreException e) {
                                IStatus status = new Status(IStatus.ERROR,
                                                SBMConnectorPlugin.PLUGIN_ID, e.getMessage(), e);
                                // StatusManager.getManager().handle(status,
                                // StatusManager.BLOCK);
                                return status;
                        } finally {
                                monitor.done();
                        }
                } else {
                        // use report
                        monitor.beginTask("Loading Report", 0);
                        try {
                                List<TTItem> list = client.getTTItemsByReport(query
                                                .getAttribute(SBMRepositoryQueryPage.REPORT));
                                for(TTItem i:list) {
                                        TaskData data = datahandler.convert(repository, i,
                                                        monitor, query);
                                        collector.accept(data);
                                }
                        } catch (CoreException e) {
                                IStatus status = new Status(IStatus.ERROR,
                                                SBMConnectorPlugin.PLUGIN_ID, e.getMessage(), e);
                                // StatusManager.getManager().handle(status,
                                // StatusManager.BLOCK);
                                return status;
                        } finally {
                                monitor.done();
                        }
                }

                return Status.OK_STATUS;
        }

        @Override
        public void updateRepositoryConfiguration(TaskRepository taskRepository,
                        IProgressMonitor monitor) throws CoreException {
                // TODO Auto-generated method stub

        }

        @Override
        public void updateTaskFromTaskData(TaskRepository taskRepository,
                        ITask task, TaskData taskData) {
                getTaskMapping(taskData).applyTo(task);
        }

        @Override
        public TaskMapper getTaskMapping(TaskData taskData) {
                return new TaskMapper(taskData);
        }

        @Override
        public AbstractTaskDataHandler getTaskDataHandler() {
                return datahandler;
        }

        public boolean canDeleteTask(TaskRepository repository, ITask task) {
                return false;
        }

        @Override
        public boolean hasRepositoryDueDate(TaskRepository taskRepository,
                        ITask task, TaskData taskData) {
                // TODO Auto-generated method stub
                return false;
        }

        private String getQueryTable(TaskRepository repository) {
                return repository
                                .getProperty(SBMRepositorySettingsPage.STRING_SOLUTIONTABLE);
        }

        
}