/* * 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.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 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; } public String getFieldValue(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"; } //Todo caching public String getFieldLabel(TTItem ttitem, String fieldname) { String itemid = ttitem.getGenericItem().getValue().getItemID().getValue(); String tableid = new StringTokenizer(itemid, ":").nextToken(); if (tables.isEmpty()) { try { tables = port.getTables(getAuth(), null, TableType.PRIMARY_TABLE); } catch (AEWebservicesFaultFault e) { new CoreException( RepositoryStatus.createInternalError( SBMConnectorPlugin.PLUGIN_ID, e.getFaultInfo(), e)); } } 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"; } 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; } public List getPrimaryTables() { List table_names = new ArrayList(); if (tables.isEmpty()) { try { tables = port.getTables(getAuth(), null, TableType.PRIMARY_TABLE); } catch (AEWebservicesFaultFault e) { new CoreException( RepositoryStatus.createInternalError( SBMConnectorPlugin.PLUGIN_ID, e.getFaultInfo(), e)); } } for (TableData table : tables) { table_names.add(table.getName().getValue()); } return table_names; } }