Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 2769e116db066fc46ea2e4fa3bb1e2370e00a7a8 (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
/*******************************************************************************
 * Copyright (c) 2015 Ericsson AB.
 * 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:
 *     Jacques Bouthillier - initial API and implementation
 *******************************************************************************/

package org.eclipse.egerrit.internal.ui.utils;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Collection;
import java.util.Date;
import java.util.Iterator;
import java.util.TimeZone;

import org.eclipse.core.databinding.conversion.Converter;
import org.eclipse.core.databinding.conversion.IConverter;
import org.eclipse.egerrit.internal.core.EGerritCorePlugin;
import org.eclipse.egerrit.internal.core.GerritClient;
import org.eclipse.egerrit.internal.model.ChangeInfo;
import org.eclipse.egerrit.internal.model.ChangeMessageInfo;
import org.eclipse.egerrit.internal.model.CommentInfo;
import org.eclipse.egerrit.internal.model.FileInfo;
import org.eclipse.egerrit.internal.model.GitPersonInfo;
import org.eclipse.egerrit.internal.model.MergeableInfo;
import org.eclipse.egerrit.internal.model.RevisionInfo;
import org.eclipse.egerrit.internal.ui.editors.QueryHelpers;
import org.eclipse.egerrit.internal.ui.table.model.SubmitType;
import org.eclipse.jface.text.Document;
import org.eclipse.jface.text.IDocument;
import org.eclipse.osgi.util.NLS;

/**
 * This class in used to transform the Gerrit data to a databinding display value
 *
 * @since 1.0
 */
public class DataConverter {

	/**
	 * The default constructor. Do not allow to build an object of this class
	 */
	private DataConverter() {
	}

	/**
	 * @param outputTime
	 *            SimpleDateFormat requested
	 * @return an IConverter from the Gerrit Timestamp to a new format
	 */
	public static IConverter gerritTimeConverter(final SimpleDateFormat outputTime) {
		return new Converter(String.class, String.class) {

			@Override
			public Object convert(Object fromObject) {

				SimpleDateFormat formatIn = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.");// Gerrit timestamp format //$NON-NLS-1$

				Date dateNew = null;

				if (fromObject != null && !"".equals(fromObject)) { //$NON-NLS-1$
					try {
						formatIn.setTimeZone(TimeZone.getTimeZone("UTC")); //$NON-NLS-1$
						dateNew = formatIn.parse((String) fromObject);
					} catch (ParseException ex) {
						EGerritCorePlugin.logError(ex.getMessage());
					}
					return outputTime.format(dateNew);

				}
				return ""; //$NON-NLS-1$

			}
		};

	}

	/**
	 * @return an IConverter from the GitPersonInfo structure to a format having the name and e-mail information
	 */
	public static IConverter gitPersonConverter() {
		return new Converter(GitPersonInfo.class, String.class) {
			@Override
			public Object convert(Object fromObject) {

				if (fromObject != null && !"".equals(fromObject)) { //$NON-NLS-1$
					GitPersonInfo person = (GitPersonInfo) fromObject;
					if (person.getName() != null && person.getEmail() != null) {
						// "<a>  </a> is to allow the link selection of the text
						return "<a>" + person.getName() + "<" + person.getEmail() + ">" + "</a>"; //$NON-NLS-1$//$NON-NLS-2$ //$NON-NLS-3$ //$NON-NLS-4$
					} else {
						return null;
					}
				} else {
					return null;
				}
			}
		};

	}

	/**
	 * @return an IConverter from the Submit type structure to a format to display
	 */
	public static IConverter submitTypeConverter() {
		return new Converter(MergeableInfo.class, String.class) {

			@Override
			public Object convert(Object fromObject) {

				if (fromObject != null && !"".equals(fromObject)) { //$NON-NLS-1$
					MergeableInfo mInfo = (MergeableInfo) fromObject;
					return SubmitType.getEnumName(mInfo.getSubmit_type());
				} else {
					return null;
				}
			}
		};

	}

	/**
	 * @return an IConverter from the boolean structure to the "Cannot Merge" to display
	 */
	public static IConverter cannotMergeConverter() {
		return new Converter(Boolean.class, String.class) {

			@Override
			public Object convert(Object fromObject) {

				if (fromObject != null && !"".equals(fromObject)) { //$NON-NLS-1$
					return new Boolean((boolean) fromObject).booleanValue() ? "" : Messages.DataConverter_0; //$NON-NLS-1$
				} else {
					return null;
				}
			}
		};

	}

	/**
	 * Insert the tag for the link text
	 *
	 * @return
	 */
	public static IConverter linkText() {
		return new Converter(String.class, String.class) {

			@Override
			public Object convert(Object fromObject) {
				if (fromObject == null) {
					return ""; //$NON-NLS-1$
				}
				return new StringBuilder().append("<a>").append(fromObject).append("</a>").toString(); //$NON-NLS-1$ //$NON-NLS-2$
			}
		};
	}

	/**
	 * Convert a string to a Document
	 *
	 * @param gerritClient
	 * @param selection
	 * @param changeInfo
	 * @param changeInfo
	 * @param selection
	 * @param gerritClient
	 * @return IConverter
	 */
	public static IConverter fromStringToDocument(GerritClient gerritClient) {
		return new Converter(String.class, IDocument.class) {

			@Override
			public Object convert(Object fromObject) {
				if (fromObject == null) {
					return null;
				}
				ChangeMessageInfo message = (ChangeMessageInfo) fromObject;

				//There is no comment
				if (!message.isComment()) {
					return new Document(message.getMessage());
				}

				//Get comments and format the message with those
				ChangeInfo containingChange = (ChangeInfo) message.eContainer();
				RevisionInfo selectedRevision = containingChange.getRevisionByNumber(message.get_revision_number());
				if (selectedRevision != null) {
					QueryHelpers.loadComments(gerritClient, selectedRevision);
				}

				return new Document(message.getMessage() + "\n" + formatMessageWithComments(message, selectedRevision)); //$NON-NLS-1$
			}
		};

	}

	/**
	 * Creating a string with the file path and associated comments to this file.
	 *
	 * @param chmsgInfo
	 * @param revInfo
	 * @return String
	 */
	private static String formatMessageWithComments(ChangeMessageInfo chmsgInfo, RevisionInfo revInfo) {
		StringBuilder sb = new StringBuilder();
		if (revInfo != null) {
			Collection<FileInfo> files = revInfo.getFiles().values();
			for (FileInfo fileInfo : files) {
				if (!fileInfo.getComments().isEmpty()) {
					sb.append("\n"); //$NON-NLS-1$
					sb.append(fileInfo.getPath());

					Iterator<CommentInfo> commentsIter = fileInfo.getComments().iterator();
					while (commentsIter.hasNext()) {
						//List the comments available for this timestamp only
						CommentInfo comment = commentsIter.next();
						if (chmsgInfo.getDate().equals(comment.getUpdated())) {
							sb.append(NLS.bind(Messages.DataConverter_4,
									new Object[] { comment.getLine(), comment.getMessage() }));
						}
					}
					sb.append("\n"); //$NON-NLS-1$
				}
			}
		}
		return sb.toString();
	}

	/**
	 * Insert the tag for the link text
	 *
	 * @param fChangeInfo
	 * @return
	 */
	public static IConverter patchSetSelected(ChangeInfo changeInfo) {
		return new Converter(String.class, String.class) {
			@Override
			public Object convert(Object fromObject) {
				if (fromObject == null || changeInfo.getUserSelectedRevision() == null) {
					//Force this string to be long to reserve the space because the first time things get rendered there is no selection
					return "                               "; //$NON-NLS-1$
				}
				RevisionInfo revInfo = changeInfo.getUserSelectedRevision();
				StringBuilder sb = new StringBuilder();
				sb.append(Messages.DataConverter_5);
				sb.append(revInfo.get_number());
				sb.append("/"); //$NON-NLS-1$
				sb.append(changeInfo.getRevision().get_number());
				return sb.toString();
			}
		};
	}
}

Back to the top