Skip to main content
summaryrefslogtreecommitdiffstats
blob: 67a86f169d73c994c973126dd82711cd0c4e8d0e (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
/*******************************************************************************
 * Copyright (c) 2001, 2007 IBM Corporation and others.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 * IBM Corporation - initial API and implementation
 * yyyymmdd bug      Email and other contact information
 * -------- -------- -----------------------------------------------------------
 *  20070419   182864 gilberta@ca.ibm.com - Gilbert Andrews
 *******************************************************************************/

package org.eclipse.wst.ws.internal.explorer.platform.uddi.actions;

import java.net.MalformedURLException;
import java.util.Enumeration;
import java.util.Hashtable;
import java.util.Vector;
import org.eclipse.wst.ws.internal.explorer.platform.datamodel.ListElement;
import org.eclipse.wst.ws.internal.explorer.platform.perspective.Controller;
import org.eclipse.wst.ws.internal.explorer.platform.perspective.FormTool;
import org.eclipse.wst.ws.internal.explorer.platform.perspective.FormToolPropertiesInterface;
import org.eclipse.wst.ws.internal.explorer.platform.perspective.MessageQueue;
import org.eclipse.wst.ws.internal.explorer.platform.perspective.Perspective;
import org.eclipse.wst.ws.internal.explorer.platform.uddi.constants.UDDIActionInputs;
import org.eclipse.wst.ws.internal.explorer.platform.uddi.datamodel.BusinessElement;
import org.eclipse.wst.ws.internal.explorer.platform.uddi.datamodel.RegistryElement;
import org.eclipse.wst.ws.internal.explorer.platform.uddi.perspective.MultipleFormToolPropertiesInterface;
import org.eclipse.wst.ws.internal.explorer.platform.uddi.perspective.RegistryNode;
import org.eclipse.wst.ws.internal.explorer.platform.uddi.perspective.UDDIPerspective;
import org.eclipse.wst.ws.internal.explorer.platform.util.MultipartFormDataException;
import org.eclipse.wst.ws.internal.explorer.platform.util.MultipartFormDataParser;
import org.eclipse.wst.ws.internal.explorer.platform.util.Validator;
import org.uddi4j.UDDIException;
import org.uddi4j.client.UDDIProxy;
import org.uddi4j.datatype.business.BusinessEntity;
import org.uddi4j.datatype.service.BusinessService;
import org.uddi4j.datatype.service.BusinessServices;
import org.uddi4j.transport.TransportException;

public class ManageReferencedServicesAction extends UDDIPropertiesFormAction
{
  protected String subQueryKey_;
  protected boolean isSubQueryGet_;
  protected String newSubQuery_;
  protected String newSubQueryItem_;
  protected boolean subQueryInitiated_;
  protected RegistryNode regNode_;

  public ManageReferencedServicesAction(Controller controller)
  {
    super(controller);
    subQueryKey_ = null;
    isSubQueryGet_ = false;
    subQueryInitiated_ = false;
    regNode_ = getRegistryNode();
  }

  protected final boolean processParsedResults(MultipartFormDataParser parser) throws MultipartFormDataException
  {
    newSubQuery_ = parser.getParameter(UDDIActionInputs.NEW_SUBQUERY_INITIATED);
    String isSubQueryGetString = parser.getParameter(UDDIActionInputs.SUBQUERY_GET);
    newSubQueryItem_ = parser.getParameter(UDDIActionInputs.NEW_SUBQUERY_QUERY_ITEM);
    subQueryKey_ = parser.getParameter(UDDIActionInputs.SUBQUERY_KEY);
    String[] referencedServicesCheckboxes = parser.getParameterValues(UDDIActionInputs.REFERENCED_SERVICE_SELECT_STATE);
    String[] serviceNodeIds = parser.getParameterValues(UDDIActionInputs.NODEID_SERVICE);
    String operation = parser.getParameter(UDDIActionInputs.MANAGE_REFERENCED_SERVICES_OPERATION);
    String publishURL = parser.getParameter(UDDIActionInputs.QUERY_INPUT_ADVANCED_PUBLISH_URL);
    String userId = parser.getParameter(UDDIActionInputs.QUERY_INPUT_ADVANCED_USERID);
    String password = parser.getParameter(UDDIActionInputs.QUERY_INPUT_ADVANCED_PASSWORD);

    // Validate the data.
    boolean inputsValid = true;
    UDDIPerspective uddiPerspective = controller_.getUDDIPerspective();
    MessageQueue messageQueue = uddiPerspective.getMessageQueue();

    FormTool formTool = getSelectedFormTool();
    FormToolPropertiesInterface formToolPI = ((MultipleFormToolPropertiesInterface)formTool).getFormToolProperties(subQueryKey_);
    formToolPI.clearErrors();

    if (newSubQuery_ != null && newSubQuery_.length() > 0)
      subQueryInitiated_ = true;
    else
      subQueryInitiated_ = false;

    if (isSubQueryGetString != null)
      isSubQueryGet_ = Boolean.valueOf(isSubQueryGetString).booleanValue();

    if (operation != null)
      propertyTable_.put(UDDIActionInputs.MANAGE_REFERENCED_SERVICES_OPERATION,operation);
    else
      removeProperty(UDDIActionInputs.MANAGE_REFERENCED_SERVICES_OPERATION);

    if (referencedServicesCheckboxes != null)
      propertyTable_.put(UDDIActionInputs.REFERENCED_SERVICE_SELECT_STATE,referencedServicesCheckboxes);
    else
      removeProperty(UDDIActionInputs.REFERENCED_SERVICE_SELECT_STATE);

    if (serviceNodeIds != null)
    {
      Vector services = (Vector)formToolPI.getProperty(UDDIActionInputs.QUERY_INPUT_SERVICES);
      if (services == null)
        services = new Vector();
      Hashtable resultHash = new Hashtable();
      for (int i=0;i<serviceNodeIds.length;i++)
        resultHash.put(serviceNodeIds[i],Boolean.TRUE);
      for (int i=0;i<services.size();i++)
      {
        ListElement listElement = (ListElement)services.elementAt(i);
        if (resultHash.get(String.valueOf(listElement.getTargetNodeId())) == null)
        {
          services.removeElementAt(i);
          i--;
        }
      }
      propertyTable_.put(UDDIActionInputs.QUERY_INPUT_SERVICES,services);
    }
    else
      removeProperty(UDDIActionInputs.QUERY_INPUT_SERVICES);

    RegistryElement regElement = (RegistryElement)regNode_.getTreeElement();
    if (!regElement.isLoggedIn())
    {
      if (publishURL != null)
      {
        propertyTable_.put(UDDIActionInputs.QUERY_INPUT_ADVANCED_PUBLISH_URL,publishURL);
        formTool.setProperty(UDDIActionInputs.QUERY_INPUT_ADVANCED_PUBLISH_URL,publishURL);
      }

      if (!subQueryInitiated_ && !Validator.validateString(publishURL))
      {
        inputsValid = false;
        formTool.flagError(UDDIActionInputs.QUERY_INPUT_ADVANCED_PUBLISH_URL);
        messageQueue.addMessage(uddiPerspective.getMessage("MSG_ERROR_INVALID_PUBLISH_URL"));
      }

      if (userId != null)
      {
        propertyTable_.put(UDDIActionInputs.QUERY_INPUT_ADVANCED_USERID,userId);
        formTool.setProperty(UDDIActionInputs.QUERY_INPUT_ADVANCED_USERID,userId);
      }

      if (!subQueryInitiated_ && !Validator.validateString(userId))
      {
        inputsValid = false;
        formTool.flagError(UDDIActionInputs.QUERY_INPUT_ADVANCED_USERID);
        messageQueue.addMessage(uddiPerspective.getMessage("MSG_ERROR_INVALID_USERID"));
      }

      if (password != null)
      {
        propertyTable_.put(UDDIActionInputs.QUERY_INPUT_ADVANCED_PASSWORD,password);
        formTool.setProperty(UDDIActionInputs.QUERY_INPUT_ADVANCED_PASSWORD,password);
      }
    }

    for (int i=0;i<removedProperties_.size();i++)
      formToolPI.removeProperty(removedProperties_.elementAt(i));
    formToolPI.updatePropertyTable(propertyTable_);

    return inputsValid;
  }

  public boolean run()
  {
    UDDIPerspective uddiPerspective = controller_.getUDDIPerspective();
    MessageQueue messageQueue = uddiPerspective.getMessageQueue();
    String operationString = (String)propertyTable_.get(UDDIActionInputs.MANAGE_REFERENCED_SERVICES_OPERATION);
    int operation = Integer.parseInt(operationString);
    RegistryElement regElement = (RegistryElement)regNode_.getTreeElement();
    BusinessElement busElement = (BusinessElement)(getSelectedNavigatorNode().getTreeElement());
    FormTool formTool = getSelectedFormTool();
    FormToolPropertiesInterface formToolPI = ((MultipleFormToolPropertiesInterface)formTool).getFormToolProperties(subQueryKey_);
    Vector referencedServices = (Vector)formToolPI.getProperty(UDDIActionInputs.QUERY_INPUT_REFERENCED_SERVICES);
    boolean operationResult = true;
    try
    {
      String publishURL = (String)propertyTable_.get(UDDIActionInputs.QUERY_INPUT_ADVANCED_PUBLISH_URL);
      String userId = (String)propertyTable_.get(UDDIActionInputs.QUERY_INPUT_ADVANCED_USERID);
      String password = (String)propertyTable_.get(UDDIActionInputs.QUERY_INPUT_ADVANCED_PASSWORD);
      UDDIProxy proxy = regElement.getProxy();
      BusinessEntity currentSP = busElement.getBusinessEntity();
      BusinessEntity sp = (BusinessEntity)proxy.get_businessDetail(currentSP.getBusinessKey()).getBusinessEntityVector().get(0);
      
      if (!regElement.isLoggedIn())
        regElement.performLogin(publishURL,userId,password);
        
      Hashtable servicesHash = new Hashtable();
      Vector backupBusServiceVector = new Vector();
      switch (operation)
      {
        case UDDIActionInputs.MANAGE_REFERENCED_SERVICES_OPERATION_ADD:
          Vector services = (Vector)propertyTable_.get(UDDIActionInputs.QUERY_INPUT_SERVICES);          
          for (int i=0;i<services.size();i++)
          {
            ListElement serviceListElement = (ListElement)services.elementAt(i);
            BusinessService bs = (BusinessService)serviceListElement.getObject();
            servicesHash.put(bs.getServiceKey(),bs);
          }
          
          // Save the current list of BusinessServices for rollback operations.
          backupAndUpdateBusinessServices(backupBusServiceVector,sp,servicesHash,true);
                    
          try
          {
            Vector beVector = new Vector();
            beVector.add(sp);
            sp = (BusinessEntity)proxy.save_business(regElement.getAuthInfoString(), beVector).getBusinessEntityVector().get(0);
            for (int i=0;i<services.size();i++)
            {
              ListElement serviceElement = (ListElement)services.elementAt(i);
              BusinessService bs = (BusinessService)serviceElement.getObject();
              String bsName = bs.getDefaultNameString();
              messageQueue.addMessage(uddiPerspective.getMessage("MSG_INFO_SERVICE_REFERENCED",bsName));
              services.removeElementAt(i);
              i--;
            }
          }
          catch (TransportException e)
          {
            // Roll back and restore the old list of services.
            rollbackBusinessServices(sp,backupBusServiceVector,services,uddiPerspective,messageQueue,"MSG_ERROR_SERVICE_NOT_REFERENCED");
            handleUnexpectedException(uddiPerspective,messageQueue,"TransportException",e);
            operationResult = false;
          }
          catch (UDDIException e)
          {
            // Roll back and restore the old list of services.
            rollbackBusinessServices(sp,backupBusServiceVector,services,uddiPerspective,messageQueue,"MSG_ERROR_SERVICE_NOT_REFERENCED");
            if(UDDIExceptionHandler.requiresReset(e))
        		regElement.setDefaults();
            messageQueue.addMessage(uddiPerspective.getController().getMessage("MSG_ERROR_UNEXPECTED"));
            messageQueue.addMessage("UDDIException");
            messageQueue.addMessage(e.toString());
            operationResult = false;
          }
          formToolPI.setProperty(UDDIActionInputs.QUERY_INPUT_SERVICES,services);
          break;
        case UDDIActionInputs.MANAGE_REFERENCED_SERVICES_OPERATION_REMOVE:
          String[] referencedServicesCheckboxes = (String[])propertyTable_.get(UDDIActionInputs.REFERENCED_SERVICE_SELECT_STATE);
          Vector selectedReferencedServiceVector = new Vector();
          for (int i=0;i<referencedServicesCheckboxes.length;i++)
          {
            boolean isChecked = Boolean.valueOf(referencedServicesCheckboxes[i]).booleanValue();
            if (isChecked)
            {
              ListElement referencedServiceListElement = (ListElement)referencedServices.elementAt(i);
              BusinessService bs = (BusinessService)referencedServiceListElement.getObject();
              servicesHash.put(bs.getServiceKey(),bs);
              selectedReferencedServiceVector.addElement(bs);
            }
          }          
          
          // Save the current list of business services for rollback operations.
          backupAndUpdateBusinessServices(backupBusServiceVector,sp,servicesHash,false);
          
          try
          {
            Vector beVector = new Vector();
            beVector.add(sp);
            sp = (BusinessEntity)proxy.save_business(regElement.getAuthInfoString(), beVector).getBusinessEntityVector().get(0);
            for (int i=0;i<selectedReferencedServiceVector.size();i++)
            {
              BusinessService bs = (BusinessService)selectedReferencedServiceVector.elementAt(i);
              String bsName = bs.getDefaultNameString();
              messageQueue.addMessage(uddiPerspective.getMessage("MSG_INFO_SERVICE_REFERENCE_REMOVED",bsName));
            }
          }
          catch (TransportException e)
          {
            // Roll back and restore the old list of services.
            rollbackBusinessServices(sp,backupBusServiceVector,selectedReferencedServiceVector,uddiPerspective,messageQueue,"MSG_ERROR_REFERENCE_NOT_REMOVED");
            handleUnexpectedException(uddiPerspective,messageQueue,"TransportException",e);
            operationResult = false;
          }
          catch (UDDIException e)
          {
            // Roll back and restore the old list of services.
            rollbackBusinessServices(sp,backupBusServiceVector,selectedReferencedServiceVector,uddiPerspective,messageQueue,"MSG_ERROR_REFERENCE_NOT_REMOVED");
            if(UDDIExceptionHandler.requiresReset(e))
            	regElement.setDefaults();
            messageQueue.addMessage(uddiPerspective.getController().getMessage("MSG_ERROR_UNEXPECTED"));
            messageQueue.addMessage("UDDIException");
            messageQueue.addMessage(e.toString());
            operationResult = false;
          }
      }
      // Ensure that the referenced services are refreshed when the form reloads.
      formToolPI.removeProperty(UDDIActionInputs.QUERY_INPUT_REFERENCED_SERVICES);
      busElement.setBusinessEntity(sp);
      return operationResult;
    }
    catch (TransportException e)
    {
      handleUnexpectedException(uddiPerspective,messageQueue,"TransportException",e);
      operationResult = false;
    }
    catch (UDDIException e)
    {
    	if(UDDIExceptionHandler.requiresReset(e))
    		regElement.setDefaults();
    	messageQueue.addMessage(uddiPerspective.getController().getMessage("MSG_ERROR_UNEXPECTED"));
    	messageQueue.addMessage("UDDIException");
    	messageQueue.addMessage(e.toString());
    	operationResult = false;
    }
    catch (MalformedURLException e)
    {
      handleUnexpectedException(uddiPerspective,messageQueue,"MalformedURLException",e);
      operationResult = false;
    }
    return operationResult;
  }

  public final boolean isSubQueryInitiated()
  {
    return subQueryInitiated_;
  }

  public final boolean isSubQueryGet()
  {
    return isSubQueryGet_;
  }

  public final String getSubQueryKey()
  {
    return subQueryKey_;
  }

  public final String getNewSubQuery()
  {
    return newSubQuery_;
  }

  public final String getNewSubQueryItem()
  {
    return newSubQueryItem_;
  }
  
  // Backup the business services and remove those in serviceHash from the current list. Return the updated list of business services
  private final void backupAndUpdateBusinessServices(Vector backupBusServiceVector,BusinessEntity be,Hashtable servicesHash,boolean isAdd)
  {
    // Save the current list of business services for rollback operations.
    BusinessServices currentBusServices = be.getBusinessServices();
    if (currentBusServices == null)
      currentBusServices = new BusinessServices();
    Vector currentBusServiceVector = currentBusServices.getBusinessServiceVector();
    for (int i=0;i<currentBusServiceVector.size();i++)
    {
      BusinessService currentBusService = (BusinessService)currentBusServiceVector.elementAt(i);
      backupBusServiceVector.addElement(currentBusService);
      // If the current business service is a member of the list to be removed/added, remove it now.
      if (servicesHash.get(currentBusService.getServiceKey()) != null)
      {
        currentBusServiceVector.removeElementAt(i);
        i--;
      }
    }
    
    if (isAdd)
    {
      Enumeration e = servicesHash.elements();
      while (e.hasMoreElements())
        currentBusServiceVector.addElement((BusinessService)e.nextElement());
    }
    be.setBusinessServices(currentBusServices);
  }
  
  private final void rollbackBusinessServices(BusinessEntity be,Vector backupBusServiceVector,Vector operatedServices,Perspective perspective,MessageQueue messageQueue,String errorMessageKey)
  {
    // Roll back and restore the old list of services.
    be.getBusinessServices().setBusinessServiceVector(backupBusServiceVector);
    for (int i=0;i<operatedServices.size();i++)
    {
      ListElement serviceListElement = (ListElement)operatedServices.elementAt(i);
      BusinessService bs = (BusinessService)serviceListElement.getObject();
      String bsName = bs.getDefaultNameString();
      messageQueue.addMessage(perspective.getMessage(errorMessageKey,bsName));
    }    
  }
}

Back to the top