Skip to main content
summaryrefslogtreecommitdiffstats
blob: 712eb626f7ce865574acd93f95e5700b5c6839c8 (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
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
/*******************************************************************************
 * Copyright (c) 2010 Frank Becker 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:
 *     Frank Becker - initial API and implementation
 *******************************************************************************/

package org.eclipse.mylyn.internal.bugzilla.core.service;

import java.io.IOException;
import java.text.NumberFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Locale;
import java.util.Map;
import java.util.Set;

import org.apache.commons.httpclient.HttpClient;
import org.apache.xmlrpc.XmlRpcException;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.core.runtime.IStatus;
import org.eclipse.core.runtime.Status;
import org.eclipse.core.runtime.SubProgressMonitor;
import org.eclipse.mylyn.commons.net.AbstractWebLocation;
import org.eclipse.mylyn.commons.net.AuthenticationCredentials;
import org.eclipse.mylyn.commons.net.AuthenticationType;
import org.eclipse.mylyn.internal.bugzilla.core.BugHistory;
import org.eclipse.mylyn.internal.bugzilla.core.BugHistory.Revision;
import org.eclipse.mylyn.internal.bugzilla.core.BugzillaAttachmentMapper;
import org.eclipse.mylyn.internal.bugzilla.core.BugzillaAttribute;
import org.eclipse.mylyn.internal.bugzilla.core.BugzillaClient;
import org.eclipse.mylyn.internal.bugzilla.core.BugzillaCorePlugin;
import org.eclipse.mylyn.internal.bugzilla.core.BugzillaCustomField;
import org.eclipse.mylyn.internal.bugzilla.core.BugzillaTaskDataHandler;
import org.eclipse.mylyn.internal.bugzilla.core.RepositoryConfiguration;
import org.eclipse.mylyn.internal.commons.xmlrpc.CommonXmlRpcClient;
import org.eclipse.mylyn.tasks.core.IRepositoryPerson;
import org.eclipse.mylyn.tasks.core.data.TaskAttribute;
import org.eclipse.mylyn.tasks.core.data.TaskAttributeMapper;
import org.eclipse.mylyn.tasks.core.data.TaskCommentMapper;
import org.eclipse.mylyn.tasks.core.data.TaskData;
import org.eclipse.mylyn.tasks.core.data.TaskDataCollector;

@SuppressWarnings("restriction")
public class BugzillaXmlRpcClient extends CommonXmlRpcClient {

	public static final String XML_BUGZILLA_VERSION = "Bugzilla.version"; //$NON-NLS-1$

	public static final String XML_BUGZILLA_TIME = "Bugzilla.time"; //$NON-NLS-1$ 

	public static final String XML_USER_LOGIN = "User.login"; //$NON-NLS-1$

	public static final String XML_USER_GET = "User.get"; //$NON-NLS-1$

	public static final String XML_BUG_ATTACHMENTS = "Bug.attachments"; //$NON-NLS-1$

	public static final String XML_BUG_FIELDS = "Bug.fields"; //$NON-NLS-1$

	public static final String XML_BUG_GET = "Bug.get"; //$NON-NLS-1$

	public static final String XML_BUG_COMMENTS = "Bug.comments"; //$NON-NLS-1$

	public static final String XML_BUG_HISTORY = "Bug.history"; //$NON-NLS-1$

	public static final String XML_PRODUCT_GET_SELECTABLE = "Product.get_selectable_products"; //$NON-NLS-1$

	public static final String XML_PRODUCT_GET_ENTERABLE = "Product.get_enterable_products"; //$NON-NLS-1$

	public static final String XML_PRODUCT_GET_ACCESSIBLE = "Product.get_accessible_products"; //$NON-NLS-1$

	public static final String XML_PRODUCT_GET = "Product.get"; //$NON-NLS-1$

	/*
	 * Parameter Definitions
	 * 
	 */

	public static final String XML_PARAMETER_LOGIN = "login"; //$NON-NLS-1$

	public static final String XML_PARAMETER_PASSWORD = "password"; //$NON-NLS-1$

	public static final String XML_PARAMETER_REMEMBER = "remember"; //$NON-NLS-1$

	public static final String XML_PARAMETER_IDS = "ids"; //$NON-NLS-1$

	public static final String XML_PARAMETER_NAMES = "names"; //$NON-NLS-1$

	public static final String XML_PARAMETER_MATCH = "match"; //$NON-NLS-1$

	public static final String XML_PARAMETER_EXCLUDE_FIELDS = "exclude_fields"; //$NON-NLS-1$

	/*
	 * Response Parameter Definitions
	 * 
	 */

	public static final String XML_RESPONSE_DB_TIME = "db_time"; //$NON-NLS-1$

	public static final String XML_RESPONSE_WEB_TIME = "web_time"; //$NON-NLS-1$

	public static final String[] XML_BUGZILLA_TIME_RESPONSE_TO_REMOVE = {
			"tz_offset", "tz_short_name", "web_time_utc", "tz_name" }; //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ //$NON-NLS-4$

	public static final String XML_RESPONSE_VERSION = "version"; //$NON-NLS-1$

	public static final String XML_RESPONSE_USERS = "users"; //$NON-NLS-1$

	public static final String XML_RESPONSE_ID = "id"; //$NON-NLS-1$

	public static final String XML_RESPONSE_IDS = "ids"; //$NON-NLS-1$

	public static final String XML_RESPONSE_FIELDS = "fields"; //$NON-NLS-1$

	public static final String XML_RESPONSE_PRODUCTS = "products"; //$NON-NLS-1$

	public static final String XML_RESPONSE_BUGS = "bugs"; //$NON-NLS-1$

	/*
	 * Fields
	 * 
	 */
	private int userID = -1;

	private final BugzillaClient bugzillaClient;

	public BugzillaXmlRpcClient(AbstractWebLocation location, HttpClient client, BugzillaClient bugzillaClient) {
		super(location, client);
		this.bugzillaClient = bugzillaClient;
	}

	public BugzillaXmlRpcClient(AbstractWebLocation location, BugzillaClient bugzillaClient) {
		super(location);
		this.bugzillaClient = bugzillaClient;
	}

	public String getVersion(final IProgressMonitor monitor) throws XmlRpcException {
		return (new BugzillaXmlRpcOperation<String>(this) {
			@Override
			public String execute() throws XmlRpcException {
				String result = ""; //$NON-NLS-1$
				HashMap<?, ?> response = (HashMap<?, ?>) call(monitor, XML_BUGZILLA_VERSION, (Object[]) null);
				result = response2String(response, XML_RESPONSE_VERSION);
				return result;
			}
		}).execute();
	}

	public Date getDBTime(final IProgressMonitor monitor) throws XmlRpcException {
		return (new BugzillaXmlRpcOperation<Date>(this) {
			@Override
			public Date execute() throws XmlRpcException {
				Date result = null;
				HashMap<?, ?> response = (HashMap<?, ?>) call(monitor, XML_BUGZILLA_TIME, (Object[]) null);
				result = response2Date(response, XML_RESPONSE_DB_TIME);
				return result;
			}
		}).execute();
	}

	public Date getWebTime(final IProgressMonitor monitor) throws XmlRpcException {
		return (new BugzillaXmlRpcOperation<Date>(this) {
			@Override
			public Date execute() throws XmlRpcException {
				Date result = null;
				HashMap<?, ?> response = (HashMap<?, ?>) call(monitor, XML_BUGZILLA_TIME, (Object[]) null);
				result = response2Date(response, XML_RESPONSE_WEB_TIME);
				return result;
			}
		}).execute();
	}

	public HashMap<?, ?> getTime(final IProgressMonitor monitor) throws XmlRpcException {
		return (new BugzillaXmlRpcOperation<HashMap<?, ?>>(this) {
			@Override
			public HashMap<?, ?> execute() throws XmlRpcException {
				HashMap<?, ?> response = (HashMap<?, ?>) call(monitor, XML_BUGZILLA_TIME, (Object[]) null);
				if (response != null) {
					for (String exclude : XML_BUGZILLA_TIME_RESPONSE_TO_REMOVE) {
						response.remove(exclude);
					}
				}
				return response;
			}
		}).execute();
	}

	public int login(final IProgressMonitor monitor) throws XmlRpcException {
		userID = -1;
		final AuthenticationCredentials credentials = this.getLocation().getCredentials(AuthenticationType.REPOSITORY);
		if (credentials != null) {
			String user = credentials.getUserName();
			String password = credentials.getPassword();
			if ("".equals(user) || "".equals(password)) { //$NON-NLS-1$//$NON-NLS-2$
				return userID;
			}
			userID = (new BugzillaXmlRpcOperation<Integer>(this) {
				@SuppressWarnings("serial")
				@Override
				public Integer execute() throws XmlRpcException {
					HashMap<?, ?> response = (HashMap<?, ?>) call(monitor, XML_USER_LOGIN,
							new Object[] { new HashMap<String, Object>() {
								{
									put(XML_PARAMETER_LOGIN, credentials.getUserName());
									put(XML_PARAMETER_PASSWORD, credentials.getPassword());
									put(XML_PARAMETER_REMEMBER, true);
								}
							} });
					if (response != null) {
						Integer result = response2Integer(response, XML_RESPONSE_ID);
						return result;
					}
					return null;
				}
			}).execute();
		}
		return userID;
	}

	private Object[] getUserInfoInternal(final IProgressMonitor monitor, final Object[] callParm)
			throws XmlRpcException {
		return (new BugzillaXmlRpcOperation<Object[]>(this) {
			@Override
			public Object[] execute() throws XmlRpcException {
				Object[] result = null;
				HashMap<?, ?> response = (HashMap<?, ?>) call(monitor, XML_USER_GET, callParm);
				result = response2ObjectArray(response, XML_RESPONSE_USERS);
				return result;
			}
		}).execute();
	}

	@SuppressWarnings("serial")
	public Object[] getUserInfoFromIDs(final IProgressMonitor monitor, final Integer[] ids) throws XmlRpcException {
		return getUserInfoInternal(monitor, new Object[] { new HashMap<String, Object[]>() {
			{
				put(XML_PARAMETER_IDS, ids);
			}
		} });
	}

	@SuppressWarnings("serial")
	public Object[] getUserInfoFromNames(final IProgressMonitor monitor, final String[] names) throws XmlRpcException {
		return getUserInfoInternal(monitor, new Object[] { new HashMap<String, Object[]>() {
			{
				put(XML_PARAMETER_NAMES, names);
			}
		} });
	}

	public Object[] getUserInfoWithMatch(final IProgressMonitor monitor, String[] matchs) throws XmlRpcException {
		HashMap<String, Object[]> parmArray = new HashMap<String, Object[]>();
		Object[] callParm = new Object[] { parmArray };
		parmArray.put(XML_PARAMETER_MATCH, matchs);
		return getUserInfoInternal(monitor, callParm);
	}

	private Object[] getFieldsInternal(final IProgressMonitor monitor, final Object[] callParm) throws XmlRpcException {
		return (new BugzillaXmlRpcOperation<Object[]>(this) {
			@Override
			public Object[] execute() throws XmlRpcException {
				Object[] result = null;
				HashMap<?, ?> response = (HashMap<?, ?>) call(monitor, XML_BUG_FIELDS, callParm);
				result = response2ObjectArray(response, XML_RESPONSE_FIELDS);
				return result;
			}
		}).execute();
	}

	public Object[] getAllFields(final IProgressMonitor monitor) throws XmlRpcException {
		return getFieldsInternal(monitor, null);
	}

	@SuppressWarnings("serial")
	public Object[] getFieldsWithNames(final IProgressMonitor monitor, final String[] names) throws XmlRpcException {
		return getFieldsInternal(monitor, new Object[] { new HashMap<String, Object[]>() {
			{
				put(XML_PARAMETER_NAMES, names);
			}
		} });
	}

	@SuppressWarnings("serial")
	public Object[] getFieldsWithIDs(final IProgressMonitor monitor, final Integer[] ids) throws XmlRpcException {
		return getFieldsInternal(monitor, new Object[] { new HashMap<String, Object[]>() {
			{
				put(XML_PARAMETER_IDS, ids);
			}
		} });
	}

	public Object[] getSelectableProducts(final IProgressMonitor monitor) throws XmlRpcException {
		return (new BugzillaXmlRpcOperation<Object[]>(this) {
			@Override
			public Object[] execute() throws XmlRpcException {
				Object[] result = null;
				HashMap<?, ?> response = (HashMap<?, ?>) call(monitor, XML_PRODUCT_GET_SELECTABLE, (Object[]) null);
				result = response2ObjectArray(response, XML_RESPONSE_IDS);
				return result;
			}
		}).execute();
	}

	public Object[] getEnterableProducts(final IProgressMonitor monitor) throws XmlRpcException {
		return (new BugzillaXmlRpcOperation<Object[]>(this) {
			@Override
			public Object[] execute() throws XmlRpcException {
				Object[] result = null;
				HashMap<?, ?> response = (HashMap<?, ?>) call(monitor, XML_PRODUCT_GET_ENTERABLE, (Object[]) null);
				result = response2ObjectArray(response, XML_RESPONSE_IDS);
				return result;
			}
		}).execute();
	}

	public Object[] getAccessibleProducts(final IProgressMonitor monitor) throws XmlRpcException {
		return (new BugzillaXmlRpcOperation<Object[]>(this) {
			@Override
			public Object[] execute() throws XmlRpcException {
				Object[] result = null;
				HashMap<?, ?> response = (HashMap<?, ?>) call(monitor, XML_PRODUCT_GET_ACCESSIBLE, (Object[]) null);
				result = response2ObjectArray(response, XML_RESPONSE_IDS);
				return result;
			}
		}).execute();
	}

	public Object[] getProducts(final IProgressMonitor monitor, final Object[] ids) throws XmlRpcException {
		return (new BugzillaXmlRpcOperation<Object[]>(this) {
			@SuppressWarnings("serial")
			@Override
			public Object[] execute() throws XmlRpcException {
				Object[] result = null;
				HashMap<?, ?> response = (HashMap<?, ?>) call(monitor, XML_PRODUCT_GET,
						new Object[] { new HashMap<String, Object[]>() {
							{
								put(XML_PARAMETER_IDS, ids);
							}
						} });
				result = response2ObjectArray(response, XML_RESPONSE_PRODUCTS);
				return result;
			}
		}).execute();
	}

	private Object[] response2ObjectArray(HashMap<?, ?> response, String name) throws XmlRpcException {
		Object[] result;
		if (response == null) {
			return null;
		}
		try {
			result = (Object[]) response.get(name);
		} catch (ClassCastException e) {
			result = null;
			throw new XmlRpcClassCastException(e);
		}
		return result;
	}

	private Integer response2Integer(HashMap<?, ?> response, String name) throws XmlRpcException {
		Integer result;
		if (response == null) {
			return null;
		}
		try {
			result = (Integer) response.get(name);
		} catch (ClassCastException e) {
			result = null;
			throw new XmlRpcClassCastException(e);
		}
		return result;
	}

	private String response2String(HashMap<?, ?> response, String name) throws XmlRpcException {
		String result;
		if (response == null) {
			return null;
		}
		try {
			result = (String) response.get(name);
		} catch (ClassCastException e) {
			result = null;
			throw new XmlRpcClassCastException(e);
		}
		return result;
	}

	private Date response2Date(HashMap<?, ?> response, String name) throws XmlRpcException {
		Date result;
		if (response == null) {
			return null;
		}
		try {
			result = (Date) response.get(name);
		} catch (ClassCastException e) {
			result = null;
			throw new XmlRpcClassCastException(e);
		}
		return result;
	}

	public void updateConfiguration(IProgressMonitor monitor, RepositoryConfiguration repositoryConfiguration,
			String fileName) throws CoreException {
		repositoryConfiguration.setValidTransitions(monitor, fileName, this);
		if (!repositoryConfiguration.getProducts().isEmpty()) {
			updateProductInfo(monitor, repositoryConfiguration);
		}
	}

	public void updateProductInfo(IProgressMonitor monitor, RepositoryConfiguration repositoryConfiguration)
			throws CoreException {
		try {
			Object[] productIDs = getAccessibleProducts(monitor);
			Object[] products = getProducts(monitor, productIDs);
			for (Object object : products) {
				if (object instanceof HashMap<?, ?>) {
					String defaultMilestone = null;
					String product = (String) ((HashMap<?, ?>) object).get("name"); //$NON-NLS-1$
					HashMap<?, ?> values = (HashMap<?, ?>) ((HashMap<?, ?>) object).get("internals"); //$NON-NLS-1$
					if (values == null) {
						continue;
					}
					if (values instanceof HashMap<?, ?>) {
						Object defaultMilestoneObj = ((HashMap<?, ?>) values).get("defaultmilestone"); //$NON-NLS-1$
						if (defaultMilestoneObj instanceof String) {
							defaultMilestone = (String) defaultMilestoneObj;
						} else if (defaultMilestoneObj instanceof Double) {
							defaultMilestone = ((Double) defaultMilestoneObj).toString();
						} else if (defaultMilestoneObj instanceof Integer) {
							defaultMilestone = ((Integer) defaultMilestoneObj).toString();
						}
					}
					if (product != null && !product.equals("") //$NON-NLS-1$
							&& defaultMilestone != null && !defaultMilestone.equals("")) { //$NON-NLS-1$
						repositoryConfiguration.setDefaultMilestone(product, defaultMilestone);
					}
				}
			}
		} catch (XmlRpcException e) {
			throw new CoreException(new Status(IStatus.ERROR, BugzillaCorePlugin.ID_PLUGIN,
					"Can not get the Default Milestones using XMLRPC")); //$NON-NLS-1$
		}

	}

	public int getUserID() {
		return userID;
	}

	public List<BugHistory> getHistory(final Integer[] ids, final IProgressMonitor monitor) throws XmlRpcException {
		return (new BugzillaXmlRpcOperation<List<BugHistory>>(this) {
			@Override
			public List<BugHistory> execute() throws XmlRpcException {
				Map<String, Object> params = new HashMap<String, Object>();
				params.put("ids", ids); //$NON-NLS-1$
				Map<?, ?> response = (HashMap<?, ?>) call(monitor, XML_BUG_HISTORY, params);
				if (response != null) {
					List<BugHistory> result = new ArrayList<BugHistory>(ids.length);
					for (Object item : (Object[]) response.get("bugs")) { //$NON-NLS-1$
						Map<?, ?> map = (Map<?, ?>) item;
						Integer id = (Integer) map.get("id"); //$NON-NLS-1$
						BugHistory history = new BugHistory(id);
						Object[] historyItems = (Object[]) map.get("history"); //$NON-NLS-1$
						for (Object historyItem : historyItems) {
							Map<?, ?> historyItemMap = (Map<?, ?>) historyItem;
							Revision revision = history.createRevision((Date) historyItemMap.get("when"), //$NON-NLS-1$
									(String) historyItemMap.get("who")); //$NON-NLS-1$
							Object[] changeItems = (Object[]) historyItemMap.get("changes"); //$NON-NLS-1$
							if (changeItems != null) {
								for (Object changeItem : changeItems) {
									Map<?, ?> changeItemMap = (Map<?, ?>) changeItem;
									Object attachmentID = changeItemMap.get("attachment_id"); //$NON-NLS-1$
									int attachmentId = -1;
									if (attachmentID instanceof Integer) {
										attachmentId = (Integer) attachmentID;
									} else if (attachmentID instanceof String) {
										Integer.parseInt((String) attachmentID);
									}
									revision.addChange((String) changeItemMap.get("field_name"), //$NON-NLS-1$
											(String) changeItemMap.get("added"), (String) changeItemMap.get("removed"), //$NON-NLS-1$ //$NON-NLS-2$
											attachmentId);
								}
							}
						}
						result.add(history);
					}
					return result;
				}
				return null;
			}
		}).execute();
	}

	@SuppressWarnings("unchecked")
	private HashMap<String, HashMap<String, Object[]>> response2HashMapHashMap(HashMap<?, ?> response, String name)
			throws XmlRpcException {
		HashMap<String, HashMap<String, Object[]>> result;
		if (response == null) {
			return null;
		}
		try {
			result = (HashMap<String, HashMap<String, Object[]>>) response.get(name);
		} catch (ClassCastException e) {
			result = null;
			throw new XmlRpcClassCastException(e);
		}
		return result;
	}

	@SuppressWarnings("unchecked")
	private HashMap<String, Object[]> response2HashMap(HashMap<?, ?> response, String name) throws XmlRpcException {
		HashMap<String, Object[]> result;
		if (response == null) {
			return null;
		}
		try {
			result = (HashMap<String, Object[]>) response.get(name);
		} catch (ClassCastException e) {
			result = null;
			throw new XmlRpcClassCastException(e);
		}
		return result;
	}

	protected String getConnectorKind() {
		return BugzillaCorePlugin.CONNECTOR_KIND;
	}

	public Object[] getBugs(final IProgressMonitor monitor, final Object[] ids) throws XmlRpcException {
		return (new BugzillaXmlRpcOperation<Object[]>(this) {
			@SuppressWarnings("serial")
			@Override
			public Object[] execute() throws XmlRpcException {
				Object[] result = null;
				HashMap<?, ?> response = (HashMap<?, ?>) call(monitor, XML_BUG_GET,
						new Object[] { new HashMap<String, Object[]>() {
							{
								put(XML_PARAMETER_IDS, ids);
							}
						} });
				result = response2ObjectArray(response, XML_RESPONSE_BUGS);
				return result;
			}
		}).execute();
	}

	public HashMap<String, HashMap<String, Object[]>> getCommentsInternal(final IProgressMonitor monitor,
			final Object[] ids) throws XmlRpcException {
		return (new BugzillaXmlRpcOperation<HashMap<String, HashMap<String, Object[]>>>(this) {
			@SuppressWarnings("serial")
			@Override
			public HashMap<String, HashMap<String, Object[]>> execute() throws XmlRpcException {
				HashMap<String, HashMap<String, Object[]>> result = null;
				HashMap<?, ?> response = (HashMap<?, ?>) call(monitor, XML_BUG_COMMENTS,
						new Object[] { new HashMap<String, Object[]>() {
							{
								put(XML_PARAMETER_IDS, ids);
							}
						} });
				result = response2HashMapHashMap(response, XML_RESPONSE_BUGS);
				return result;
			}
		}).execute();
	}

	public HashMap<String, Object[]> getAttachmentsInternal(final IProgressMonitor monitor, final Object[] ids)
			throws XmlRpcException {
		return (new BugzillaXmlRpcOperation<HashMap<String, Object[]>>(this) {
			@SuppressWarnings("serial")
			@Override
			public HashMap<String, Object[]> execute() throws XmlRpcException {
				HashMap<String, Object[]> result = null;
				HashMap<?, ?> response = (HashMap<?, ?>) call(monitor, XML_BUG_ATTACHMENTS,
						new Object[] { new HashMap<String, Object[]>() {
							{
								put(XML_PARAMETER_IDS, ids);
								put(XML_PARAMETER_EXCLUDE_FIELDS, new String[] { "data" }); //$NON-NLS-1$
							}
						} });
				result = response2HashMap(response, XML_RESPONSE_BUGS);
				return result;
			}
		}).execute();
	}

	private String getMappedBugzillaAttribute(String xmlrpcName) {
		if (xmlrpcName != null) {
			if (xmlrpcName.equals("target_milestone") || xmlrpcName.equals("see_also") //$NON-NLS-1$ //$NON-NLS-2$
					|| xmlrpcName.equals("resolution") || xmlrpcName.equals("version") //$NON-NLS-1$ //$NON-NLS-2$
					|| xmlrpcName.equals("op_sys") || xmlrpcName.equals("component") //$NON-NLS-1$ //$NON-NLS-2$
					|| xmlrpcName.equals("priority") || xmlrpcName.equals("qa_contact") //$NON-NLS-1$ //$NON-NLS-2$
					|| xmlrpcName.equals("keywords") || xmlrpcName.equals("product") //$NON-NLS-1$ //$NON-NLS-2$
					|| xmlrpcName.equals("assigned_to") || xmlrpcName.equals("classification") //$NON-NLS-1$ //$NON-NLS-2$
					|| xmlrpcName.equals("cc") || xmlrpcName.equals("remaining_time") //$NON-NLS-1$ //$NON-NLS-2$
					|| xmlrpcName.equals("estimated_time") || xmlrpcName.equals("deadline") //$NON-NLS-1$ //$NON-NLS-2$
					|| xmlrpcName.equals("alias")) { //$NON-NLS-1$ 
				return xmlrpcName;
			} else if (xmlrpcName.equals("last_change_time")) { //$NON-NLS-1$
				return "delta_ts"; //$NON-NLS-1$
			} else if (xmlrpcName.equals("summary")) { //$NON-NLS-1$
				return "short_desc"; //$NON-NLS-1$
			} else if (xmlrpcName.equals("is_confirmed")) { //$NON-NLS-1$
				return "everconfirmed"; //$NON-NLS-1$
			} else if (xmlrpcName.equals("is_open")) { //$NON-NLS-1$
				return "status_open"; //$NON-NLS-1$
			} else if (xmlrpcName.equals("depends_on")) { //$NON-NLS-1$
				return "dependson"; //$NON-NLS-1$
			} else if (xmlrpcName.equals("creator")) { //$NON-NLS-1$
				return "reporter"; //$NON-NLS-1$
			} else if (xmlrpcName.equals("id")) { //$NON-NLS-1$
				return "bug_id"; //$NON-NLS-1$
			} else if (xmlrpcName.equals("creation_time")) { //$NON-NLS-1$
				return "creation_ts"; //$NON-NLS-1$
			} else if (xmlrpcName.equals("groups")) { //$NON-NLS-1$
				return "group"; //$NON-NLS-1$
			} else if (xmlrpcName.equals("platform")) { //$NON-NLS-1$
				return "rep_platform"; //$NON-NLS-1$
			} else if (xmlrpcName.equals("is_creator_accessible")) { //$NON-NLS-1$
				return "reporter_accessible"; //$NON-NLS-1$
			} else if (xmlrpcName.equals("status")) { //$NON-NLS-1$
				return "bug_status"; //$NON-NLS-1$
			} else if (xmlrpcName.equals("is_cc_accessible")) { //$NON-NLS-1$
				return "cclist_accessible"; //$NON-NLS-1$
			} else if (xmlrpcName.equals("severity")) { //$NON-NLS-1$
				return "bug_severity"; //$NON-NLS-1$
			} else if (xmlrpcName.equals("blocks")) { //$NON-NLS-1$
				return "blocked"; //$NON-NLS-1$
			} else if (xmlrpcName.equals("url")) { //$NON-NLS-1$
				return "bug_file_loc"; //$NON-NLS-1$
			} else if (xmlrpcName.equals("whiteboard")) { //$NON-NLS-1$
				return "status_whiteboard"; //$NON-NLS-1$
			} else if (xmlrpcName.equals("update_token")) { //$NON-NLS-1$
				return "token"; //$NON-NLS-1$
			} else if (xmlrpcName.equals("dupe_of")) { //$NON-NLS-1$
				return "dup_id"; //$NON-NLS-1$
			}
		}
		return "UNKNOWN"; //$NON-NLS-1$
	}

	public void getTaskData(Set<String> taskIds, final TaskDataCollector collector, final TaskAttributeMapper mapper,
			final IProgressMonitor monitor) throws IOException, CoreException {
		HashMap<String, TaskData> taskDataMap = new HashMap<String, TaskData>();

		taskIds = new HashSet<String>(taskIds);
		while (taskIds.size() > 0) {

			Set<String> idsToRetrieve = new HashSet<String>();
			Iterator<String> itr = taskIds.iterator();
			for (int x = 0; itr.hasNext() && x < BugzillaClient.MAX_RETRIEVED_PER_QUERY; x++) {
				String taskId = itr.next();
				String taskIdOrg = taskId;
				// remove leading zeros
				boolean changed = false;
				while (taskId.startsWith("0")) { //$NON-NLS-1$
					taskId = taskId.substring(1);
					changed = true;
				}
				idsToRetrieve.add(taskId);
				if (changed) {
					taskIds.remove(taskIdOrg);
					taskIds.add(taskId);
				}
			}
			Integer[] formData = new Integer[idsToRetrieve.size()];

			if (idsToRetrieve.size() == 0) {
				return;
			}

			RepositoryConfiguration repositoryConfiguration = bugzillaClient.getRepositoryConfiguration();
			if (repositoryConfiguration == null) {
				repositoryConfiguration = bugzillaClient.getRepositoryConfiguration(new SubProgressMonitor(monitor, 1),
						null);
			}
			List<BugzillaCustomField> customFields = new ArrayList<BugzillaCustomField>();
			if (repositoryConfiguration != null) {
				customFields = repositoryConfiguration.getCustomFields();
			}
			itr = idsToRetrieve.iterator();
			int x = 0;
			for (; itr.hasNext(); x++) {
				String taskId = itr.next();
				formData[x] = new Integer(taskId);

				TaskData taskData = new TaskData(mapper, getConnectorKind(), getLocation().getUrl(), taskId);
				bugzillaClient.setupExistingBugAttributes(getLocation().getUrl(), taskData);
				taskDataMap.put(taskId, taskData);
			}
			try {
				if (getUserID() == -1) {
					login(monitor);
				}
				Object[] result = getBugs(monitor, formData);
				HashMap<String, Object[]> resultAttachments = getAttachmentsInternal(monitor, formData);
				HashMap<String, HashMap<String, Object[]>> resultComments = getCommentsInternal(monitor, formData);
				for (Object resultMap : result) {
					if (resultMap instanceof Map<?, ?>) {
						Map<?, ?> taskDataResultMap = (Map<?, ?>) resultMap;
						Integer id = (Integer) taskDataResultMap.get("id"); //$NON-NLS-1$
						TaskData taskData = taskDataMap.get(id.toString());
						HashMap<String, Object[]> comments = resultComments.get(id.toString());
						Object[] attachments = resultAttachments.get(id.toString());
						updateTaskDataFromMap(mapper, customFields, taskDataResultMap, taskData);
						addCommentsFromHashToTaskData(mapper, taskData, comments);
						addAttachmentsFromHashToTaskData(mapper, taskData, attachments);
						collector.accept(taskData);
					}
				}

				taskIds.removeAll(idsToRetrieve);
				taskDataMap.clear();
			} catch (XmlRpcException e) {
				throw new CoreException(new Status(IStatus.ERROR, BugzillaCorePlugin.ID_PLUGIN, "XmlRpcException: ", e)); //$NON-NLS-1$				
			}
		}
	}

	@SuppressWarnings("unchecked")
	private void updateTaskDataFromMap(final TaskAttributeMapper mapper, List<BugzillaCustomField> customFields,
			Map<?, ?> taskDataResultMap, TaskData taskData) {
		for (String attrib : (Set<String>) taskDataResultMap.keySet()) {
			Object value = taskDataResultMap.get(attrib);
			if (attrib.startsWith(BugzillaCustomField.CUSTOM_FIELD_PREFIX)) {
				TaskAttribute endAttribute = taskData.getRoot().getAttribute(attrib);
				if (endAttribute == null) {
					String desc = "???"; //$NON-NLS-1$
					BugzillaCustomField customField = null;
					for (BugzillaCustomField bugzillaCustomField : customFields) {
						if (attrib.equals(bugzillaCustomField.getName())) {
							customField = bugzillaCustomField;
							break;
						}
					}
					if (customField != null) {
						TaskAttribute atr = taskData.getRoot().createAttribute(attrib);
						desc = customField.getDescription();
						atr.getMetaData().defaults().setLabel(desc).setReadOnly(false);
						atr.getMetaData().setKind(TaskAttribute.KIND_DEFAULT);
						atr.getMetaData().setType(TaskAttribute.TYPE_SHORT_TEXT);
						switch (customField.getFieldType()) {
						case FreeText:
							atr.getMetaData().setType(TaskAttribute.TYPE_SHORT_TEXT);
							break;
						case DropDown:
							atr.getMetaData().setType(TaskAttribute.TYPE_SINGLE_SELECT);
							break;
						case MultipleSelection:
							atr.getMetaData().setType(TaskAttribute.TYPE_MULTI_SELECT);
							break;
						case LargeText:
							atr.getMetaData().setType(TaskAttribute.TYPE_LONG_TEXT);
							break;
						case DateTime:
							atr.getMetaData().setType(TaskAttribute.TYPE_DATETIME);
							break;

						default:
							List<String> options = customField.getOptions();
							if (options.size() > 0) {
								atr.getMetaData().setType(TaskAttribute.TYPE_SINGLE_SELECT);
							} else {
								atr.getMetaData().setType(TaskAttribute.TYPE_SHORT_TEXT);
							}
						}
						atr.getMetaData().setReadOnly(false);
						if (value instanceof String || value instanceof Boolean || value instanceof Integer
								|| value instanceof Date) {
							atr.setValue(getValueStringFromObject(value, false));
						} else if (value instanceof Object[]) {
							for (Object object1 : (Object[]) value) {
								if (object1 instanceof String || object1 instanceof Boolean
										|| object1 instanceof Integer || object1 instanceof Date) {
									atr.addValue(getValueStringFromObject(object1, true));
								}
							}
						}
					}
				} else {
					if (value instanceof String || value instanceof Boolean || value instanceof Integer
							|| value instanceof Date) {
						endAttribute.addValue(getValueStringFromObject(value, false));
					} else if (value instanceof Object[]) {
						for (Object object1 : (Object[]) value) {
							if (object1 instanceof String || object1 instanceof Boolean || object1 instanceof Integer
									|| object1 instanceof Date) {
								endAttribute.addValue(getValueStringFromObject(object1, true));
							}
						}
					}
				}
				continue;
			}
			BugzillaAttribute tag = BugzillaAttribute.UNKNOWN;
			try {
				String key = getMappedBugzillaAttribute(attrib);
				tag = BugzillaAttribute.valueOf(key.toUpperCase(Locale.ENGLISH));
			} catch (RuntimeException e) {
				if (e instanceof IllegalArgumentException) {
					// ignore unrecognized tags
					continue;
				}
				throw e;
			}
			switch (tag) {
			case UNKNOWN:
				continue;
			default:
				createAttrribute(taskData, mapper, value, tag, true);
				break;
			}
		}
	}

	private void addAttachmentsFromHashToTaskData(final TaskAttributeMapper mapper, TaskData taskData,
			Object[] attachments) {
		if (attachments != null) {
			for (Object attachmentTemp : attachments) {
				// We have the following information which are not used:
				// (Date) attachment.get("last_change_time");
				// (Integer) attachment.get("bug_id");
				// (Integer) attachment.get("is_private");
				// (Integer) attachment.get("is_url");

				HashMap<?, ?> attachment = (HashMap<?, ?>) attachmentTemp;
				Date creation_time = (Date) attachment.get("creation_time"); //$NON-NLS-1$
				Integer id = (Integer) attachment.get("id"); //$NON-NLS-1$
				String file_name = (String) attachment.get("file_name"); //$NON-NLS-1$
				String summary = (String) attachment.get("summary"); //$NON-NLS-1$
				String content_type = (String) attachment.get("content_type"); //$NON-NLS-1$

				Integer is_obsolete = (Integer) attachment.get("is_obsolete"); //$NON-NLS-1$
				Integer is_patch = (Integer) attachment.get("is_patch"); //$NON-NLS-1$
				String creator = (String) attachment.get("creator"); //$NON-NLS-1$
				TaskAttribute attachmentAttribute = taskData.getRoot().createAttribute(
						TaskAttribute.PREFIX_ATTACHMENT + id);
				BugzillaAttachmentMapper attachmentMapper = BugzillaAttachmentMapper.createFrom(attachmentAttribute);
				attachmentMapper.setAttachmentId(id.toString());
				IRepositoryPerson author = taskData.getAttributeMapper().getTaskRepository().createPerson(creator);
				author.setName(creator);
				attachmentMapper.setAuthor(author);
//				attachmentMapper.setComment(summary);
				attachmentMapper.setContentType(content_type);
				attachmentMapper.setCreationDate(creation_time);
				attachmentMapper.setDeprecated(is_obsolete.equals("1")); //$NON-NLS-1$
				attachmentMapper.setDescription(summary);
				attachmentMapper.setFileName(file_name);
				attachmentMapper.setLength(-1L);
				attachmentMapper.setPatch(is_patch.equals("1")); //$NON-NLS-1$
				attachmentMapper.applyTo(attachmentAttribute);
			}
		}
	}

	private void addCommentsFromHashToTaskData(final TaskAttributeMapper mapper, TaskData taskData,
			HashMap<String, Object[]> comments) {
		if (comments != null) {
			Object[] commentArray = comments.get("comments"); //$NON-NLS-1$
			if (commentArray != null) {
				int commentNum = 0;
				for (Object object2 : commentArray) {
					@SuppressWarnings("unchecked")
					HashMap<String, Object> commentHash = (HashMap<String, Object>) object2;
					String text = (String) commentHash.get("text"); //$NON-NLS-1$
					if (commentNum == 0) {
						createAttrribute(taskData, mapper, text, BugzillaAttribute.LONG_DESC, true);
						commentNum++;
						continue;
					}
					TaskAttribute attribute = taskData.getRoot().createAttribute(
							TaskAttribute.PREFIX_COMMENT + commentNum);
					TaskCommentMapper taskComment = TaskCommentMapper.createFrom(attribute);
					Integer commentID = (Integer) commentHash.get("id"); //$NON-NLS-1$
					Date time = (Date) commentHash.get("time"); //$NON-NLS-1$
					String creator = (String) commentHash.get("creator"); //$NON-NLS-1$
					Boolean is_private = (Boolean) commentHash.get("is_private"); //$NON-NLS-1$
					@SuppressWarnings("unused")
					Integer attachment_id = (Integer) commentHash.get("attachment_id"); //$NON-NLS-1$

					taskComment.setCommentId(commentID.toString());
					taskComment.setNumber(commentNum);
					IRepositoryPerson author = taskData.getAttributeMapper().getTaskRepository().createPerson(creator);
					author.setName(creator);
					taskComment.setAuthor(author);
					taskComment.setIsPrivate(is_private);
					TaskAttribute attrTimestamp = attribute.createAttribute(BugzillaAttribute.BUG_WHEN.getKey());
					attrTimestamp.setValue(getValueStringFromObject(time, false));
					try {
						SimpleDateFormat x0 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss", Locale.US); //$NON-NLS-1$
						String dateString = x0.format(time);
						SimpleDateFormat x1 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss ZZZ"); //$NON-NLS-1$
						Date x2 = x1.parse(dateString + " GMT"); //$NON-NLS-1$

						taskComment.setCreationDate(x2);
					} catch (ParseException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
					taskComment.setText(text.trim());
					taskComment.applyTo(attribute);
					commentNum++;
				}
			}
		}

	}

	private String getValueStringFromObject(Object value, boolean dateWithTimezone) {
		if (value instanceof String) {
			return (String) value;
		} else if (value instanceof Boolean) {
			return (Boolean) value ? "1" : "0"; //$NON-NLS-1$ //$NON-NLS-2$
		} else if (value instanceof Integer) {
			return ((Integer) value).toString();
		} else if (value instanceof Double) {
			NumberFormat numberInstance = NumberFormat.getInstance(Locale.US);//NumberFormat.getNumberInstance();
			numberInstance.setMaximumFractionDigits(2);
			numberInstance.setMinimumFractionDigits(2);
			return numberInstance.format(value);
		} else if (value instanceof Date) {
			try {
				Date y = (Date) value;
				SimpleDateFormat x0 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss", Locale.US); //$NON-NLS-1$
				String dateString = x0.format(y);
				SimpleDateFormat x1 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss ZZZ"); //$NON-NLS-1$
				Date x2 = x1.parse(dateString + " GMT"); //$NON-NLS-1$
				return dateWithTimezone ? x1.format(x2) : x1.format(x2).substring(0, 10) + " 00:00:00"; //$NON-NLS-1$
			} catch (ParseException e) {
				return ""; //$NON-NLS-1$
			}
		}
		return null;
	}

	private TaskAttribute createAttrribute(TaskData taskData, TaskAttributeMapper mapper, Object value,
			BugzillaAttribute tag, boolean clearValueFirst) {
		TaskAttribute attribute = taskData.getRoot().getMappedAttribute(tag.getKey());
		if (value instanceof String || value instanceof Boolean || value instanceof Integer || value instanceof Date
				|| value instanceof Double) {
			if (attribute == null) {
				attribute = BugzillaTaskDataHandler.createAttribute(taskData, tag);
				attribute.setValue(getValueStringFromObject(value, true));
			} else {
				if (clearValueFirst) {
					attribute.clearValues();
				}
				attribute.addValue(getValueStringFromObject(value, true));
			}
		} else if (value instanceof Object[]) {
			if (attribute == null) {
				attribute = BugzillaTaskDataHandler.createAttribute(taskData, tag);
			} else if (clearValueFirst) {
				attribute.clearValues();
			}
			String valueList = ""; //$NON-NLS-1$
			if (tag.equals(BugzillaAttribute.DEPENDSON) || tag.equals(BugzillaAttribute.BLOCKED)
					|| tag.equals(BugzillaAttribute.KEYWORDS)) {
				for (Object object : (Object[]) value) {
					if (valueList.equals("")) { //$NON-NLS-1$
						if (object instanceof String || object instanceof Boolean || object instanceof Integer
								|| object instanceof Date) {
							valueList = getValueStringFromObject(object, true);
						}
					} else {
						if (object instanceof String || object instanceof Boolean || object instanceof Integer
								|| object instanceof Date) {
							valueList += ", " + getValueStringFromObject(object, true); //$NON-NLS-1$
						}
					}
				}
				attribute.setValue(valueList);
			} else {
				for (Object object : (Object[]) value) {
					if (object instanceof String || object instanceof Boolean || object instanceof Integer
							|| object instanceof Date) {
						attribute.addValue(getValueStringFromObject(object, true));
					}
				}

			}
			return attribute;
		}
		return null;
	}

}

Back to the top