Skip to main content
summaryrefslogtreecommitdiffstats
blob: 9ef8c607b5acc1e93911867defd42bb964ef6371 (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
/*******************************************************************************
 * Copyright (c) 2004,2006 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
 * -------- -------- -----------------------------------------------------------
 * 20060404   134913 sengpl@ca.ibm.com - Seng Phung-Lu   
 * 20060517   142027 sengpl@ca.ibm.com - Seng Phung-Lu    
 *******************************************************************************/
package org.eclipse.jst.ws.internal.consumption.ui.widgets;

import java.util.ArrayList;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.Hashtable;
import java.util.List;

import org.eclipse.core.runtime.IAdaptable;
import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.core.runtime.IStatus;
import org.eclipse.core.runtime.Status;
import org.eclipse.jst.j2ee.webservice.wsdd.Handler;
import org.eclipse.jst.j2ee.webservice.wsdd.PortComponent;
import org.eclipse.jst.j2ee.webservice.wsdd.WebServiceDescription;
import org.eclipse.jst.j2ee.webservice.wsdd.WsddFactory;
import org.eclipse.jst.j2ee.webservice.wsdd.WsddResource;
import org.eclipse.jst.j2ee.webservice.wsdd.internal.impl.WsddFactoryImpl;
import org.eclipse.jst.ws.internal.consumption.ui.common.HandlerDescriptionHelper;
import org.eclipse.jst.ws.internal.consumption.ui.common.HandlerDescriptionHolder;
import org.eclipse.jst.ws.internal.consumption.ui.widgets.object.HandlerTableItem;
import org.eclipse.wst.common.frameworks.datamodel.AbstractDataModelOperation;
import org.eclipse.wst.common.internal.emf.utilities.EtoolsCopyUtility;

/*
 * Provide a way to externalize the edited fields and create new handlers
 *  
 */
public class ServiceHandlersWidgetOutputCommand extends AbstractDataModelOperation
{

  private WsddResource[] wsddResource_;
  private boolean isMultipleSelection_;
  private HandlerDescriptionHolder[] handlerDescriptionHolder_;

  /**
   * For each HandlerDescriptionHolder, the user selected Handlers are
   * sync'd up with the internal model
   * For multiple services, handlers are added (not removed) to each port.
   * 
   */
  public IStatus execute( IProgressMonitor monitor, IAdaptable adaptable )
  {
    IStatus status = Status.OK_STATUS;
    try {
      if (isMultipleSelection_){
        for (int i=0; i<handlerDescriptionHolder_.length;i++){
           
          handlerDescriptionHolder_[i].addHandlerToAllPorts();
        }
      }
      else {
        // add handler(s) for single service selected
        for (int h=0;h<handlerDescriptionHolder_.length;h++){
  
          Hashtable wsPortsTable = new Hashtable();
          Hashtable wsPortToHandlerTable = new Hashtable();
  
          String serviceDescName = handlerDescriptionHolder_[h].getDescriptionName();
          // load PortName -> PortComponent table
          HandlerDescriptionHolder hdh = HandlerDescriptionHelper.getForDescriptionName(handlerDescriptionHolder_, serviceDescName);
          if (hdh!=null) {
            
            WebServiceDescription wsDescription_ =  hdh.getDescriptionObject();          
            if (wsDescription_ != null) {
              List wsPorts = wsDescription_.getPortComponents();
              for (int k = 0; k < wsPorts.size(); k++) {
                PortComponent port = (PortComponent) wsPorts.get(k);
                wsPortsTable.put(port.getPortComponentName(), port);
                wsPortToHandlerTable.put(port.getPortComponentName(), new ArrayList());
              }
            }

            List handlerTableItems = hdh.getHandlerList();          
            if (handlerTableItems != null) {
  
              WsddFactory wsddFactory = new WsddFactoryImpl();
  
              //form port components tables first
              for (int i = 0; i < handlerTableItems.size(); i++) {
  
                HandlerTableItem hti = (HandlerTableItem) handlerTableItems.get(i);
                PortComponent wsPort = (PortComponent) hti.getPort();
                if (wsPort == null) {
                //find it
                  if (wsPortsTable.get(hti.getPortName()) != null)
                    wsPort = (PortComponent) wsPortsTable.get(hti.getPortName());
                  else {
                    // create it; should not get into here
                    wsPort = wsddFactory.createPortComponent();
                    wsPort.setPortComponentName(hti.getPortName());
                    wsPortsTable.put(wsPort.getPortComponentName(), wsPort);
                  }
                }
                wsPortToHandlerTable.put(wsPort.getPortComponentName(), new ArrayList());
              }
  
              // form Handler and ports table
              for (int i = 0; i < handlerTableItems.size(); i++) {
    
                HandlerTableItem hti = (HandlerTableItem) handlerTableItems.get(i);
                String portName = hti.getPortName();
    
                PortComponent port = (PortComponent) wsPortsTable.get(portName);
                if (port == null) return status;
    
                List existingHandlers = port.getHandlers();
    
                Object handler = hti.getHandler();
                if (!existingHandlers.contains(handler)) {
                  // create it
                  Handler newHandler = wsddFactory.createHandler();
                  newHandler.setHandlerName(hti.getHandlerName());
                  newHandler.setHandlerClass(hti.getHandlerClassName());
                  ((List) wsPortToHandlerTable.get(portName)).add(newHandler);
    
                }
                else if (handler instanceof Handler) {
                  // clone it
                  EtoolsCopyUtility copyUtil = new EtoolsCopyUtility();
                  copyUtil.setCopyAdapters(true);
                  Handler clonedHandler = (Handler) copyUtil.copy((Handler) handler);
                  ((List) wsPortToHandlerTable.get(portName)).add(clonedHandler);
                }
              }
            }
 
            // remove existing handlers from ports
            removeExistingHandlers(wsPortsTable, wsPortToHandlerTable);
    
            // add ports to wsDescription
            addPortsToDescriptions(wsPortsTable, wsDescription_);
    
            // add handlers to ports
            addHandlersToPorts(wsPortsTable, wsPortToHandlerTable);
          }
        }
      }
      
      // save
      if (wsddResource_ != null) {
        for (int i=0;i<wsddResource_.length;i++) {
        	if (wsddResource_[i]!=null)
        		wsddResource_[i].save(new HashMap());
        }
      }
  
      }
    catch (Exception e) {
      e.printStackTrace();
    }
    
    return status;
  }

  private void removeExistingHandlers(Hashtable portsTable, Hashtable portsToHandlersTable) {
    try {
      Enumeration ports = portsToHandlersTable.keys();
      while (ports.hasMoreElements()) {
        String portName = (String) ports.nextElement();
        Object port = portsTable.get(portName);
        if (port != null && port instanceof PortComponent) {
          List handlers = ((PortComponent) port).getHandlers();
          handlers.clear();
        }
      }
    }
    catch (Exception e) {
      e.printStackTrace();
    }
  }

  private void addPortsToDescriptions(Hashtable portsTable, WebServiceDescription wsDescription_) {
    try {
      Enumeration ports = portsTable.keys();
      while (ports.hasMoreElements()) {
        String portName = (String) ports.nextElement();
        // newly edited port
        Object port = portsTable.get(portName);
        
        // existing port
        List emfPorts = wsDescription_.getPortComponents();
        if (!emfPorts.contains(port) && port != null && port instanceof PortComponent) {
          emfPorts.add((PortComponent) port);
        }
      }
    }

    catch (Exception e) {
      e.printStackTrace();
    }
  }

  private void addHandlersToPorts(Hashtable portsTable, Hashtable portsToHandlersTable) {
    try {
      Enumeration ports = portsToHandlersTable.keys();
      while (ports.hasMoreElements()) {
        String portName = (String) ports.nextElement();
        Object port = portsTable.get(portName);
        if (port != null && port instanceof PortComponent) {
          List handlers = (List) portsToHandlersTable.get(portName);

          List modelHandlers = ((PortComponent) port).getHandlers();
          modelHandlers.addAll(handlers);
        }
      }
    }
    catch (Exception e) {
      e.printStackTrace();
    }
  }

  /**
   * HandlerDescriptionHolder
   * @param hdhs
   */
  public void setHandlerDescriptionHolders(HandlerDescriptionHolder[] hdhs){
    this.handlerDescriptionHolder_ = hdhs;
  }
  
  public void setIsMultipleSelection(boolean isMulitpleSelection) {
    this.isMultipleSelection_ = isMulitpleSelection;
  }


  public void setWsddResource(WsddResource[] wsddRes) {
    this.wsddResource_ = wsddRes;
  }

}

Back to the top