/* * 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.math.BigInteger; import java.net.URL; import java.util.ArrayList; import java.util.Date; import java.util.Iterator; import java.util.List; import java.util.StringTokenizer; import javax.xml.namespace.QName; import javax.xml.ws.BindingProvider; import net.brutex.mylyn.sbmconnector.SBMConnectorPlugin; import net.brutex.mylyn.sbmconnector.core.model.SBMField; 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.AEWebservicesFaultFault; import net.brutex.sbm.wsclient.Aewebservices71; import net.brutex.sbm.wsclient.Aewebservices71PortType; import net.brutex.sbm.wsclient.Auth; import net.brutex.sbm.wsclient.Field; import net.brutex.sbm.wsclient.NameValue; import net.brutex.sbm.wsclient.Note; import net.brutex.sbm.wsclient.ObjectFactory; import net.brutex.sbm.wsclient.TTItem; import net.brutex.sbm.wsclient.TableData; import net.brutex.sbm.wsclient.TableType; import net.brutex.sbm.wsclient.Value; import org.eclipse.core.runtime.CoreException; import org.eclipse.mylyn.commons.net.AuthenticationCredentials; import org.eclipse.mylyn.commons.net.AuthenticationType; import org.eclipse.mylyn.tasks.core.RepositoryStatus; import org.eclipse.mylyn.tasks.core.TaskRepository; public class SBMClient { private Aewebservices71PortType port; private static final QName SERVICE_NAME = new QName("http://localhost:80/gsoap/aewebservices71.wsdl", "aewebservices71"); private TaskRepository repository; private ObjectFactory of; private List tables = new ArrayList(); public SBMClient(TaskRepository repository) { this.repository = repository; this.of = new ObjectFactory(); URL wsdlURL = Aewebservices71.WSDL_LOCATION; wsdlURL = this.getClass().getResource("/META-INF/aewebservices71.wsdl"); Aewebservices71 ss = new Aewebservices71(wsdlURL, SERVICE_NAME); port = ss.getAewebservices71(); ((BindingProvider)port).getRequestContext().put( BindingProvider.ENDPOINT_ADDRESS_PROPERTY, repository.getRepositoryUrl()); } public boolean canAuthenticate() throws CoreException { try { port.getUser(getAuth(), repository.getCredentials(AuthenticationType.REPOSITORY).getUserName()); } catch (AEWebservicesFaultFault e) { new CoreException(RepositoryStatus.createLoginError( repository.getRepositoryUrl(), SBMConnectorPlugin.PLUGIN_ID)); return false; } return true; } public List getTTItemsByTable(String tablename, String sql_where) throws CoreException { List list = new ArrayList(); if(sql_where==null || sql_where.isEmpty()) sql_where = "TS_ID>0"; try { list = port.getItemsByQueryWithName( getAuth(), tablename, "("+sql_where+")", "TS_SUBMITDATE desc", BigInteger.valueOf(500l), null); } catch (AEWebservicesFaultFault e) { new CoreException( RepositoryStatus.createInternalError( SBMConnectorPlugin.PLUGIN_ID, e.getFaultInfo(), e)); } return list; } public TTItem getTTItem(String itemid) { TTItem item = of.createTTItem(); try { item = port.getItem(getAuth(), itemid, null); } catch (AEWebservicesFaultFault e) { new CoreException( RepositoryStatus.createInternalError( SBMConnectorPlugin.PLUGIN_ID, e.getFaultInfo(), e)); } return item; } private Auth getAuth() { Auth auth = of.createAuth(); AuthenticationCredentials credentials = repository.getCredentials(AuthenticationType.REPOSITORY); auth.setUserId(of.createAuthUserId(credentials.getUserName())); auth.setPassword(of.createAuthPassword(credentials.getPassword())); return auth; } /** * Gets the field value for a system generic field. * * @param ttitem the ttitem * @param fieldname the fieldname * @return the static field value */ public String getStaticFieldValue(TTItem ttitem, String fieldname) { if(fieldname.equals(SBMStaticFields.SUBMITDATE.getValue())) { Date date = ttitem.getCreateDate().getValue().toGregorianCalendar().getTime(); return String.valueOf(date.getTime()); } if(fieldname.equals(SBMStaticFields.LASTMODIFIEDDATE.getValue())) { return String.valueOf(ttitem.getModifiedDate().getValue().toGregorianCalendar().getTimeInMillis()); } if(fieldname.equals("TITLE")) { if(ttitem.getTitle()==null || ttitem.getTitle().isNil()) return ""; return ttitem.getTitle().getValue(); } if(fieldname.equals(SBMStaticFields.ISSUEID.getValue())) { if(ttitem.getGenericItem()==null || ttitem.getGenericItem().getValue().getItemName()==null) { return ""; } return ttitem.getGenericItem().getValue().getItemName().getValue(); } if(fieldname.equals("ISSUETYPE")) { if(ttitem.getItemType()==null || ttitem.getItemType().isNil()) return ""; return ttitem.getItemType().getValue(); } if(fieldname.equals(SBMStaticFields.STATE.getValue())) { if(ttitem.getState()==null || ttitem.getState().isNil()) return ""; return ttitem.getState().getValue(); } if(fieldname.equals(SBMStaticFields.ID.getValue())) { return ttitem.getGenericItem().getValue().getItemID().getValue(); } if(fieldname.equals(SBMStaticFields.PROJECTID.getValue())) { if(ttitem.getClassification() ==null || ttitem.getClassification().isNil()) return ""; return ttitem.getClassification().getValue(); } if(fieldname.equals(SBMStaticFields.PROJECTUUID.getValue())) { if(ttitem.getClassificationUUID()==null || ttitem.getClassificationUUID().isNil()) return ""; return ttitem.getClassificationUUID().getValue(); } if(fieldname.equals("DESCRIPTION")) { if(ttitem.getDescription() == null || ttitem.getDescription().isNil()) return ""; return ttitem.getDescription().getValue(); } if(fieldname.equals(SBMStaticFields.SUBMITTER.getValue())) { if(ttitem.getCreatedBy()==null || ttitem.getCreatedBy().isNil()) return ""; return ttitem.getCreatedBy().getValue(); } if(fieldname.equals(SBMStaticFields.SUBMITDATE.getValue())) { return String.valueOf(ttitem.getCreateDate().getValue().toGregorianCalendar().getTimeInMillis()); } if(fieldname.equals(SBMStaticFields.LASTMODIFIER.getValue())) { if(ttitem.getModifiedBy()==null || ttitem.getModifiedBy().isNil()) return ""; return ttitem.getModifiedBy().getValue(); } if(fieldname.equals(SBMStaticFields.LASTMODIFIEDDATE.getValue())) { return String.valueOf(ttitem.getModifiedDate().getValue().toGregorianCalendar().getTimeInMillis()); } if(fieldname.equals(SBMStaticFields.ACTIVEINACTIVE.getValue())) { return ttitem.getActiveInactive().getValue(); } if(fieldname.equals(SBMStaticFields.OWNER.getValue())) { return ttitem.getOwner().getValue(); } if(fieldname.equals(SBMStaticFields.ITEMURL.getValue())) { return ttitem.getUrl().getValue(); } if(fieldname.equals(SBMStaticFields.UUID.getValue())) { return ttitem.getGenericItem().getValue().getItemUUID().getValue(); } if(fieldname.equals(SBMStaticFields.CLOSEDATE.getValue())) { Iterator list = ttitem.getExtendedFieldList().iterator(); while (list.hasNext()) { NameValue field = list.next(); if(field.getName().getValue().equals("CLOSEDATE")) { return field.getValue().getValue().getInternalValue().getValue(); } } } if(fieldname.equals(SBMStaticFields.LASTSTATECHANGEDATE.getValue())) { Iterator list = ttitem.getExtendedFieldList().iterator(); while (list.hasNext()) { NameValue field = list.next(); if(field.getName().getValue().equals("LASTSTATECHANGEDATE")) { return field.getValue().getValue().getInternalValue().getValue(); } } } if(fieldname.equals(SBMStaticFields.SECONDARYOWNER.getValue())) { Iterator list = ttitem.getExtendedFieldList().iterator(); while (list.hasNext()) { NameValue field = list.next(); if(field.getName().getValue().equals("SECONDARYOWNER")) { return field.getValue().getValue().getInternalValue().getValue(); } } } if(fieldname.equals(SBMStaticFields.LASTSTATECHANGER.getValue())) { Iterator list = ttitem.getExtendedFieldList().iterator(); while (list.hasNext()) { NameValue field = list.next(); if(field.getName().getValue().equals("LASTSTATECHANGER")) { return field.getValue().getValue().getDisplayValue().getValue(); } } } return "UNKNOWN"; } public String getFieldLabel(TTItem ttitem, String fieldname) { refreshTables(); String itemid = ttitem.getGenericItem().getValue().getItemID().getValue(); String tableid = new StringTokenizer(itemid, ":").nextToken(); for (TableData table : tables) { if (String.valueOf(table.getTableID().intValue()).equals(tableid)) { Iterator iter = table.getFieldList().iterator(); while(iter.hasNext()) { Field f = iter.next(); if(f.getName().getValue().equals(fieldname)) { return f.getDisplayName().getValue(); } } break; } } return "label_UNKNOWN"; } /** * Gets the table database name. * * @param ttitem the ttitem * @return the table name or null in case table is not found */ public String getTableName(TTItem ttitem) { refreshTables(); String itemid = ttitem.getGenericItem().getValue().getItemID().getValue(); String tableid = new StringTokenizer(itemid, ":").nextToken(); for (TableData table : tables) { if (String.valueOf(table.getTableID().intValue()).equals(tableid)) { return table.getName().getValue(); } } return null; } public List getNotes(TTItem ttitem) { List notes = new ArrayList(); Iterator iter = ttitem.getNoteList().iterator(); while(iter.hasNext()) { Note n = iter.next(); SBMNote note = new SBMNote("sbm_user", n.getTitle().getValue()+"\n"+n.getNote().getValue(), n.getModificationDateTime().toGregorianCalendar().getTime(), n.getId().toString()); notes.add(note); } return notes; } /** * Gets the names of all available primary tables. * A table name is a unique reference within one SBM environment, thus can be * used as a key. * * @return the primary table names as a list */ public List getPrimaryTables() { refreshTables(); List table_names = new ArrayList(); for (TableData table : tables) { table_names.add(table.getName().getValue()); } return table_names; } /** * Refresh table specifications from SBM web service. This * is only done once per SBMClient instance. */ private void refreshTables() { if (tables.isEmpty()) { try { //currently we limit this to primary tables tables = port.getTables(getAuth(), null, TableType.PRIMARY_TABLE); } catch (AEWebservicesFaultFault e) { new CoreException( RepositoryStatus.createInternalError( SBMConnectorPlugin.PLUGIN_ID, e.getFaultInfo(), e)); } } } /** * Gets the fields for a primary table * * @param tablename the table database name * @return the fields, empty when table does not exist */ public List getFields(String tablename) { refreshTables(); List fields = new ArrayList(); for (TableData table : tables) { if(table.getName().getValue().equals(tablename)) { Iterator iter = table.getFieldList().iterator(); while(iter.hasNext()) { Field f = iter.next(); SBMField nf = new SBMField( SBMFieldTypes.fromValue(f.getFieldType().value()), tablename, f.getDisplayName().getValue(), f.getName().getValue()); fields.add(nf); } break; } } return fields; } /** * Gets the field value for custom defined field. * (those from <extendedFieldList>) * * @param ttitem the ttitem * @param fieldname the fieldname * @return the field value or null if the field is not found */ public SBMFieldValue getFieldValue(TTItem ttitem, String fieldname) { SBMFieldValue value; Iterator fs = ttitem.getExtendedFieldList().iterator(); while(fs.hasNext()) { NameValue nv = fs.next(); if(nv.getName().getValue().equals(fieldname)) { if (nv.getValue()!=null && !nv.getValue().isNil()) { value = new SBMFieldValue( nv.getValue().getValue().getInternalValue().getValue(), nv.getValue().getValue().getDisplayValue().getValue()); return value; } } } return null; } /** * Gets the field values for custom defined, multi type field. * (those from <extendedFieldList>) * * @param ttitem the ttitem * @param fieldname the fieldname * @return the list of field values */ public List getFieldValues(TTItem ttitem, String fieldname) { List values = new ArrayList(); Iterator fs = ttitem.getExtendedFieldList().iterator(); while(fs.hasNext()) { NameValue nv = fs.next(); if(nv.getName().getValue().equals(fieldname)) { if (nv.getValues()!=null && !nv.getValues().isEmpty()) { Iterator nvv = nv.getValues().iterator(); while(nvv.hasNext()) { Value nvv_value = nvv.next(); SBMFieldValue value = new SBMFieldValue( nvv_value.getInternalValue().getValue(), nvv_value.getDisplayValue().getValue()); values.add(value); } return values; } } } return values; } }