Subversion Repositories XServices

Rev

Rev 30 | Rev 39 | Go to most recent revision | 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.List;
import java.util.Set;

import net.brutex.mylyn.sbmconnector.SBMConnectorPlugin;
import net.brutex.mylyn.sbmconnector.core.model.SBMField;
import net.brutex.mylyn.sbmconnector.core.model.SBMFieldAttributes;
import net.brutex.mylyn.sbmconnector.core.model.SBMFieldTypes;
import net.brutex.mylyn.sbmconnector.core.model.SBMFieldValue;
import net.brutex.mylyn.sbmconnector.core.model.SBMNote;
import net.brutex.mylyn.sbmconnector.core.model.SBMStaticFields;
import net.brutex.sbm.wsclient.TTItem;

import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.mylyn.tasks.core.ITaskMapping;
import org.eclipse.mylyn.tasks.core.RepositoryResponse;
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.TaskAttributeMapper;
import org.eclipse.mylyn.tasks.core.data.TaskCommentMapper;
import org.eclipse.mylyn.tasks.core.data.TaskData;

/**
 * The Class SBMTicketDataHandler.
 */
public class SBMTicketDataHandler extends AbstractTaskDataHandler {

        /** The connector. */
        private final SBMRepositoryConnector connector;
        
        /**
         * Instantiates a new SBM ticket data handler.
         *
         * @param connector the connector
         */
        public SBMTicketDataHandler(SBMRepositoryConnector connector) {
                this.connector = connector;
        }
        
        /* (non-Javadoc)
         * @see org.eclipse.mylyn.tasks.core.data.AbstractTaskDataHandler#getAttributeMapper(org.eclipse.mylyn.tasks.core.TaskRepository)
         */
        @Override
        public TaskAttributeMapper getAttributeMapper(TaskRepository taskRepository) {
                return new SBMTicketAttributeMapper(taskRepository);
        }

        /* (non-Javadoc)
         * @see org.eclipse.mylyn.tasks.core.data.AbstractTaskDataHandler#initializeTaskData(org.eclipse.mylyn.tasks.core.TaskRepository, org.eclipse.mylyn.tasks.core.data.TaskData, org.eclipse.mylyn.tasks.core.ITaskMapping, org.eclipse.core.runtime.IProgressMonitor)
         */
        @Override
        public boolean initializeTaskData(TaskRepository repository, TaskData taskData,
                        ITaskMapping initializationData, IProgressMonitor monitor)
                        throws CoreException {
                
                /*TaskAttribute attribute = taskData.getRoot().createAttribute(SBMStaticFields.TITLE);
                attribute.getMetaData().setReadOnly(false).setType(TaskAttribute.TYPE_SHORT_TEXT).setLabel("Summary:");

                /*attribute = taskData.getRoot().createAttribute(SBMStaticFields.DESCRIPTION);
                attribute.getMetaData().setReadOnly(false).setType(TaskAttribute.TYPE_LONG_TEXT).setLabel("Description:");

                attribute = taskData.getRoot().createAttribute(SBMStaticFields.SUBMITDATE);
                attribute.getMetaData().setReadOnly(false).setType(TaskAttribute.TYPE_DATETIME).setLabel("Create Date:");


                  if (!taskData.isNew()) {
                        attribute = taskData.getRoot().createAttribute(TaskAttribute.TASK_URL);
                        attribute.getMetaData().setReadOnly(true).setKind(TaskAttribute.KIND_DEFAULT).setType(
                                        TaskAttribute.TYPE_URL).setLabel("Location:");
                        File file = client.getTask(taskData.getTaskId(), monitor);
                        try {
                                attribute.setValue(file.toURI().toURL().toString());
                        } catch (MalformedURLException e) {
                        }
                }

                attribute = taskData.getRoot().createAttribute(TaskAttribute.PRODUCT);
                attribute.getMetaData().setReadOnly(false).setKind(TaskAttribute.KIND_DEFAULT).setType(
                                TaskAttribute.TYPE_SINGLE_SELECT).setLabel("Project");
                                */
                taskData.setPartial(true);
                return true;
        }

        /* (non-Javadoc)
         * @see org.eclipse.mylyn.tasks.core.data.AbstractTaskDataHandler#postTaskData(org.eclipse.mylyn.tasks.core.TaskRepository, org.eclipse.mylyn.tasks.core.data.TaskData, java.util.Set, org.eclipse.core.runtime.IProgressMonitor)
         */
        @Override
        public RepositoryResponse postTaskData(TaskRepository repository,
                        TaskData taskData, Set<TaskAttribute> oldAttributes,
                        IProgressMonitor monitor) throws CoreException {
                // TODO Auto-generated method stub
                return null;
        }
        
        /**
         * Convert.
         *
         * @param repository the repository
         * @param ttitem the ttitem
         * @param monitor the monitor
         * @return the task data
         * @throws CoreException the core exception
         */
        public TaskData convert(TaskRepository repository, TTItem ttitem, IProgressMonitor monitor) 
                throws CoreException {
                SBMClient client = SBMRepositoryConnector.getClient(repository);
                TaskData data = new TaskData(new SBMTicketAttributeMapper(repository),
                                SBMConnectorPlugin.CONNECTOR_KIND, 
                                repository.getRepositoryUrl(), 
                                ttitem.getGenericItem().getValue().getItemID().getValue());
                initializeTaskData(repository, data, null, monitor);
                // Create Fields for all the generic SBM (system) fields
                for (SBMTicketAttribute f : SBMTicketAttribute.values()) {
                        TaskAttribute attr;
                        if(f.getTaskName()!=null) {
                                attr = data.getRoot().createMappedAttribute(f.getTaskName());
                        } else {
                                attr = data.getRoot().createAttribute(f.getSBMName());
                        }
                        try {
                                SBMStaticFields.valueOf(f.getSBMName()); //check if in static field list
                                attr.getMetaData().setReadOnly(true);
                        } catch (IllegalArgumentException e) {
                                attr.getMetaData().setReadOnly(false);  
                        }                       
                        attr.getMetaData().setType(f.getTaskType());
                        attr.getMetaData().setLabel(client.getFieldLabel(ttitem, f.getSBMName()));
                        if(f.isVisible()) {
                                attr.getMetaData().setKind(TaskAttribute.KIND_DEFAULT);
                        } else {
                                attr.getMetaData().setKind(null);
                        }
                        attr.setValue(client.getStaticFieldValue(ttitem, f.getSBMName()));
                }
                
                //Custom fields         
                List<SBMField> customfields = client.getFields(client.getTableName(ttitem));
                for(SBMField f : customfields) {
                        if(SBMTicketAttribute.getSBMAttributeBySBMName(f.getName())!=null) {
                                continue;
                        }
                        TaskAttribute custom = data.getRoot().createAttribute(f.getName());
                        custom.getMetaData().setType(
                                        new SBMTicketAttributeMapper(repository).mapToTaskKey(f.getType()));
                        custom.getMetaData().setLabel(f.getLabel());
                        custom.getMetaData().setKind(TaskAttribute.KIND_DEFAULT);
                        custom.getMetaData().setReadOnly(true);
                        if(f.getType()==SBMFieldTypes.SELECTION &&
                                        client.getFieldValue(ttitem, f.getName())!=null) {
                                String val = client.getFieldValue(ttitem, f.getName()).getValue();
                                custom.putOption(val, val);
                                custom.setValue(val);
                        } else if(f.getType()==SBMFieldTypes.MULTIPLE_SELECTION 
                                        || f.getType()==SBMFieldTypes.MULTIPLE_RELATIONAL
                                        || f.getType()==SBMFieldTypes.MULTIPLE_GROUP
                                        || f.getType()==SBMFieldTypes.MULTIPLE_USERGROUP) {
                                List<SBMFieldValue> values = client.getFieldValues(ttitem, f.getName());
                                for(SBMFieldValue val : values) {
                                        custom.putOption(val.getInternalValue(), val.getValue());
                                        custom.addValue(val.getValue());
                                }                               
                        } else if(client.getFieldValue(ttitem, f.getName())!=null) {
                                        custom.setValue(client.getFieldValue(ttitem, f.getName()).getValue());
                        }
                }
                
                //Notes
                for(SBMNote n : client.getNotes(ttitem)) {
                        TaskCommentMapper mapper = new TaskCommentMapper();
                        mapper.setCommentId(n.getId());
                        mapper.setNumber(Integer.valueOf(n.getId()));
                        mapper.setText(n.getText());
                        mapper.setCreationDate(n.getCreation());
                        
                        TaskAttribute a = data.getRoot().createAttribute("NOTE"+n.getId());
                        mapper.applyTo(a);
                }
                                
                data.setPartial(false);
                return data;
        }
}