Upload/Download of multiple files now possible
[phpeclipse.git] / archive / net.sourceforge.phpeclipse.wiki / src / net / sourceforge / phpeclipse / wiki / actions / mediawiki / post / PostJob.java
1 package net.sourceforge.phpeclipse.wiki.actions.mediawiki.post;
2
3 import java.io.IOException;
4 import java.io.InputStream;
5 import java.io.StringWriter;
6 import java.util.Date;
7 import java.util.HashMap;
8
9 import net.sourceforge.phpeclipse.wiki.actions.ProblemConsole;
10 import net.sourceforge.phpeclipse.wiki.actions.mediawiki.config.IWikipedia;
11 import net.sourceforge.phpeclipse.wiki.actions.mediawiki.connect.Content;
12 import net.sourceforge.phpeclipse.wiki.actions.mediawiki.connect.MediaWikiConnector;
13 import net.sourceforge.phpeclipse.wiki.actions.mediawiki.exceptions.MethodException;
14 import net.sourceforge.phpeclipse.wiki.actions.mediawiki.exceptions.UnexpectedAnswerException;
15 import net.sourceforge.phpeclipse.wiki.editor.WikiEditorPlugin;
16 import net.sourceforge.phpeclipse.wiki.internal.Configuration;
17 import net.sourceforge.phpeclipse.wiki.preferences.Util;
18 import net.sourceforge.phpeclipse.wiki.velocity.EditorText;
19
20 import org.apache.velocity.VelocityContext;
21 import org.apache.velocity.app.Velocity;
22 import org.eclipse.core.resources.IFile;
23 import org.eclipse.core.resources.WorkspaceJob;
24 import org.eclipse.core.runtime.CoreException;
25 import org.eclipse.core.runtime.IProgressMonitor;
26 import org.eclipse.core.runtime.IStatus;
27 import org.eclipse.core.runtime.Status;
28 import org.eclipse.core.runtime.jobs.Job;
29 import org.eclipse.ui.progress.IProgressConstants;
30
31 public class PostJob extends WorkspaceJob {
32   IFile[] files;
33
34   IWikipedia wikipedia;
35
36   Configuration configuration;
37
38   String user;
39
40   String password;
41
42   public PostJob(Configuration configuration, IWikipedia wikipedia, String user, String password, IFile[] files) {
43     super("Refresh Job");
44     this.files = files;
45     this.wikipedia = wikipedia;
46     this.user = user;
47     this.password = password;
48     this.configuration = configuration;
49   }
50
51   public String generateUrl(String template, String wikiname) {
52     EditorText text = new EditorText(null);
53     /* first, we init the runtime engine. Defaults are fine. */
54
55     try {
56       Velocity.init();
57
58       /* lets make a Context and put data into it */
59
60       VelocityContext context = new VelocityContext();
61
62       context.put("config", configuration);
63       text.clear();
64       text.setWikiname(wikiname);
65       context.put("text", text);
66
67       /* lets make our own string to render */
68       StringWriter w = new StringWriter();
69       w = new StringWriter();
70       Velocity.evaluate(context, w, "mystring", template);
71       return w.toString();
72
73     } catch (Exception e) {
74       // TODO Auto-generated catch block
75       e.printStackTrace();
76     }
77     return template;
78   }
79
80   private void selectWiki(String body, MediaWikiConnector connector, String actionUrl, String wikiName) {
81
82     try {
83       String url = generateUrl(configuration.getURL(), wikiName);
84
85       Date d = new Date();
86       Content content = new Content(String.valueOf(d.getTime()), body);
87
88       connector.store(wikipedia, actionUrl, wikiName, content, "", false, false);
89
90     } catch (Exception e) {
91       e.printStackTrace();
92       WikiEditorPlugin.getDefault()
93           .reportError("Exception occured: ", e.getMessage() + "\nSee stacktrace in /.metadata/.log file.");
94     }
95   }
96
97   public IStatus runInWorkspace(IProgressMonitor monitor) {
98     boolean success = false;
99     MediaWikiConnector connector = null;
100     InputStream is = null;
101     ProblemConsole console = new ProblemConsole();
102     String actionUrl = configuration.getURL();
103     if (actionUrl == null || actionUrl.equals("")) {
104       // fall back to default settings
105       actionUrl = wikipedia.getActionUrl();
106     }
107     try {
108       monitor.beginTask("Refresh Wikis", 100);
109       int partWork = 100 / files.length;
110       int work = 0;
111       int autoCreateIndex = -1;
112       StringBuffer buffer = new StringBuffer();
113       HashMap map = new HashMap();
114       String wikiTitle;
115       IFile file = null;
116
117       connector = new MediaWikiConnector();
118       success = connector.login(wikipedia, actionUrl, user, password, false);
119       if (success) {
120         for (int i = 0; i < files.length; i++) {
121           try {
122             file = files[i];
123             is = file.getContents();
124             String wikiName = Util.getFileWikiName(file);
125             String body = StoreWikipediaAction.getInputStreamAsString(is, wikipedia.getCharSet());
126             autoCreateIndex = body.indexOf(WikiEditorPlugin.AUTOMATICALLY_CREATED);
127             if (autoCreateIndex < 0) {
128               selectWiki(body, connector, actionUrl, wikiName);
129             } else {
130               console.println("File: " + file.getLocation().toString() + "\n==>no upload allowed; Wiki text contains string: "
131                   + WikiEditorPlugin.AUTOMATICALLY_CREATED);
132             }
133
134           } catch (CoreException e1) {
135             if (file != null) {
136               console.println("File: " + file.getLocation().toString() + "\n==>CoreException: "
137                   + e1.getMessage());
138             }
139           }
140           if (monitor.isCanceled()) {
141             return Status.CANCEL_STATUS;
142           }
143           work += partWork;
144           monitor.worked(work);
145         }
146       }
147
148       if (isModal(this)) {
149         // The progress dialog is still open show the message
150         console.reportError();
151       } else {
152         setProperty(IProgressConstants.KEEP_PROPERTY, Boolean.TRUE);
153         //            setProperty(IProgressConstants.ACTION_PROPERTY, getWikisCompletedAction());
154       }
155       return Status.OK_STATUS;
156       //        } catch(CoreException e) {
157       //          return e.getStatus();
158     } catch (IOException e) {
159       console.println("==>IOException: " + e.getMessage());
160     } catch (UnexpectedAnswerException e) {
161       console.println("==>UnexpectedAnswerException: " + e.getMessage());
162     } catch (MethodException e) {
163       console.println("==>HTTP-MethodException: " + e.getMessage());
164     } finally {
165       monitor.done();
166       if (success && connector != null) {
167         try {
168           connector.logout(wikipedia, actionUrl);
169         } catch (UnexpectedAnswerException e1) {
170           console.println("==>UnexpectedAnswerException: " + e1.getMessage());
171         } catch (MethodException e1) {
172           console.println("==>HTTP-MethodException: " + e1.getMessage());
173         }
174       }
175       if (is != null) {
176         try {
177           is.close();
178         } catch (IOException e1) {
179         }
180       }
181     }
182     if (isModal(this)) {
183       // The progress dialog is still open show the message
184       console.reportError();
185     }
186     return Status.CANCEL_STATUS;
187   }
188
189   public boolean isModal(Job job) {
190     Boolean isModal = (Boolean) job.getProperty(IProgressConstants.PROPERTY_IN_DIALOG);
191     if (isModal == null) {
192       return false;
193     }
194     return isModal.booleanValue();
195   }
196
197 }