Skip to main content
summaryrefslogtreecommitdiffstats
blob: 8a0c3f998218a0730f64fa7a2bb57bf2af76e5de (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
/*******************************************************************************
 * 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
 * -------- -------- -----------------------------------------------------------
 * 20060721   149070 gilberta@ca.ibm.com - Gilbert Andrews, update to respect preferences
 *******************************************************************************/
package org.eclipse.wst.ws.internal.explorer.platform.actions;

import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.OutputStream;
import org.eclipse.core.resources.IFile;
import org.eclipse.core.resources.IProject;
import org.eclipse.core.resources.IResource;
import org.eclipse.core.resources.IWorkspaceRoot;
import org.eclipse.core.resources.ResourcesPlugin;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.IStatus;
import org.eclipse.core.runtime.NullProgressMonitor;
import org.eclipse.wst.command.internal.env.context.PersistentResourceContext;
import org.eclipse.wst.command.internal.env.eclipse.EclipseEnvironment;
import org.eclipse.wst.common.environment.IEnvironment;
import org.eclipse.wst.ws.internal.explorer.platform.constants.ActionInputs;
import org.eclipse.wst.ws.internal.explorer.platform.perspective.Controller;
import org.eclipse.wst.ws.internal.explorer.platform.perspective.MessageQueue;
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.wsil.AddWSDLToWSILCommand;

public abstract class ImportToWorkbenchAction extends FormAction {
  private IWorkspaceRoot iWorkspaceRoot_;
  private IProject iProject_;
  private IResource targetFileResource_;

    public ImportToWorkbenchAction(Controller controller) {
        super(controller);
        iWorkspaceRoot_ = ResourcesPlugin.getWorkspace().getRoot();
    }

    protected boolean processParsedResults(MultipartFormDataParser parser) throws MultipartFormDataException {
        getSelectedFormTool();
        MessageQueue msgQueue = controller_.getCurrentPerspective().getMessageQueue();
        boolean inputsValid = true;
        String workbenchProjectName = parser.getParameter(ActionInputs.WORKBENCH_PROJECT_NAME);
        if (workbenchProjectName == null || workbenchProjectName.length() <= 0) {
            msgQueue.addMessage(controller_.getMessage("MSG_ERROR_INVALID_WORKBENCH_PROJECT"));
            inputsValid = false;
        }
        else
        {
            iProject_ = iWorkspaceRoot_.getProject(workbenchProjectName);
            propertyTable_.put(ActionInputs.WORKBENCH_PROJECT_NAME, workbenchProjectName);
        }
        String importFile = parser.getParameter(ActionInputs.IMPORT_FILE);
        if (importFile != null)
        {
            propertyTable_.put(ActionInputs.IMPORT_FILE, ActionInputs.IMPORT_FILE);
            String importedFileName = parser.getParameter(ActionInputs.IMPORTED_FILE_NAME);
            if (importedFileName == null || importedFileName.length() <= 0)
            {
                msgQueue.addMessage(controller_.getMessage("MSG_ERROR_INVALID_FILE_NAME"));
                inputsValid = false;
            }
            else
                propertyTable_.put(ActionInputs.IMPORTED_FILE_NAME, importedFileName);
        }
        String importToWSIL = parser.getParameter(ActionInputs.IMPORT_TO_WSIL);
        if (importToWSIL != null)
        {
            propertyTable_.put(ActionInputs.IMPORT_TO_WSIL, ActionInputs.IMPORT_TO_WSIL);
            propertyTable_.put(ActionInputs.IMPORTED_WSDL_URL, parser.getParameter(ActionInputs.IMPORTED_WSDL_URL));
            String wsilFileName = parser.getParameter(ActionInputs.WSIL_FILE_NAME);
            if (wsilFileName == null || wsilFileName.length() <= 0)
            {
                msgQueue.addMessage(controller_.getMessage("MSG_ERROR_INVALID_WSIL_FILE_NAME"));
                inputsValid = false;
            }
            else
                propertyTable_.put(ActionInputs.WSIL_FILE_NAME, wsilFileName);
        }
        if (importFile == null && importToWSIL == null)
        {
            msgQueue.addMessage(controller_.getMessage("MSG_ERROR_NO_IMPORT_OPTION_SELECTED"));
            inputsValid = false;
        }
        return inputsValid;
    }

    private OutputStream getOutputStream(String defaultFileName) throws FileNotFoundException {
        File file = new File(controller_.getServletEngineStateLocation() + defaultFileName);
        FileOutputStream fos = new FileOutputStream(file);
        return fos;
    }

    private boolean importTempFileToWebProject(String defaultFileName) throws FileNotFoundException, CoreException
    {
      if (targetFileResource_ != null && targetFileResource_.getType() == IResource.FILE)
        targetFileResource_.delete(true,new NullProgressMonitor());

      String importedFileName = (String)propertyTable_.get(ActionInputs.IMPORTED_FILE_NAME);
      File file = new File(controller_.getServletEngineStateLocation()+defaultFileName);
      BufferedInputStream bis = new BufferedInputStream(new FileInputStream(file));

      IFile iFile = iProject_.getFile("/"+importedFileName);
      iFile.create(bis,true,new NullProgressMonitor());

      // Remove the temporary file
      file.delete();
      return true;
    }

    public boolean fileExists()
    {
      String importedFileName = (String)propertyTable_.get(ActionInputs.IMPORTED_FILE_NAME);
      if (importedFileName != null)
      {
        targetFileResource_ = iProject_.findMember(importedFileName);
        return (targetFileResource_ != null && targetFileResource_.getType() == IResource.FILE);
      }
      else
        return false;
    }

    public boolean isTargetFileResourceReadOnly()
    {
      return targetFileResource_.getResourceAttributes().isReadOnly();
    }

    
    public boolean isCheckoutFilesEnabled()
    {
    	return PersistentResourceContext.getInstance().isCheckoutFilesEnabled();
    }

    public String getWebServicePluginFileMessage(String messageId)
    {
      String importedFileName = (String)propertyTable_.get(ActionInputs.IMPORTED_FILE_NAME);
      return controller_.getMessage(messageId,new String[]{iProject_.getFullPath().toString(),importedFileName});
    }

    
    public boolean isOverwriteFilesEnabled()
    {
    	return PersistentResourceContext.getInstance().isOverwriteFilesEnabled();
    }

    public boolean validateEdit()
    {
      IFile[] files = new IFile[1];
      files[0] = (IFile)targetFileResource_;
      IStatus status = ResourcesPlugin.getWorkspace().validateEdit(files,null);
      MessageQueue messageQueue = controller_.getCurrentPerspective().getMessageQueue();
      messageQueue.addMessage(status.getMessage());
      return status.isOK();
    }

    public boolean run() {
        MessageQueue msgQueue = controller_.getCurrentPerspective().getMessageQueue();
        if (propertyTable_.get(ActionInputs.IMPORT_FILE) != null)
        {
            try {
                ImportToFileSystemAction action = newImportToFileSystemAction();
                action.run();
                String defaultFileName = action.getDefaultFileName();
                if (!action.write(getOutputStream(defaultFileName))) {
                    msgQueue.addMessage(controller_.getMessage("MSG_ERROR_WRITING_TEMP_FILE_TO_FS"));
                    return false;
                }
                importTempFileToWebProject(defaultFileName);
                String importedFileName = (String)propertyTable_.get(ActionInputs.IMPORTED_FILE_NAME);
                msgQueue.addMessage(controller_.getMessage("MSG_INFO_IMPORT_TO_WORKBENCH_SUCCESSFUL", importedFileName));
            }
            catch (FileNotFoundException fnfe) {
                msgQueue.addMessage(fnfe.getMessage());
                return false;
            }
            catch (CoreException ce) {
                msgQueue.addMessage(ce.getMessage());
                return false;
            }
        }
        if (propertyTable_.get(ActionInputs.IMPORT_TO_WSIL) != null)
        {
            String projectName = (String)propertyTable_.get(ActionInputs.WORKBENCH_PROJECT_NAME);
            String importedWSILFileName = (String)propertyTable_.get(ActionInputs.WSIL_FILE_NAME);
            String wsdlURL = (String)propertyTable_.get(ActionInputs.IMPORTED_WSDL_URL);
            StringBuffer wsilPlatformURL = new StringBuffer("platform:/resource/");
            wsilPlatformURL.append(projectName);
            wsilPlatformURL.append('/');
            wsilPlatformURL.append(importedWSILFileName);
            AddWSDLToWSILCommand command = new AddWSDLToWSILCommand();
            String[] args = new String[5];
            args[0] = AddWSDLToWSILCommand.ARG_WSIL;
            args[1] = wsilPlatformURL.toString();
            args[2] = AddWSDLToWSILCommand.ARG_WSDL;
            args[3] = wsdlURL;
            args[4] = AddWSDLToWSILCommand.ARG_RESOLVE_WSDL;
            command.setArguments(args);
            IEnvironment env = new EclipseEnvironment(null, null, null);
            command.setEnvironment( env );
            command.execute( null, null );
            msgQueue.addMessage(controller_.getMessage("MSG_INFO_IMPORT_SERVICE_REF_TO_WSIL_SUCCESSFUL", importedWSILFileName));
        }
        return true;
    }

    public abstract String getStatusContentVar();
    public abstract String getStatusContentPage();
    public abstract ImportToFileSystemAction newImportToFileSystemAction();
}

Back to the top