8c5530cc7784e685e99b0bc7ce4bd756606f8584
[public/netxms.git] / src / java / netxms-eclipse / ServerConfig / src / org / netxms / ui / eclipse / serverconfig / views / ExportFileBuilder.java
1 /**
2 * NetXMS - open source network management system
3 * Copyright (C) 2003-2016 Victor Kirhenshtein
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
18 */
19 package org.netxms.ui.eclipse.serverconfig.views;
20
21 import java.io.FileOutputStream;
22 import java.io.OutputStream;
23 import java.io.OutputStreamWriter;
24 import java.net.HttpURLConnection;
25 import java.net.URL;
26 import java.net.URLConnection;
27 import java.util.HashMap;
28 import java.util.HashSet;
29 import java.util.List;
30 import java.util.Map;
31 import java.util.Set;
32 import java.util.UUID;
33 import org.eclipse.core.runtime.IProgressMonitor;
34 import org.eclipse.core.runtime.Platform;
35 import org.eclipse.jface.action.Action;
36 import org.eclipse.jface.action.IMenuManager;
37 import org.eclipse.jface.action.IToolBarManager;
38 import org.eclipse.jface.commands.ActionHandler;
39 import org.eclipse.jface.viewers.ArrayContentProvider;
40 import org.eclipse.jface.viewers.ILabelProvider;
41 import org.eclipse.jface.viewers.IStructuredSelection;
42 import org.eclipse.jface.viewers.TableViewer;
43 import org.eclipse.jface.window.Window;
44 import org.eclipse.swt.SWT;
45 import org.eclipse.swt.layout.GridData;
46 import org.eclipse.swt.layout.GridLayout;
47 import org.eclipse.swt.widgets.Composite;
48 import org.eclipse.swt.widgets.FileDialog;
49 import org.eclipse.ui.IActionBars;
50 import org.eclipse.ui.ISaveablePart;
51 import org.eclipse.ui.contexts.IContextService;
52 import org.eclipse.ui.forms.events.HyperlinkAdapter;
53 import org.eclipse.ui.forms.events.HyperlinkEvent;
54 import org.eclipse.ui.forms.widgets.FormToolkit;
55 import org.eclipse.ui.forms.widgets.ImageHyperlink;
56 import org.eclipse.ui.forms.widgets.ScrolledForm;
57 import org.eclipse.ui.forms.widgets.Section;
58 import org.eclipse.ui.forms.widgets.TableWrapData;
59 import org.eclipse.ui.forms.widgets.TableWrapLayout;
60 import org.eclipse.ui.handlers.IHandlerService;
61 import org.eclipse.ui.model.WorkbenchLabelProvider;
62 import org.eclipse.ui.part.ViewPart;
63 import org.netxms.base.NXCommon;
64 import org.netxms.client.NXCSession;
65 import org.netxms.client.Script;
66 import org.netxms.client.datacollection.DciSummaryTableDescriptor;
67 import org.netxms.client.events.EventGroup;
68 import org.netxms.client.events.EventObject;
69 import org.netxms.client.events.EventProcessingPolicyRule;
70 import org.netxms.client.events.EventTemplate;
71 import org.netxms.client.market.Repository;
72 import org.netxms.client.objects.AbstractObject;
73 import org.netxms.client.objects.Template;
74 import org.netxms.client.objecttools.ObjectTool;
75 import org.netxms.client.snmp.SnmpTrap;
76 import org.netxms.ui.eclipse.console.resources.SharedIcons;
77 import org.netxms.ui.eclipse.epp.dialogs.RuleSelectionDialog;
78 import org.netxms.ui.eclipse.eventmanager.dialogs.EventSelectionDialog;
79 import org.netxms.ui.eclipse.jobs.ConsoleJob;
80 import org.netxms.ui.eclipse.nxsl.dialogs.SelectScriptDialog;
81 import org.netxms.ui.eclipse.objectbrowser.dialogs.ObjectSelectionDialog;
82 import org.netxms.ui.eclipse.serverconfig.Activator;
83 import org.netxms.ui.eclipse.serverconfig.Messages;
84 import org.netxms.ui.eclipse.serverconfig.dialogs.ObjectToolSelectionDialog;
85 import org.netxms.ui.eclipse.serverconfig.dialogs.RepositorySelectionDialog;
86 import org.netxms.ui.eclipse.serverconfig.dialogs.SelectSnmpTrapDialog;
87 import org.netxms.ui.eclipse.serverconfig.dialogs.SummaryTableSelectionDialog;
88 import org.netxms.ui.eclipse.serverconfig.dialogs.helpers.TrapListLabelProvider;
89 import org.netxms.ui.eclipse.serverconfig.views.helpers.RuleComparator;
90 import org.netxms.ui.eclipse.serverconfig.views.helpers.RuleLabelProvider;
91 import org.netxms.ui.eclipse.serverconfig.views.helpers.ScriptComparator;
92 import org.netxms.ui.eclipse.serverconfig.views.helpers.ScriptLabelProvider;
93 import org.netxms.ui.eclipse.serverconfig.views.helpers.SummaryTablesComparator;
94 import org.netxms.ui.eclipse.serverconfig.views.helpers.SummaryTablesLabelProvider;
95 import org.netxms.ui.eclipse.serverconfig.views.helpers.ToolComparator;
96 import org.netxms.ui.eclipse.serverconfig.views.helpers.ToolLabelProvider;
97 import org.netxms.ui.eclipse.shared.ConsoleSharedData;
98 import org.netxms.ui.eclipse.tools.ObjectLabelComparator;
99 import org.netxms.ui.eclipse.widgets.LabeledText;
100
101 /**
102 * Export file builder
103 */
104 public class ExportFileBuilder extends ViewPart implements ISaveablePart
105 {
106 public static final String ID = "org.netxms.ui.eclipse.serverconfig.views.ExportFileBuilder"; //$NON-NLS-1$
107
108 private NXCSession session = (NXCSession)ConsoleSharedData.getSession();
109 private FormToolkit toolkit;
110 private ScrolledForm form;
111 private LabeledText description;
112 private TableViewer templateViewer;
113 private TableViewer eventViewer;
114 private TableViewer trapViewer;
115 private TableViewer ruleViewer;
116 private TableViewer scriptViewer;
117 private TableViewer toolsViewer;
118 private TableViewer summaryTableViewer;
119 private Action actionSave;
120 private Action actionPublish;
121 private Map<Long, EventObject> events = new HashMap<Long, EventObject>();
122 private Map<Long, Template> templates = new HashMap<Long, Template>();
123 private Map<Long, SnmpTrap> traps = new HashMap<Long, SnmpTrap>();
124 private Map<UUID, EventProcessingPolicyRule> rules = new HashMap<UUID, EventProcessingPolicyRule>();
125 private Map<Long, Script> scripts = new HashMap<Long, Script>();
126 private Map<Long, ObjectTool> tools = new HashMap<Long, ObjectTool>();
127 private Map<Integer, DciSummaryTableDescriptor> summaryTables = new HashMap<Integer, DciSummaryTableDescriptor>();
128 private boolean modified = false;
129 private List<SnmpTrap> snmpTrapCache = null;
130 private List<EventProcessingPolicyRule> rulesCache = null;
131 private String exportFileName = null;
132
133 /* (non-Javadoc)
134 * @see org.eclipse.ui.part.WorkbenchPart#createPartControl(org.eclipse.swt.widgets.Composite)
135 */
136 @Override
137 public void createPartControl(Composite parent)
138 {
139 // Initiate loading of required plugins if they was not loaded yet
140 try
141 {
142 Platform.getAdapterManager().loadAdapter(new EventTemplate(0), "org.eclipse.ui.model.IWorkbenchAdapter"); //$NON-NLS-1$
143 Platform.getAdapterManager().loadAdapter(session.getTopLevelObjects()[0], "org.eclipse.ui.model.IWorkbenchAdapter"); //$NON-NLS-1$
144 }
145 catch(Exception e)
146 {
147 }
148
149 toolkit = new FormToolkit(getSite().getShell().getDisplay());
150 form = toolkit.createScrolledForm(parent);
151 form.setText(Messages.get().ExportFileBuilder_FormTitle);
152
153 TableWrapLayout layout = new TableWrapLayout();
154 layout.numColumns = 3;
155 form.getBody().setLayout(layout);
156
157 createFileSection();
158 createTemplateSection();
159 createEventSection();
160 createTrapSection();
161 createRuleSection();
162 createScriptSection();
163 createToolsSection();
164 createSummaryTablesSection();
165
166 form.reflow(true);
167
168 activateContext();
169 createActions();
170 contributeToActionBars();
171 }
172
173 /**
174 * Activate context
175 */
176 private void activateContext()
177 {
178 IContextService contextService = (IContextService)getSite().getService(IContextService.class);
179 if (contextService != null)
180 {
181 contextService.activateContext("org.netxms.ui.eclipse.serverconfig.context.ExportFileBuilder"); //$NON-NLS-1$
182 }
183 }
184
185 /**
186 * Create "File" section
187 */
188 private void createFileSection()
189 {
190 Section section = toolkit.createSection(form.getBody(), Section.TITLE_BAR);
191 section.setText(Messages.get().ExportFileBuilder_SectionFile);
192 TableWrapData td = new TableWrapData();
193 td.align = TableWrapData.FILL;
194 td.grabHorizontal = true;
195 td.colspan = 2;
196 section.setLayoutData(td);
197
198 Composite clientArea = toolkit.createComposite(section);
199 GridLayout layout = new GridLayout();
200 layout.numColumns = 2;
201 clientArea.setLayout(layout);
202 section.setClient(clientArea);
203
204 description = new LabeledText(clientArea, SWT.NONE, SWT.MULTI | SWT.BORDER | SWT.H_SCROLL | SWT.V_SCROLL);
205 toolkit.adapt(description);
206 description.setLabel(Messages.get().ExportFileBuilder_Description);
207 GridData gd = new GridData();
208 gd.horizontalAlignment = SWT.FILL;
209 gd.verticalAlignment = SWT.FILL;
210 gd.grabExcessHorizontalSpace = true;
211 gd.grabExcessVerticalSpace = true;
212 gd.heightHint = 200;
213 description.setLayoutData(gd);
214 }
215
216 /**
217 * Create "Templates" section
218 */
219 private void createTemplateSection()
220 {
221 Section section = toolkit.createSection(form.getBody(), Section.TITLE_BAR);
222 section.setText(Messages.get().ExportFileBuilder_SectionTemplates);
223 TableWrapData td = new TableWrapData();
224 td.align = TableWrapData.FILL;
225 td.grabHorizontal = true;
226 section.setLayoutData(td);
227
228 Composite clientArea = toolkit.createComposite(section);
229 GridLayout layout = new GridLayout();
230 layout.numColumns = 2;
231 clientArea.setLayout(layout);
232 section.setClient(clientArea);
233
234 templateViewer = new TableViewer(clientArea, SWT.FULL_SELECTION | SWT.MULTI | SWT.BORDER);
235 toolkit.adapt(templateViewer.getTable());
236 GridData gd = new GridData();
237 gd.horizontalAlignment = SWT.FILL;
238 gd.grabExcessHorizontalSpace = true;
239 gd.verticalAlignment = SWT.FILL;
240 gd.grabExcessVerticalSpace = true;
241 gd.heightHint = 200;
242 gd.verticalSpan = 2;
243 templateViewer.getTable().setLayoutData(gd);
244 templateViewer.setContentProvider(new ArrayContentProvider());
245 templateViewer.setLabelProvider(new WorkbenchLabelProvider());
246 templateViewer.setComparator(new ObjectLabelComparator((ILabelProvider)templateViewer.getLabelProvider()));
247 templateViewer.getTable().setSortDirection(SWT.UP);
248
249 final ImageHyperlink linkAdd = toolkit.createImageHyperlink(clientArea, SWT.NONE);
250 linkAdd.setText(Messages.get().ExportFileBuilder_Add);
251 linkAdd.setImage(SharedIcons.IMG_ADD_OBJECT);
252 gd = new GridData();
253 gd.verticalAlignment = SWT.TOP;
254 linkAdd.setLayoutData(gd);
255 linkAdd.addHyperlinkListener(new HyperlinkAdapter() {
256 @Override
257 public void linkActivated(HyperlinkEvent e)
258 {
259 addTemplates();
260 }
261 });
262
263 final ImageHyperlink linkRemove = toolkit.createImageHyperlink(clientArea, SWT.NONE);
264 linkRemove.setText(Messages.get().ExportFileBuilder_Remove);
265 linkRemove.setImage(SharedIcons.IMG_DELETE_OBJECT);
266 gd = new GridData();
267 gd.verticalAlignment = SWT.TOP;
268 linkRemove.setLayoutData(gd);
269 linkRemove.addHyperlinkListener(new HyperlinkAdapter() {
270 @Override
271 public void linkActivated(HyperlinkEvent e)
272 {
273 removeObjects(templateViewer, templates);
274 }
275 });
276 }
277
278 /**
279 * Create "Events" section
280 */
281 private void createEventSection()
282 {
283 Section section = toolkit.createSection(form.getBody(), Section.TITLE_BAR);
284 section.setText(Messages.get().ExportFileBuilder_SectionEvents);
285 TableWrapData td = new TableWrapData();
286 td.align = TableWrapData.FILL;
287 td.grabHorizontal = true;
288 section.setLayoutData(td);
289
290 Composite clientArea = toolkit.createComposite(section);
291 GridLayout layout = new GridLayout();
292 layout.numColumns = 2;
293 clientArea.setLayout(layout);
294 section.setClient(clientArea);
295
296 eventViewer = new TableViewer(clientArea, SWT.FULL_SELECTION | SWT.MULTI | SWT.BORDER);
297 toolkit.adapt(eventViewer.getTable());
298 GridData gd = new GridData();
299 gd.horizontalAlignment = SWT.FILL;
300 gd.grabExcessHorizontalSpace = true;
301 gd.verticalAlignment = SWT.FILL;
302 gd.grabExcessVerticalSpace = true;
303 gd.heightHint = 200;
304 gd.verticalSpan = 2;
305 eventViewer.getTable().setLayoutData(gd);
306 eventViewer.setContentProvider(new ArrayContentProvider());
307 eventViewer.setLabelProvider(new WorkbenchLabelProvider());
308 eventViewer.setComparator(new ObjectLabelComparator((ILabelProvider)eventViewer.getLabelProvider()));
309 eventViewer.getTable().setSortDirection(SWT.UP);
310
311 final ImageHyperlink linkAdd = toolkit.createImageHyperlink(clientArea, SWT.NONE);
312 linkAdd.setText(Messages.get().ExportFileBuilder_Add);
313 linkAdd.setImage(SharedIcons.IMG_ADD_OBJECT);
314 gd = new GridData();
315 gd.verticalAlignment = SWT.TOP;
316 linkAdd.setLayoutData(gd);
317 linkAdd.addHyperlinkListener(new HyperlinkAdapter() {
318 @Override
319 public void linkActivated(HyperlinkEvent e)
320 {
321 addEvents();
322 }
323 });
324
325 final ImageHyperlink linkRemove = toolkit.createImageHyperlink(clientArea, SWT.NONE);
326 linkRemove.setText(Messages.get().ExportFileBuilder_Remove);
327 linkRemove.setImage(SharedIcons.IMG_DELETE_OBJECT);
328 gd = new GridData();
329 gd.verticalAlignment = SWT.TOP;
330 linkRemove.setLayoutData(gd);
331 linkRemove.addHyperlinkListener(new HyperlinkAdapter() {
332 @Override
333 public void linkActivated(HyperlinkEvent e)
334 {
335 removeObjects(eventViewer, events);
336 }
337 });
338 }
339
340 /**
341 * Create "SNMP Traps" section
342 */
343 private void createTrapSection()
344 {
345 Section section = toolkit.createSection(form.getBody(), Section.TITLE_BAR);
346 section.setText(Messages.get().ExportFileBuilder_SectionTraps);
347 TableWrapData td = new TableWrapData();
348 td.align = TableWrapData.FILL;
349 td.grabHorizontal = true;
350 section.setLayoutData(td);
351
352 Composite clientArea = toolkit.createComposite(section);
353 GridLayout layout = new GridLayout();
354 layout.numColumns = 2;
355 clientArea.setLayout(layout);
356 section.setClient(clientArea);
357
358 trapViewer = new TableViewer(clientArea, SWT.FULL_SELECTION | SWT.MULTI | SWT.BORDER);
359 toolkit.adapt(trapViewer.getTable());
360 GridData gd = new GridData();
361 gd.horizontalAlignment = SWT.FILL;
362 gd.grabExcessHorizontalSpace = true;
363 gd.verticalAlignment = SWT.FILL;
364 gd.grabExcessVerticalSpace = true;
365 gd.heightHint = 200;
366 gd.verticalSpan = 2;
367 trapViewer.getTable().setLayoutData(gd);
368 trapViewer.setContentProvider(new ArrayContentProvider());
369 trapViewer.setLabelProvider(new TrapListLabelProvider());
370 trapViewer.setComparator(new ObjectLabelComparator((ILabelProvider)eventViewer.getLabelProvider()));
371 trapViewer.getTable().setSortDirection(SWT.UP);
372
373 final ImageHyperlink linkAdd = toolkit.createImageHyperlink(clientArea, SWT.NONE);
374 linkAdd.setText(Messages.get().ExportFileBuilder_Add);
375 linkAdd.setImage(SharedIcons.IMG_ADD_OBJECT);
376 gd = new GridData();
377 gd.verticalAlignment = SWT.TOP;
378 linkAdd.setLayoutData(gd);
379 linkAdd.addHyperlinkListener(new HyperlinkAdapter() {
380 @Override
381 public void linkActivated(HyperlinkEvent e)
382 {
383 if (snmpTrapCache == null)
384 {
385 new ConsoleJob(Messages.get().ExportFileBuilder_TrapsLoadJobName, ExportFileBuilder.this, Activator.PLUGIN_ID, null) {
386 @Override
387 protected void runInternal(IProgressMonitor monitor) throws Exception
388 {
389 snmpTrapCache = session.getSnmpTrapsConfigurationSummary();
390 runInUIThread(new Runnable() {
391 @Override
392 public void run()
393 {
394 addTraps();
395 }
396 });
397 }
398
399 @Override
400 protected String getErrorMessage()
401 {
402 return Messages.get().ExportFileBuilder_TrapsLoadJobError;
403 }
404 }.start();
405 }
406 else
407 {
408 addTraps();
409 }
410 }
411 });
412
413 final ImageHyperlink linkRemove = toolkit.createImageHyperlink(clientArea, SWT.NONE);
414 linkRemove.setText(Messages.get().ExportFileBuilder_Remove);
415 linkRemove.setImage(SharedIcons.IMG_DELETE_OBJECT);
416 gd = new GridData();
417 gd.verticalAlignment = SWT.TOP;
418 linkRemove.setLayoutData(gd);
419 linkRemove.addHyperlinkListener(new HyperlinkAdapter() {
420 @Override
421 public void linkActivated(HyperlinkEvent e)
422 {
423 removeObjects(trapViewer, traps);
424 }
425 });
426 }
427
428 /**
429 * Create "Rules" section
430 */
431 private void createRuleSection()
432 {
433 Section section = toolkit.createSection(form.getBody(), Section.TITLE_BAR);
434 section.setText(Messages.get().ExportFileBuilder_SectionRules);
435 TableWrapData td = new TableWrapData();
436 td.align = TableWrapData.FILL;
437 td.grabHorizontal = true;
438 section.setLayoutData(td);
439
440 Composite clientArea = toolkit.createComposite(section);
441 GridLayout layout = new GridLayout();
442 layout.numColumns = 2;
443 clientArea.setLayout(layout);
444 section.setClient(clientArea);
445
446 ruleViewer = new TableViewer(clientArea, SWT.FULL_SELECTION | SWT.MULTI | SWT.BORDER);
447 toolkit.adapt(ruleViewer.getTable());
448 GridData gd = new GridData();
449 gd.horizontalAlignment = SWT.FILL;
450 gd.grabExcessHorizontalSpace = true;
451 gd.verticalAlignment = SWT.FILL;
452 gd.grabExcessVerticalSpace = true;
453 gd.heightHint = 200;
454 gd.verticalSpan = 2;
455 ruleViewer.getTable().setLayoutData(gd);
456 ruleViewer.setContentProvider(new ArrayContentProvider());
457 ruleViewer.setLabelProvider(new RuleLabelProvider());
458 ruleViewer.setComparator(new RuleComparator());
459 ruleViewer.getTable().setSortDirection(SWT.UP);
460
461 final ImageHyperlink linkAdd = toolkit.createImageHyperlink(clientArea, SWT.NONE);
462 linkAdd.setText(Messages.get().ExportFileBuilder_Add);
463 linkAdd.setImage(SharedIcons.IMG_ADD_OBJECT);
464 gd = new GridData();
465 gd.verticalAlignment = SWT.TOP;
466 linkAdd.setLayoutData(gd);
467 linkAdd.addHyperlinkListener(new HyperlinkAdapter() {
468 @Override
469 public void linkActivated(HyperlinkEvent e)
470 {
471 if (rulesCache == null)
472 {
473 new ConsoleJob(Messages.get().ExportFileBuilder_EPPLoadJobName, ExportFileBuilder.this, Activator.PLUGIN_ID, null) {
474 @Override
475 protected void runInternal(IProgressMonitor monitor) throws Exception
476 {
477 rulesCache = session.getEventProcessingPolicy().getRules();
478 runInUIThread(new Runnable() {
479 @Override
480 public void run()
481 {
482 addRules();
483 }
484 });
485 }
486
487 @Override
488 protected String getErrorMessage()
489 {
490 return Messages.get().ExportFileBuilder_EPPLoadJobError;
491 }
492 }.start();
493 }
494 else
495 {
496 addRules();
497 }
498 }
499 });
500
501 final ImageHyperlink linkRemove = toolkit.createImageHyperlink(clientArea, SWT.NONE);
502 linkRemove.setText(Messages.get().ExportFileBuilder_Remove);
503 linkRemove.setImage(SharedIcons.IMG_DELETE_OBJECT);
504 gd = new GridData();
505 gd.verticalAlignment = SWT.TOP;
506 linkRemove.setLayoutData(gd);
507 linkRemove.addHyperlinkListener(new HyperlinkAdapter() {
508 @Override
509 public void linkActivated(HyperlinkEvent e)
510 {
511 removeObjects(ruleViewer, rules);
512 }
513 });
514 }
515
516 /**
517 * Create "Scripts" section
518 */
519 private void createScriptSection()
520 {
521 Section section = toolkit.createSection(form.getBody(), Section.TITLE_BAR);
522 section.setText("Scripts");
523 TableWrapData td = new TableWrapData();
524 td.align = TableWrapData.FILL;
525 td.grabHorizontal = true;
526 section.setLayoutData(td);
527
528 Composite clientArea = toolkit.createComposite(section);
529 GridLayout layout = new GridLayout();
530 layout.numColumns = 2;
531 clientArea.setLayout(layout);
532 section.setClient(clientArea);
533
534 scriptViewer = new TableViewer(clientArea, SWT.FULL_SELECTION | SWT.MULTI | SWT.BORDER);
535 toolkit.adapt(ruleViewer.getTable());
536 GridData gd = new GridData();
537 gd.horizontalAlignment = SWT.FILL;
538 gd.grabExcessHorizontalSpace = true;
539 gd.verticalAlignment = SWT.FILL;
540 gd.grabExcessVerticalSpace = true;
541 gd.heightHint = 200;
542 gd.verticalSpan = 2;
543 scriptViewer.getTable().setLayoutData(gd);
544 scriptViewer.setContentProvider(new ArrayContentProvider());
545 scriptViewer.setLabelProvider(new ScriptLabelProvider());
546 scriptViewer.setComparator(new ScriptComparator());
547 scriptViewer.getTable().setSortDirection(SWT.UP);
548
549 final ImageHyperlink linkAdd = toolkit.createImageHyperlink(clientArea, SWT.NONE);
550 linkAdd.setText(Messages.get().ExportFileBuilder_Add);
551 linkAdd.setImage(SharedIcons.IMG_ADD_OBJECT);
552 gd = new GridData();
553 gd.verticalAlignment = SWT.TOP;
554 linkAdd.setLayoutData(gd);
555 linkAdd.addHyperlinkListener(new HyperlinkAdapter() {
556 @Override
557 public void linkActivated(HyperlinkEvent e)
558 {
559 addScripts();
560 }
561 });
562
563 final ImageHyperlink linkRemove = toolkit.createImageHyperlink(clientArea, SWT.NONE);
564 linkRemove.setText(Messages.get().ExportFileBuilder_Remove);
565 linkRemove.setImage(SharedIcons.IMG_DELETE_OBJECT);
566 gd = new GridData();
567 gd.verticalAlignment = SWT.TOP;
568 linkRemove.setLayoutData(gd);
569 linkRemove.addHyperlinkListener(new HyperlinkAdapter() {
570 @Override
571 public void linkActivated(HyperlinkEvent e)
572 {
573 removeObjects(scriptViewer, scripts);
574 }
575 });
576 }
577
578 /**
579 * Create "Object Tools" section
580 */
581 private void createToolsSection()
582 {
583 Section section = toolkit.createSection(form.getBody(), Section.TITLE_BAR);
584 section.setText("Object Tools");
585 TableWrapData td = new TableWrapData();
586 td.align = TableWrapData.FILL;
587 td.grabHorizontal = true;
588 section.setLayoutData(td);
589
590 Composite clientArea = toolkit.createComposite(section);
591 GridLayout layout = new GridLayout();
592 layout.numColumns = 2;
593 clientArea.setLayout(layout);
594 section.setClient(clientArea);
595
596 toolsViewer = new TableViewer(clientArea, SWT.FULL_SELECTION | SWT.MULTI | SWT.BORDER);
597 toolkit.adapt(ruleViewer.getTable());
598 GridData gd = new GridData();
599 gd.horizontalAlignment = SWT.FILL;
600 gd.grabExcessHorizontalSpace = true;
601 gd.verticalAlignment = SWT.FILL;
602 gd.grabExcessVerticalSpace = true;
603 gd.heightHint = 200;
604 gd.verticalSpan = 2;
605 toolsViewer.getTable().setLayoutData(gd);
606 toolsViewer.setContentProvider(new ArrayContentProvider());
607 toolsViewer.setLabelProvider(new ToolLabelProvider());
608 toolsViewer.setComparator(new ToolComparator());
609 toolsViewer.getTable().setSortDirection(SWT.UP);
610
611 final ImageHyperlink linkAdd = toolkit.createImageHyperlink(clientArea, SWT.NONE);
612 linkAdd.setText(Messages.get().ExportFileBuilder_Add);
613 linkAdd.setImage(SharedIcons.IMG_ADD_OBJECT);
614 gd = new GridData();
615 gd.verticalAlignment = SWT.TOP;
616 linkAdd.setLayoutData(gd);
617 linkAdd.addHyperlinkListener(new HyperlinkAdapter() {
618 @Override
619 public void linkActivated(HyperlinkEvent e)
620 {
621 addObjectTools();
622 }
623 });
624
625 final ImageHyperlink linkRemove = toolkit.createImageHyperlink(clientArea, SWT.NONE);
626 linkRemove.setText(Messages.get().ExportFileBuilder_Remove);
627 linkRemove.setImage(SharedIcons.IMG_DELETE_OBJECT);
628 gd = new GridData();
629 gd.verticalAlignment = SWT.TOP;
630 linkRemove.setLayoutData(gd);
631 linkRemove.addHyperlinkListener(new HyperlinkAdapter() {
632 @Override
633 public void linkActivated(HyperlinkEvent e)
634 {
635 removeObjects(toolsViewer, tools);
636 }
637 });
638 }
639
640 /**
641 * Create "Summary Tables" section
642 */
643 private void createSummaryTablesSection()
644 {
645 Section section = toolkit.createSection(form.getBody(), Section.TITLE_BAR);
646 section.setText("DCI Summary Tables");
647 TableWrapData td = new TableWrapData();
648 td.align = TableWrapData.FILL;
649 td.grabHorizontal = true;
650 section.setLayoutData(td);
651
652 Composite clientArea = toolkit.createComposite(section);
653 GridLayout layout = new GridLayout();
654 layout.numColumns = 2;
655 clientArea.setLayout(layout);
656 section.setClient(clientArea);
657
658 summaryTableViewer = new TableViewer(clientArea, SWT.FULL_SELECTION | SWT.MULTI | SWT.BORDER);
659 toolkit.adapt(ruleViewer.getTable());
660 GridData gd = new GridData();
661 gd.horizontalAlignment = SWT.FILL;
662 gd.grabExcessHorizontalSpace = true;
663 gd.verticalAlignment = SWT.FILL;
664 gd.grabExcessVerticalSpace = true;
665 gd.heightHint = 200;
666 gd.verticalSpan = 2;
667 summaryTableViewer.getTable().setLayoutData(gd);
668 summaryTableViewer.setContentProvider(new ArrayContentProvider());
669 summaryTableViewer.setLabelProvider(new SummaryTablesLabelProvider());
670 summaryTableViewer.setComparator(new SummaryTablesComparator());
671 summaryTableViewer.getTable().setSortDirection(SWT.UP);
672
673 final ImageHyperlink linkAdd = toolkit.createImageHyperlink(clientArea, SWT.NONE);
674 linkAdd.setText(Messages.get().ExportFileBuilder_Add);
675 linkAdd.setImage(SharedIcons.IMG_ADD_OBJECT);
676 gd = new GridData();
677 gd.verticalAlignment = SWT.TOP;
678 linkAdd.setLayoutData(gd);
679 linkAdd.addHyperlinkListener(new HyperlinkAdapter() {
680 @Override
681 public void linkActivated(HyperlinkEvent e)
682 {
683 addSummaryTables();
684 }
685 });
686
687 final ImageHyperlink linkRemove = toolkit.createImageHyperlink(clientArea, SWT.NONE);
688 linkRemove.setText(Messages.get().ExportFileBuilder_Remove);
689 linkRemove.setImage(SharedIcons.IMG_DELETE_OBJECT);
690 gd = new GridData();
691 gd.verticalAlignment = SWT.TOP;
692 linkRemove.setLayoutData(gd);
693 linkRemove.addHyperlinkListener(new HyperlinkAdapter() {
694 @Override
695 public void linkActivated(HyperlinkEvent e)
696 {
697 removeObjects(summaryTableViewer, summaryTables);
698 }
699 });
700 }
701
702 /**
703 * Create actions
704 */
705 private void createActions()
706 {
707 final IHandlerService handlerService = (IHandlerService)getSite().getService(IHandlerService.class);
708
709 actionSave = new Action(Messages.get().ExportFileBuilder_Save, SharedIcons.SAVE) {
710 @Override
711 public void run()
712 {
713 save();
714 }
715 };
716 actionSave.setActionDefinitionId("org.netxms.ui.eclipse.serverconfig.commands.save_exported_config"); //$NON-NLS-1$
717 handlerService.activateHandler(actionSave.getActionDefinitionId(), new ActionHandler(actionSave));
718
719 actionPublish = new Action("&Publish...", Activator.getImageDescriptor("icons/publish.gif")) {
720 @Override
721 public void run()
722 {
723 publish();
724 }
725 };
726 actionPublish.setActionDefinitionId("org.netxms.ui.eclipse.serverconfig.commands.publish_config"); //$NON-NLS-1$
727 handlerService.activateHandler(actionPublish.getActionDefinitionId(), new ActionHandler(actionPublish));
728 }
729
730 /**
731 * Contribute actions to action bar
732 */
733 private void contributeToActionBars()
734 {
735 IActionBars bars = getViewSite().getActionBars();
736 fillLocalPullDown(bars.getMenuManager());
737 fillLocalToolBar(bars.getToolBarManager());
738 }
739
740 /**
741 * Fill local pull-down menu
742 *
743 * @param manager
744 * Menu manager for pull-down menu
745 */
746 private void fillLocalPullDown(IMenuManager manager)
747 {
748 manager.add(actionSave);
749 manager.add(actionPublish);
750 }
751
752 /**
753 * Fill local tool bar
754 *
755 * @param manager
756 * Menu manager for local toolbar
757 */
758 private void fillLocalToolBar(IToolBarManager manager)
759 {
760 manager.add(actionSave);
761 manager.add(actionPublish);
762 }
763
764 /* (non-Javadoc)
765 * @see org.eclipse.ui.part.WorkbenchPart#setFocus()
766 */
767 @Override
768 public void setFocus()
769 {
770 form.setFocus();
771 }
772
773 /**
774 * Mark view as modified
775 */
776 private void setModified()
777 {
778 if (!modified)
779 {
780 modified = true;
781 firePropertyChange(PROP_DIRTY);
782 }
783 }
784
785 /**
786 * Publish configuration
787 */
788 private void publish()
789 {
790 new ConsoleJob("Get list of configured repositories", this, Activator.PLUGIN_ID, null) {
791 @Override
792 protected void runInternal(IProgressMonitor monitor) throws Exception
793 {
794 final List<Repository> repositories = session.getRepositories();
795 runInUIThread(new Runnable() {
796 @Override
797 public void run()
798 {
799 publishStage2(repositories);
800 }
801 });
802 }
803
804 @Override
805 protected String getErrorMessage()
806 {
807 return "Cannot get list of configured repositories";
808 }
809 }.start();
810 }
811
812 /**
813 * Publish configuration - stage 2
814 *
815 * @param repositories
816 */
817 private void publishStage2(List<Repository> repositories)
818 {
819 RepositorySelectionDialog dlg = new RepositorySelectionDialog(getSite().getShell(), repositories);
820 if (dlg.open() != Window.OK)
821 return;
822
823 final Repository repository = dlg.getSelection();
824 doExport(new ExportCompletionHandler() {
825 @Override
826 public void exportCompleted(final String xml)
827 {
828 new ConsoleJob("Publish configuration", ExportFileBuilder.this, Activator.PLUGIN_ID, null) {
829 @Override
830 protected void runInternal(IProgressMonitor monitor) throws Exception
831 {
832 URL url = new URL(repository.getUrl() + "/rest-api/push-export?accessToken=" + repository.getAuthToken());
833 URLConnection conn = url.openConnection();
834 if (!(conn instanceof HttpURLConnection))
835 {
836 throw new Exception("Unsupported URL type");
837 }
838 ((HttpURLConnection)conn).setRequestMethod("POST");
839 ((HttpURLConnection)conn).setRequestProperty("User-Agent", "NetXMS Console/" + NXCommon.VERSION);
840 ((HttpURLConnection)conn).setRequestProperty("Content-Type", "application/xml; charset=utf-8");
841 ((HttpURLConnection)conn).setDoOutput(true);
842 ((HttpURLConnection)conn).setAllowUserInteraction(false);
843 ((HttpURLConnection)conn).setUseCaches(false);
844
845 OutputStream out = conn.getOutputStream();
846 try
847 {
848 out.write(xml.getBytes("UTF-8"));
849 out.flush();
850
851 int responseCode = ((HttpURLConnection)conn).getResponseCode();
852 Activator.logInfo("Publish config: url=" + url.toString() + " response=" + responseCode);
853 if (responseCode != 200)
854 {
855 throw new Exception(String.format("HTTP error %d", responseCode));
856 }
857 }
858 finally
859 {
860 out.close();
861 }
862 }
863
864 @Override
865 protected String getErrorMessage()
866 {
867 return "Cannot publish configuration to repository";
868 }
869 }.start();
870 }
871 });
872 }
873
874 /**
875 * Do export operation and call completion handler when done
876 *
877 * @param completionHandler
878 */
879 private void doExport(final ExportCompletionHandler completionHandler)
880 {
881 final long[] eventList = new long[events.size()];
882 int i = 0;
883 for(EventObject o : events.values())
884 eventList[i++] = o.getCode();
885
886 final long[] templateList = new long[templates.size()];
887 i = 0;
888 for(Template t : templates.values())
889 templateList[i++] = t.getObjectId();
890
891 final long[] trapList = new long[traps.size()];
892 i = 0;
893 for(SnmpTrap t : traps.values())
894 trapList[i++] = t.getId();
895
896 final UUID[] ruleList = new UUID[rules.size()];
897 i = 0;
898 for(EventProcessingPolicyRule r : rules.values())
899 ruleList[i++] = r.getGuid();
900
901 final long[] scriptList = new long[scripts.size()];
902 i = 0;
903 for(Script s : scripts.values())
904 scriptList[i++] = s.getId();
905
906 final long[] toolList = new long[tools.size()];
907 i = 0;
908 for(ObjectTool t : tools.values())
909 toolList[i++] = t.getId();
910
911 final long[] summaryTableList = new long[summaryTables.size()];
912 i = 0;
913 for(DciSummaryTableDescriptor t : summaryTables.values())
914 summaryTableList[i++] = t.getId();
915
916 final String descriptionText = description.getText();
917
918 new ConsoleJob(Messages.get().ExportFileBuilder_ExportJobName, this, Activator.PLUGIN_ID, null) {
919 @Override
920 protected void runInternal(IProgressMonitor monitor) throws Exception
921 {
922 final String xml = session.exportConfiguration(descriptionText, eventList, trapList, templateList, ruleList, scriptList, toolList, summaryTableList);
923 runInUIThread(new Runnable() {
924 @Override
925 public void run()
926 {
927 completionHandler.exportCompleted(xml);
928 }
929 });
930 }
931
932 @Override
933 protected String getErrorMessage()
934 {
935 return Messages.get().ExportFileBuilder_ExportJobError;
936 }
937 }.start();
938 }
939
940 /**
941 * Save exported configuration to file
942 */
943 private void save()
944 {
945 doExport(new ExportCompletionHandler() {
946 @Override
947 public void exportCompleted(final String xml)
948 {
949 FileDialog dlg = new FileDialog(getSite().getShell(), SWT.SAVE);
950 dlg.setFilterExtensions(new String[] { "*.xml", "*.*" }); //$NON-NLS-1$ //$NON-NLS-2$
951 dlg.setFilterNames(new String[] { Messages.get().ConfigurationImportDialog_FileTypeXML, Messages.get().ConfigurationImportDialog_FileTypeAll });
952 dlg.setOverwrite(true);
953 dlg.setFileName(exportFileName);
954 final String fileName = dlg.open();
955 if (fileName != null)
956 {
957 exportFileName = fileName;
958 new ConsoleJob("Save exported configuration", ExportFileBuilder.this, Activator.PLUGIN_ID, null) {
959 @Override
960 protected void runInternal(IProgressMonitor monitor) throws Exception
961 {
962 OutputStreamWriter out = new OutputStreamWriter(new FileOutputStream(fileName), "UTF-8"); //$NON-NLS-1$
963 try
964 {
965 out.write(xml);
966 }
967 finally
968 {
969 out.close();
970 }
971 runInUIThread(new Runnable() {
972 @Override
973 public void run()
974 {
975 modified = false;
976 firePropertyChange(PROP_DIRTY);
977 }
978 });
979 }
980
981 @Override
982 protected String getErrorMessage()
983 {
984 return "Cannot save exported configuration to file";
985 }
986 }.start();
987 }
988 }
989 });
990 }
991
992 /* (non-Javadoc)
993 * @see org.eclipse.ui.ISaveablePart#doSave(org.eclipse.core.runtime.IProgressMonitor)
994 */
995 @Override
996 public void doSave(IProgressMonitor monitor)
997 {
998 getSite().getShell().getDisplay().syncExec(new Runnable() {
999 @Override
1000 public void run()
1001 {
1002 save();
1003 }
1004 });
1005 }
1006
1007 /* (non-Javadoc)
1008 * @see org.eclipse.ui.ISaveablePart#doSaveAs()
1009 */
1010 @Override
1011 public void doSaveAs()
1012 {
1013 }
1014
1015 /* (non-Javadoc)
1016 * @see org.eclipse.ui.ISaveablePart#isDirty()
1017 */
1018 @Override
1019 public boolean isDirty()
1020 {
1021 return modified;
1022 }
1023
1024 /* (non-Javadoc)
1025 * @see org.eclipse.ui.ISaveablePart#isSaveAsAllowed()
1026 */
1027 @Override
1028 public boolean isSaveAsAllowed()
1029 {
1030 return false;
1031 }
1032
1033 /* (non-Javadoc)
1034 * @see org.eclipse.ui.ISaveablePart#isSaveOnCloseNeeded()
1035 */
1036 @Override
1037 public boolean isSaveOnCloseNeeded()
1038 {
1039 return modified;
1040 }
1041
1042 /**
1043 * @param o
1044 * @return
1045 */
1046 private static Object keyFromObject(Object o)
1047 {
1048 if (o instanceof Template)
1049 return ((Template)o).getObjectId();
1050 if (o instanceof EventTemplate)
1051 return ((EventTemplate)o).getCode();
1052 if (o instanceof SnmpTrap)
1053 return ((SnmpTrap)o).getId();
1054 if (o instanceof Script)
1055 return ((Script)o).getId();
1056 if (o instanceof ObjectTool)
1057 return ((ObjectTool)o).getId();
1058 if (o instanceof EventProcessingPolicyRule)
1059 return ((EventProcessingPolicyRule)o).getGuid();
1060 if (o instanceof DciSummaryTableDescriptor)
1061 return ((DciSummaryTableDescriptor)o).getId();
1062 return 0L;
1063 }
1064
1065 /**
1066 * Remove objects from list
1067 *
1068 * @param viewer
1069 * @param objects
1070 */
1071 private void removeObjects(TableViewer viewer, Map<?, ?> objects)
1072 {
1073 IStructuredSelection selection = (IStructuredSelection)viewer.getSelection();
1074 if (selection.size() > 0)
1075 {
1076 for(Object o : selection.toList())
1077 objects.remove(keyFromObject(o));
1078 viewer.setInput(objects.values().toArray());
1079 setModified();
1080 }
1081 }
1082
1083 /**
1084 * Add event group contents to list
1085 */
1086 private void addEventGroups(EventGroup group)
1087 {
1088 List<EventObject> objects = session.findMultipleEventObjects(group.getEventCodes());
1089 for(EventObject o : objects)
1090 {
1091 if (o instanceof EventGroup)
1092 addEventGroups((EventGroup)o);
1093 else
1094 events.put(o.getCode(), o);
1095 }
1096 }
1097
1098 /**
1099 * Add events to list
1100 */
1101 private void addEvents()
1102 {
1103 EventSelectionDialog dlg = new EventSelectionDialog(getSite().getShell(), true);
1104 dlg.enableMultiSelection(true);
1105 if (dlg.open() == Window.OK)
1106 {
1107 for(EventObject t : dlg.getSelectedEvents())
1108 {
1109 if (t instanceof EventGroup)
1110 addEventGroups((EventGroup)t);
1111 else
1112 events.put(t.getCode(), t);
1113
1114 }
1115 eventViewer.setInput(events.values().toArray());
1116 setModified();
1117 }
1118 }
1119
1120 /**
1121 * Add templates to list
1122 */
1123 private void addTemplates()
1124 {
1125 final ObjectSelectionDialog dlg = new ObjectSelectionDialog(getSite().getShell(), null, ObjectSelectionDialog.createTemplateSelectionFilter());
1126 dlg.enableMultiSelection(true);
1127 dlg.showFilterToolTip(false);
1128 if (dlg.open() == Window.OK)
1129 {
1130 final Set<Long> idList = new HashSet<Long>();
1131 for(AbstractObject o : dlg.getSelectedObjects(Template.class))
1132 {
1133 templates.put(((Template)o).getObjectId(), (Template)o);
1134 idList.add(o.getObjectId());
1135 }
1136 templateViewer.setInput(templates.values().toArray());
1137 setModified();
1138 new ConsoleJob(Messages.get().ExportFileBuilder_ResolveJobName, this, Activator.PLUGIN_ID, null) {
1139 @Override
1140 protected void runInternal(IProgressMonitor monitor) throws Exception
1141 {
1142 final Set<Long> eventCodes = new HashSet<Long>();
1143 final Map<Long, Script> scriptList = new HashMap<Long, Script>();
1144 for(Long id : idList)
1145 {
1146 long[] e = session.getDataCollectionEvents(id);
1147 for(long c : e)
1148 {
1149 if (c >= 100000)
1150 eventCodes.add(c);
1151 }
1152
1153 for(Script s : session.getDataCollectionScripts(id))
1154 scriptList.put(s.getId(), s);
1155 }
1156 runInUIThread(new Runnable() {
1157 @Override
1158 public void run()
1159 {
1160 for(EventObject e : session.findMultipleEventObjects(eventCodes.toArray(new Long[eventCodes.size()])))
1161 {
1162 events.put(e.getCode(), e);
1163 }
1164 eventViewer.setInput(events.values().toArray());
1165
1166 scripts.putAll(scriptList);
1167 scriptViewer.setInput(scripts.values().toArray());
1168 }
1169 });
1170 }
1171
1172 @Override
1173 protected String getErrorMessage()
1174 {
1175 return null;
1176 }
1177 }.start();
1178 }
1179 }
1180
1181 /**
1182 * Add traps to list
1183 */
1184 private void addTraps()
1185 {
1186 SelectSnmpTrapDialog dlg = new SelectSnmpTrapDialog(getSite().getShell(), snmpTrapCache);
1187 if (dlg.open() == Window.OK)
1188 {
1189 final Set<Long> eventCodes = new HashSet<Long>();
1190 for(SnmpTrap t : dlg.getSelection())
1191 {
1192 traps.put(t.getId(), t);
1193 if (t.getEventCode() >= 100000)
1194 {
1195 eventCodes.add((long)t.getEventCode());
1196 }
1197 }
1198 trapViewer.setInput(traps.values().toArray());
1199 setModified();
1200 if (eventCodes.size() > 0)
1201 {
1202 for(EventObject o : session.findMultipleEventObjects(eventCodes.toArray(new Long[eventCodes.size()])))
1203 {
1204 events.put(o.getCode(), o);
1205 }
1206 eventViewer.setInput(events.values().toArray());
1207 };
1208 }
1209 }
1210
1211 /**
1212 * Add rules to list
1213 */
1214 private void addRules()
1215 {
1216 RuleSelectionDialog dlg = new RuleSelectionDialog(getSite().getShell(), rulesCache);
1217 if (dlg.open() == Window.OK)
1218 {
1219 final Set<Long> eventCodes = new HashSet<Long>();
1220 for(EventProcessingPolicyRule r : dlg.getSelectedRules())
1221 {
1222 rules.put(r.getGuid(), r);
1223 for(Long e : r.getEvents())
1224 {
1225 if (e >= 100000)
1226 {
1227 eventCodes.add(e);
1228 }
1229 }
1230 }
1231 ruleViewer.setInput(rules.values().toArray());
1232 setModified();
1233 if (eventCodes.size() > 0)
1234 {
1235 for(EventObject o : session.findMultipleEventObjects(eventCodes.toArray(new Long[eventCodes.size()])))
1236 {
1237 events.put(o.getCode(), o);
1238 }
1239 eventViewer.setInput(events.values().toArray());
1240 };
1241 }
1242 }
1243
1244 /**
1245 * Add script to list
1246 */
1247 private void addScripts()
1248 {
1249 SelectScriptDialog dlg = new SelectScriptDialog(getSite().getShell());
1250 dlg.setMultiSelection(true);
1251 if (dlg.open() == Window.OK)
1252 {
1253 for(Script s : dlg.getSelection())
1254 scripts.put(s.getId(), s);
1255 scriptViewer.setInput(scripts.values().toArray());
1256 setModified();
1257 }
1258 }
1259
1260 /**
1261 * Add oject tools to list
1262 */
1263 private void addObjectTools()
1264 {
1265 ObjectToolSelectionDialog dlg = new ObjectToolSelectionDialog(getSite().getShell());
1266 if (dlg.open() == Window.OK)
1267 {
1268 for(ObjectTool t : dlg.getSelection())
1269 tools.put(t.getId(), t);
1270 toolsViewer.setInput(tools.values().toArray());
1271 setModified();
1272 }
1273 }
1274
1275 /**
1276 * Add oject tools to list
1277 */
1278 private void addSummaryTables()
1279 {
1280 SummaryTableSelectionDialog dlg = new SummaryTableSelectionDialog(getSite().getShell());
1281 if (dlg.open() == Window.OK)
1282 {
1283 for(DciSummaryTableDescriptor t : dlg.getSelection())
1284 summaryTables.put(t.getId(), t);
1285 summaryTableViewer.setInput(summaryTables.values().toArray());
1286 setModified();
1287 }
1288 }
1289
1290 /**
1291 * Export completion handler
1292 */
1293 private interface ExportCompletionHandler
1294 {
1295 /**
1296 * Called when export is complete
1297 *
1298 * @param xml resulting XML document
1299 */
1300 public void exportCompleted(final String xml);
1301 }
1302 }