Subversion Repositories XServices

Rev

Rev 34 | Rev 42 | 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.Date;
import java.util.GregorianCalendar;
import java.util.List;
import java.util.Set;

import javax.xml.datatype.DatatypeConfigurationException;
import javax.xml.datatype.DatatypeFactory;
import javax.xml.datatype.XMLGregorianCalendar;

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.mylyn.sbmconnector.core.model.SBMSystemFields;
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.mylyn.tasks.core.IRepositoryQuery;
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 {
                monitor.subTask("Initialising task ...");
                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, IRepositoryQuery query)
                        throws CoreException {
                SBMClient client = SBMRepositoryConnector.getClient(repository);
                TaskData data = new TaskData(new SBMTicketAttributeMapper(repository),
                                SBMConnectorPlugin.CONNECTOR_KIND, repository
                                                .getRepositoryUrl(), ttitem.getGenericItem().getValue()
                                                .getItemName().getValue()
                                                + " ["
                                                + ttitem.getGenericItem().getValue().getItemID()
                                                                .getValue() + "]");
                initializeTaskData(repository, data, null, monitor);
                String fieldlist = "";
                if (repository
                                .hasProperty(SBMRepositorySettingsPage.STRING_TABLEFIELDLIST)) {
                        fieldlist = repository
                                        .getProperty(SBMRepositorySettingsPage.STRING_TABLEFIELDLIST);
                }
                // Create Fields for all the generic SBM (system) fields
                monitor.subTask("Creating generic SBM fields...");
                for (SBMSystemFields sysfield : SBMSystemFields.values()) {
                        if(!sysfield.hasTaskAttribute()) {
                                // skip the field here, because we do not have a
                                // matching task attribute to map. The field
                                // will be processed in the custom field section
                                continue;
                        }
                        if (!fieldlist.contains("," + sysfield.getValue() + ",")) {
                                // do not process custom field when it has not
                                // been selected by user in repository settings page
                                continue;
                        }
                        // try to initialize the SBM attribute
                        SBMTicketAttribute f = null;
                        TaskAttribute attr = null;
                        try {
                                f = SBMTicketAttribute.valueOf(sysfield.getValue());
                        } catch (IllegalArgumentException e) {
                                // the field is a valid sbm system field and is set up to
                                // have an equivalent in the task attributes. It must have an
                                // entry in the SBMTicketAttribute class !
                                continue; // we just skip the field 
                        }
                        
                        if (f.getTaskName() != null) {
                                attr = data.getRoot().createMappedAttribute(f.getTaskName());
                        } else {
                                // again the field is set up to have an task attribute
                                // equivalent, but it is not set up in SBMAttributes correctly
                                continue;
                        }
                        
                        attr.getMetaData().setReadOnly(true); //we do not support writing yet 
                        attr.getMetaData().setType(f.getTaskType()); // set the mapped field type
                        attr.getMetaData().setLabel(client.getFieldLabel(ttitem, f.getSBMName())); // get and set the field label
                        if (f.isVisible()) {
                                // field is not hidden, display it by setting KIND_DEFAULT
                                attr.getMetaData().setKind(TaskAttribute.KIND_DEFAULT);
                        } else {
                                // field is hidden
                                attr.getMetaData().setKind(null);
                        }
                        
                        attr.setValue(client.getStaticFieldValue(ttitem, f.getSBMName())); // get and set the field value
                }
                monitor.worked(1);
                
                //--------------
                // Custom fields
                //--------------
                List<SBMField> customfields = client.getFields(client
                                .getTableName(ttitem));
                for (SBMField f : customfields) {
                        if (!fieldlist.contains("," + f.getName() + ",")) {
                                // do not process custom field when it has not
                                // been selected by user in repository settings page
                                continue;
                        }
                        try {
                                SBMSystemFields sysfield = SBMSystemFields.valueOf(f.getName());
                                if( sysfield.hasTaskAttribute()) {
                                        // the field has an equivalent in the task attribute
                                        // mappings and therefore it can be skipped in the custom
                                        // fields section
                                        continue;
                                }
                        } catch (IllegalArgumentException e) {
                                //field is not a system field, do nothing
                        }
                        monitor.subTask("Read custom field [" + f.getName() + "]");
                        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);
                        
                        //set value/ values depending on field type
                        // single selection
                        if (f.getType() == SBMFieldTypes.SELECTION
                                        && client.getFieldValue(ttitem, f.getName()) != null) {
                                List<SBMFieldValue> options = client.getValidSet(client
                                                .getTableName(ttitem), f.getName());
                                for (SBMFieldValue optionvalue : options) {
                                        custom.putOption(optionvalue.getInternalValue(),
                                                        optionvalue.getValue());
                                }
                                String val = client.getFieldValue(ttitem, f.getName())
                                                .getValue();
                                custom.setValue(val);
                        // Multi-Type fields
                        } 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());
                                }
                        //Date fields
                        } else if (f.getType() == SBMFieldTypes.DATETIME) {
                                String datestring = client.getFieldValue(ttitem, f.getName()).getValue();
                                long timestamp;
                                try {
                                        timestamp = DatatypeFactory.newInstance().newXMLGregorianCalendar(datestring).toGregorianCalendar().getTimeInMillis();
                                        custom.setValue(String.valueOf(timestamp));
                                } catch (DatatypeConfigurationException e) {
                                        // TODO Auto-generated catch block
                                        e.printStackTrace();
                                } catch (IllegalArgumentException e) {
                                        //date field is probably empty
                                        custom.setValue("");
                                }
                                
                        // any other field type
                        } else if (client.getFieldValue(ttitem, f.getName()) != null) {
                                custom.setValue(client.getFieldValue(ttitem, f.getName())
                                                .getValue());
                        }
                }
                monitor.worked(1);

                // ----------
                // Notes
                // ----------
                monitor.subTask("Read 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);
                }
                monitor.worked(1);

                data.setPartial(false);
                return data;
        }
}