Skip to main content
summaryrefslogtreecommitdiffstats
blob: bae369744155d46068b61b81dc6bcfde43822183 (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
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
// $codepro.audit.disable com.instantiations.assist.eclipse.analysis.audit.rule.effectivejava.alwaysOverridetoString.alwaysOverrideToString, useForLoop, staticFieldSecurity, com.instantiations.assist.eclipse.analysis.deserializeabilitySecurity, com.instantiations.assist.eclipse.analysis.disallowReturnMutable, com.instantiations.assist.eclipse.analysis.enforceCloneableUsageSecurity
/*******************************************************************************
 * Copyright (c) 2010 Ericsson Research Canada
 * 
 * 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
 * 
 * Description:
 * 
 * This class implements the model controller.  It is used to control the UI 
 * model data
 * 
 * Contributors:
 *   Sebastien Dubois - Created for Mylyn Review R4E project
 *   Jacques Bouthillier - Add method definition for Report package availability
 *   
 ******************************************************************************/

package org.eclipse.mylyn.reviews.r4e.ui.internal.model;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;

import org.eclipse.core.commands.ExecutionException;
import org.eclipse.core.commands.NotEnabledException;
import org.eclipse.core.commands.NotHandledException;
import org.eclipse.core.commands.common.NotDefinedException;
import org.eclipse.core.runtime.Platform;
import org.eclipse.emf.common.util.EList;
import org.eclipse.emf.common.util.URI;
import org.eclipse.jface.preference.IPreferenceStore;
import org.eclipse.jface.viewers.IStructuredSelection;
import org.eclipse.mylyn.reviews.frame.core.model.Location;
import org.eclipse.mylyn.reviews.frame.core.model.Topic;
import org.eclipse.mylyn.reviews.notifications.core.NotificationsCore;
import org.eclipse.mylyn.reviews.notifications.spi.NotificationsConnector;
import org.eclipse.mylyn.reviews.r4e.core.model.R4EAnomaly;
import org.eclipse.mylyn.reviews.r4e.core.model.R4EAnomalyTextPosition;
import org.eclipse.mylyn.reviews.r4e.core.model.R4EContent;
import org.eclipse.mylyn.reviews.r4e.core.model.R4EReview;
import org.eclipse.mylyn.reviews.r4e.core.model.R4EReviewGroup;
import org.eclipse.mylyn.reviews.r4e.core.model.drules.R4EDesignRuleCollection;
import org.eclipse.mylyn.reviews.r4e.core.model.serial.Persistence.RModelFactoryExt;
import org.eclipse.mylyn.reviews.r4e.core.model.serial.Persistence.ResourceUpdater;
import org.eclipse.mylyn.reviews.r4e.core.model.serial.impl.ResourceHandlingException;
import org.eclipse.mylyn.reviews.r4e.core.model.serial.impl.SerializeFactory;
import org.eclipse.mylyn.reviews.r4e.ui.R4EUIPlugin;
import org.eclipse.mylyn.reviews.r4e.ui.internal.commands.ReviewState;
import org.eclipse.mylyn.reviews.r4e.ui.internal.navigator.ReviewNavigatorActionGroup;
import org.eclipse.mylyn.reviews.r4e.ui.internal.navigator.ReviewNavigatorView;
import org.eclipse.mylyn.reviews.r4e.ui.internal.preferences.PreferenceConstants;
import org.eclipse.mylyn.reviews.r4e.ui.internal.utils.R4EUIConstants;
import org.eclipse.mylyn.reviews.r4e.ui.internal.utils.UIUtils;
import org.eclipse.ui.IPropertyListener;

/**
 * @author lmcdubo
 * @version $Revision: 1.0 $
 */
public class R4EUIModelController {

	// ------------------------------------------------------------------------
	// Constants
	// ------------------------------------------------------------------------

	/**
	 * Field MAIL_CONNECTOR_IDS.
	 */
	private static final String[] MAIL_CONNECTOR_IDS = { "reviews.r4e.mail.outlook.connector" }; //$NON-NLS-1$

	// ------------------------------------------------------------------------
	// Member variables
	// ------------------------------------------------------------------------

	/**
	 * Field fRootElement.
	 */
	private static R4EUIRootElement FRootElement = null;

	/**
	 * Field FIsDialogOpen.
	 */
	private static boolean FIsDialogOpen = false;

	/**
	 * Field fActiveReview.
	 */
	private static R4EUIReviewBasic FActiveReview = null;

	/**
	 * Field FReviewSourceProvider.
	 */
	private static ReviewState FReviewSourceProvider = null;

	/**
	 * Field fView.
	 */
	private static ReviewNavigatorView FView = null;

	/**
	 * Field FReviewer.
	 */
	private static String FReviewer = null;

	/**
	 * Field modelExt.
	 */
	public static RModelFactoryExt FModelExt; //Reference to the EMF serialization model

	/**
	 * Field resourceUpdater.
	 */
	public static ResourceUpdater FResourceUpdater; //Reference to the EMF serialization model

	/**
	 * Field fFileAnomalyMap. File version to Anomaly List map
	 */
	private static final Map<String, List<R4EAnomaly>> FFileAnomalyMap = new HashMap<String, List<R4EAnomaly>>(32,
			0.75f); // $codepro.audit.disable constantNamingConvention

	/**
	 * Field FElementStateListenerList.
	 */
	private static final List<IPropertyListener> FElementStateListenerList = new ArrayList<IPropertyListener>(); // $codepro.audit.disable constantNamingConvention

	/**
	 * Field FMailConnector.
	 */
	private static NotificationsConnector FMailConnector = null;

	/**
	 * Field fFocusElement.
	 */
	private static IR4EUIModelElement FFocusElement = null;

	// ------------------------------------------------------------------------
	// Methods
	// ------------------------------------------------------------------------

	/**
	 * Method addElementStateListener.
	 * 
	 * @param aListener
	 *            IPropertyListener
	 */
	public static void addElementStateListener(IPropertyListener aListener) {
		FElementStateListenerList.add(aListener);
	}

	/**
	 * Method removeElementStateListener.
	 * 
	 * @param aListener
	 *            IPropertyListener
	 */
	public static void removeElementStateListener(IPropertyListener aListener) {
		FElementStateListenerList.remove(aListener);
	}

	/**
	 * Method setNavigatorView.
	 * 
	 * @param aView
	 *            ReviewNavigatorView
	 */
	public static void setNavigatorView(ReviewNavigatorView aView) {
		FView = aView;
	}

	/**
	 * Method getNavigatorView.
	 * 
	 * @return ReviewNavigatorView
	 */
	public static ReviewNavigatorView getNavigatorView() {
		return FView;
	}

	/**
	 * Method setReviewCommandSourceProvider.
	 * 
	 * @param aSourceProvider
	 *            ReviewState
	 */
	public static void setReviewCommandSourceProvider(ReviewState aSourceProvider) {
		FReviewSourceProvider = aSourceProvider;
	}

	/**
	 * Get the model root element
	 * 
	 * @return the root R4E element
	 */
	public static IR4EUIModelElement getRootElement() {
		return FRootElement;
	}

	/**
	 * Set the currently active review
	 * 
	 * @param aActiveReview
	 *            ReviewElement - the currently open ReviewElement
	 */
	public static void setActiveReview(R4EUIReviewBasic aActiveReview) {
		FActiveReview = aActiveReview;
		if (null != FReviewSourceProvider) {
			FReviewSourceProvider.setCurrentReview(FActiveReview);
		}
		//check to apply filters
		try {
			final ReviewNavigatorActionGroup actionGroup = (ReviewNavigatorActionGroup) FView.getActionSet();
			if (actionGroup.isMyReviewFilterSet()) {
				actionGroup.runReviewsMyFilterCommand(true);
			}
			if (actionGroup.isParticipantFilterSet()) {
				actionGroup.runReviewsParticipantFilterCommand(actionGroup.getFilterParticipant());
			}
		} catch (ExecutionException e) {
			R4EUIPlugin.Ftracer.traceError("Exception: " + e.toString() + " (" + e.getMessage() + ")");
			R4EUIPlugin.getDefault().logError("Exception: " + e.toString(), e);
		} catch (NotDefinedException e) {
			R4EUIPlugin.Ftracer.traceError("Exception: " + e.toString() + " (" + e.getMessage() + ")");
			R4EUIPlugin.getDefault().logError("Exception: " + e.toString(), e);
		} catch (NotEnabledException e) {
			R4EUIPlugin.Ftracer.traceError("Exception: " + e.toString() + " (" + e.getMessage() + ")");
			R4EUIPlugin.getDefault().logError("Exception: " + e.toString(), e);
		} catch (NotHandledException e) {
			R4EUIPlugin.Ftracer.traceError("Exception: " + e.toString() + " (" + e.getMessage() + ")");
			R4EUIPlugin.getDefault().logError("Exception: " + e.toString(), e);
		}
	}

	/**
	 * Set properties state based on the current dialog state
	 * 
	 * @param aIsDialogOpen
	 *            boolean
	 */
	public static void setJobInProgress(boolean aIsDialogOpen) {
		FIsDialogOpen = aIsDialogOpen;
		FView.getTreeViewer().getTree().setEnabled(!aIsDialogOpen); //Also disable operations on UI Tree
		for (IPropertyListener listener : FElementStateListenerList) {
			listener.propertyChanged(null, 0);
		}
	}

	/**
	 * Set properties state based on the selected element
	 * 
	 * @param aSelection
	 *            the selected element
	 */
	public static void selectionChanged(IStructuredSelection aSelection) {
		for (IPropertyListener listener : FElementStateListenerList) {
			listener.propertyChanged(null, 0); //TODO this is temporary and should be refined later
		}
	}

	/**
	 * Refresh properties
	 */
	public static void propertyChanged() {
		for (IPropertyListener listener : FElementStateListenerList) {
			listener.propertyChanged(null, 0); //TODO this is temporary and should be refined later
		}
	}

	/**
	 * Get the currently active review
	 * 
	 * @return the currently open ReviewElement
	 */
	public static R4EUIReviewBasic getActiveReview() {
		return FActiveReview;
	}

	/**
	 * Get the current dialog state
	 * 
	 * @return the current dialog state
	 */
	public static boolean isJobInProgress() {
		return FIsDialogOpen;
	}

	/**
	 * Method peekReviewGroup.
	 * 
	 * @param filePath
	 *            String
	 * @return R4EReviewGroup
	 * @throws ResourceHandlingException
	 */
	public static R4EReviewGroup peekReviewGroup(String filePath) {
		FModelExt = SerializeFactory.getModelExtension();
		R4EReviewGroup group = null;
		try {
			group = FModelExt.openR4EReviewGroup(URI.createFileURI(filePath));
		} catch (ResourceHandlingException e) {
			R4EUIPlugin.Ftracer.traceWarning("Exception: " + e.toString() + " (" + e.getMessage() + ")");
		}
		return group;
	}

	/**
	 * Method peekRuleSet.
	 * 
	 * @param filePath
	 *            String
	 * @return R4EDesignRuleCollection
	 * @throws ResourceHandlingException
	 */
	public static R4EDesignRuleCollection peekRuleSet(String filePath) throws ResourceHandlingException {
		FModelExt = SerializeFactory.getModelExtension();
		return FModelExt.openR4EDesignRuleCollection(URI.createFileURI(filePath));
	}

	/**
	 * Method loadModel. Load data from Serialized Model
	 */
	public static void loadModel() {
		FModelExt = SerializeFactory.getModelExtension();
		FResourceUpdater = SerializeFactory.getResourceUpdater();

		//Set current user as reviewer
		setReviewer(R4EUIPlugin.getDefault().getPreferenceStore().getString(PreferenceConstants.P_USER_ID));

		//Add root for Review Navigator
		FRootElement = new R4EUIRootElement(null, "R4E");

		//Load Review Groups
		final IPreferenceStore preferenceStore = R4EUIPlugin.getDefault().getPreferenceStore();
		final String groupPaths = preferenceStore.getString(PreferenceConstants.P_GROUP_FILE_PATH);
		final List<String> groupPathsList = UIUtils.parseStringList(groupPaths);
		loadReviewGroups(groupPathsList);
		final String ruleSetPaths = preferenceStore.getString(PreferenceConstants.P_RULE_SET_FILE_PATH);
		final List<String> ruleSetPathsList = UIUtils.parseStringList(ruleSetPaths);
		loadRuleSets(ruleSetPathsList);

		//Verify Mail Connectivity
		FMailConnector = NotificationsCore.getFirstEnabled(MAIL_CONNECTOR_IDS);
	}

	/**
	 * Method loadReviewGroups.
	 * 
	 * @param aGroupPaths
	 *            List<String>
	 */
	public static void loadReviewGroups(List<String> aGroupPaths) {

		final boolean changePrefsPaths = false;
		R4EReviewGroup reviewGroup = null;
		final List<String> newGroupPaths = new ArrayList<String>();
		newGroupPaths.addAll(aGroupPaths);

		for (String groupPath : aGroupPaths) {
			reviewGroup = null;

			//First try to open the review group file as entrered in the preferences
			//If it fails, then create it
			try {
				reviewGroup = FModelExt.openR4EReviewGroup(URI.createFileURI(groupPath));
				FRootElement.loadReviewGroup(reviewGroup);
			} catch (ResourceHandlingException e) {
				R4EUIPlugin.Ftracer.traceWarning("Exception: " + e.toString() + " (" + e.getMessage() + ")");

				//Bug 347780:  Remove this code for now.  Later we will implement to validate the group files
				/*
				//Review Group not found, ask user for review group creation
				MessageDialog dialog = new MessageDialog( // $codepro.audit.disable variableDeclaredInLoop
						null, "Missing Review Group", null, "Review Group file at location " + groupPath
								+ " not found, remove it from Preferences?", MessageDialog.WARNING, new String[] {
								"Yes", "No" }, R4EUIConstants.DIALOG_NO); // no is the default
				int result = dialog.open(); // $codepro.audit.disable variableDeclaredInLoop

				if (result == R4EUIConstants.DIALOG_YES) {
					changePrefsPaths = true;
					newGroupPaths.remove(groupPath);
				}
				*/
			}
		}

		//Adjust review group paths in preferences
		if (changePrefsPaths) {
			R4EUIPlugin.getDefault().getPreferenceStore().setValue(PreferenceConstants.P_GROUP_FILE_PATH,
					buildReviewGroupsStr(newGroupPaths));
		}
	}

	/**
	 * Method loadReviewGroups.
	 * 
	 * @param aRuleSetPaths
	 *            List<String>
	 */
	public static void loadRuleSets(List<String> aRuleSetPaths) {

		final boolean changePrefsPaths = false;
		R4EDesignRuleCollection ruleSet = null;
		final List<String> newRuleSetPaths = new ArrayList<String>();
		newRuleSetPaths.addAll(aRuleSetPaths);

		for (String ruleSetPath : aRuleSetPaths) {
			ruleSet = null;

			//First try to open the review group file as entrered in the preferences
			//If it fails, then create it
			try {
				ruleSet = FModelExt.openR4EDesignRuleCollection(URI.createFileURI(ruleSetPath));
				FRootElement.loadRuleSet(ruleSet);
			} catch (ResourceHandlingException e) {
				R4EUIPlugin.Ftracer.traceWarning("Exception: " + e.toString() + " (" + e.getMessage() + ")");

				//Bug 347780:  Remove this code for now.  Later we will implement to validate the group files
				/*
				//Review Group not found, ask user for review group creation
				MessageDialog dialog = new MessageDialog(
						// $codepro.audit.disable variableDeclaredInLoop
						null, "Missing Rule Set", null, "Rule Set file at location " + ruleSetPath + " not found",
						MessageDialog.WARNING, new String[] { "Ok" }, R4EUIConstants.DIALOG_YES); // no is the default
				int result = dialog.open(); // $codepro.audit.disable variableDeclaredInLoop

				if (result == R4EUIConstants.DIALOG_YES) {
					changePrefsPaths = true;
					newRuleSetPaths.remove(ruleSetPath);
				}
				 */
			}
		}

		//Adjust review group paths in preferences
		if (changePrefsPaths) {
			R4EUIPlugin.getDefault().getPreferenceStore().setValue(PreferenceConstants.P_RULE_SET_FILE_PATH,
					buildReviewGroupsStr(newRuleSetPaths));
		}
	}

	/**
	 * Method buildReviewGroupsStr.
	 * 
	 * @param aReviewGroups
	 *            String[]
	 * @return String
	 */
	private static String buildReviewGroupsStr(List<String> aReviewGroups) {
		final StringBuffer newPathsStr = new StringBuffer(R4EUIConstants.REVIEW_GROUP_PATHS_LENGTH);
		for (String group : aReviewGroups) {
			newPathsStr.append(group + System.getProperty("line.separator"));
		}
		return newPathsStr.toString();
	}

	/**
	 * Method mapAnomalies. Map anomalies to the files in which they are located
	 * 
	 * @param aReview
	 *            R4EReview
	 */
	public static void mapAnomalies(R4EReview aReview) {
		final EList<Topic> anomalies = aReview.getTopics();
		Topic anomaly = null;
		EList<Location> locations = null;
		String targetFileVersion = null;
		final int anomaliesSize = anomalies.size();
		for (int i = 0; i < anomaliesSize; i++) {
			anomaly = anomalies.get(i);

			locations = anomaly.getLocation();
			for (Location location : locations) {
				targetFileVersion = ((R4EAnomalyTextPosition) ((R4EContent) location).getLocation()).getFile()
						.getLocalVersionID();
				if (FFileAnomalyMap.containsKey(targetFileVersion)) {
					//Add anomaly for this file version if not already present
					List<R4EAnomaly> anomalyList = FFileAnomalyMap.get(targetFileVersion); // $codepro.audit.disable variableDeclaredInLoop
					if (!(anomalyList.contains(anomaly))) {
						anomalyList.add((R4EAnomaly) anomaly);
					}
				} else {
					List<R4EAnomaly> anomalyList = new ArrayList<R4EAnomaly>(); // $codepro.audit.disable variableDeclaredInLoop
					anomalyList.add((R4EAnomaly) anomaly);
					FFileAnomalyMap.put(targetFileVersion, anomalyList);
				}
			}

		}
	}

	/**
	 * Method clearAnomalyMap.
	 */
	public static void clearAnomalyMap() {
		final Set<String> keys = FFileAnomalyMap.keySet();
		for (String key : keys) {
			List<R4EAnomaly> anomaliesList = FFileAnomalyMap.get(key);
			anomaliesList.clear();
		}
		FFileAnomalyMap.clear();
	}

	/**
	 * Method getAnomaliesForFile. Get list of anomalies for a given file
	 * 
	 * @param aFilePath
	 *            String
	 * @return List<R4EAnomaly>
	 */
	public static List<R4EAnomaly> getAnomaliesForFile(String aFilePath) {
		return FFileAnomalyMap.get(aFilePath);
	}

	/**
	 * Method getReviewer.
	 * 
	 * @return R4EParticipant
	 */
	public static String getReviewer() {
		return FReviewer;
	}

	/**
	 * Method setReviewer.
	 * 
	 * @param aReviewer
	 *            R4EParticipant
	 */
	public static void setReviewer(String aReviewer) {
		FReviewer = aReviewer;
	}

	/**
	 * Method isUserQueryAvailable.
	 * 
	 * @return boolean\
	 */
	public static boolean isUserQueryAvailable() {
		//Verify if the LDAP bundle is available
		if (null != Platform.getBundle("org.eclipse.mylyn.reviews.ldap")) { //$NON-NLS-1$
			return true;
		}
		return false;
	}

	/**
	 * Method isUserReportAvailable.
	 * 
	 * @return boolean\
	 */
	public static boolean isUserReportAvailable() {
		//Verify if the Report bundle is available
		if (null != Platform.getBundle("org.eclipse.mylyn.reviews.r4e.report")) { //$NON-NLS-1$
			return true;
		}
		return false;
	}

	/**
	 * Method getMailConnector.
	 * 
	 * @return NotificationsConnector
	 */
	public static NotificationsConnector getMailConnector() {
		return FMailConnector;
	}

	/**
	 * Method setCurrentFocusElement.
	 * 
	 * @param aElement
	 *            IR4EUIModelElement
	 */
	public static void setCurrentFocusElement(IR4EUIModelElement aElement) {
		FFocusElement = aElement;
	}

	/**
	 * Method getCurrentFocusElement.
	 * 
	 * @return IR4EUIModelElement
	 */
	public static IR4EUIModelElement getCurrentFocusElement() {
		return FFocusElement;
	}
}

Back to the top