Skip to main content

This CGIT instance is deprecated, and repositories have been moved to Gitlab or Github. See the repository descriptions for specific locations.

summaryrefslogtreecommitdiffstats
blob: ca904f1f8cf8792d059a492269293c4f505c6d63 (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
/*******************************************************************************
 * Copyright (c) 2001, 2004 IBM Corporation 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:
 * IBM Corporation - initial API and implementation
 *******************************************************************************/
package org.eclipse.jst.j2ee.internal.migration;

import java.text.MessageFormat;

import org.eclipse.emf.ecore.ENamedElement;
import org.eclipse.emf.ecore.EObject;
import org.eclipse.jst.j2ee.internal.common.XMLResource;


/**
 * @author DABERG
 *
 */
public class J2EEMigrationStatus implements J2EESpecificationMigrationConstants { 
    
	public static final int COMPLETED_OK = 0;
	public static final int NOT_NEEDED = 1;
	public static final int NOT_POSSIBLE = 2;
	public static final int WARNING = 3;
	public static final int ERROR = 4; 

    public static final J2EEMigrationStatus OK_STATUS = new J2EEMigrationStatus(COMPLETED_OK, ""); //$NON-NLS-1$

	private static final J2EEMigrationStatus[] EmptyStatusArray = new J2EEMigrationStatus[0];

	private int severity;
	private String message;
	private XMLResource resource;
	private EObject targetObject;
	
	J2EEMigrationStatus() {
		//Default
	}

	public J2EEMigrationStatus(int aSeverity, String aMessage) {
		this(aSeverity, (EObject)null, aMessage);
	}
	public J2EEMigrationStatus(int aSeverity, XMLResource xmlResource) {
		this(aSeverity, xmlResource, null);
	}
	
	public J2EEMigrationStatus(int aSeverity, EObject anObject) {
		this(aSeverity, anObject, null);
	}

	public J2EEMigrationStatus(int aSeverity, XMLResource xmlResource, String aMessage) {
		severity = aSeverity;
		resource = xmlResource;
		message = aMessage;
	}
	
	public J2EEMigrationStatus(int aSeverity, EObject anObject, String aMessage) {
		severity = aSeverity;
		targetObject = anObject;
		message = aMessage;
	}
	
	protected String format(String aPattern, String arg1) {
		return MessageFormat.format(aPattern, new String[]{arg1});
	}
	
	/**
	 * Returns the message.
	 * @return String
	 */
	public String getMessage() {
		if (message == null)
			message = createDefaultMessage();
		return message;
	}
	
	public String getMessageForDisplay() {
		return getSeverityText()+" "+getMessage(); //$NON-NLS-1$
	}
	
	protected String getSeverityText() {
		switch (severity) {
			case ERROR:
				return ERROR_TEXT;
			case WARNING:
				return WARNING_TEXT;
			case NOT_NEEDED:
			case NOT_POSSIBLE:
				return INFO_TEXT;
			default:
				return ""; //$NON-NLS-1$
		}
	}

	/**
	 * Method createDefaultMessage.
	 * @return String
	 */
	private String createDefaultMessage() {
		String objDesc;
		if (getResource() != null)
			objDesc = getResource().getURI().toString();
		else if (getTargetObject() != null && getTargetObject() instanceof ENamedElement)
			objDesc = ((ENamedElement) getTargetObject()).getName();
		else
			return null;
		switch (getSeverity()) {
			case COMPLETED_OK :
				return format(DEFAULT_COMPLETED_STATUS_MSG, objDesc);
			case NOT_NEEDED :
				return format(DEFAULT_NOT_NEEDED_STATUS_MSG, objDesc);
			case NOT_POSSIBLE :
				return format(DEFAULT_NOT_POSSIBLE_STATUS_MSG, objDesc);
			case ERROR :
				return format(DEFAULT_ERROR_STATUS_MSG, objDesc);
		}
		return null;
	}


	/**
	 * Sets the message.
	 * @param message The message to set
	 */
	public void setMessage(String message) {
		this.message = message;
	}

	/**
	 * The migration was completed fine or was not needed.
	 */
	public boolean isOK() {
		return severity == COMPLETED_OK || severity == NOT_NEEDED;
	}

	/**
	 * An error ocurred during migration.
	 */
	public boolean isError() {
		return severity == ERROR;
	}
	
	/**
	 * A warning ocurred during migration.
	 */
	public boolean isWarning() {
		return severity == WARNING;
	}

	/**
	 * The migration was not required.
	 */
	public boolean isNotNeeded() {
		return severity == NOT_NEEDED;
	}
	/**
	 * The migration was not possible.
	 */
	public boolean isNotPossible() {
		return severity == NOT_POSSIBLE;
	}
	/**
	 * Returns the severity.
	 * @return int
	 */
	public int getSeverity() {
		return severity;
	}

	/**
	 * Sets the severity.
	 * @param severity The severity to set
	 */
	public void setSeverity(int severity) {
		this.severity = severity;
	}

	public J2EEMigrationStatus[] getChildren() {
		return EmptyStatusArray;
	}

	public boolean isMultiStatus() {
		return false;
	}
	public XMLResource getResource() {
		return resource;
	}
	public EObject getTargetObject() {
		return targetObject;
	}

	public String toString() {
		StringBuffer buf = new StringBuffer();
		buf.append("MigrationStatus "); //$NON-NLS-1$
		if (severity == COMPLETED_OK) {
			buf.append("Completed OK"); //$NON-NLS-1$
		} else if (severity == ERROR) {
			buf.append("ERROR"); //$NON-NLS-1$
		} else if (severity == WARNING) {
			buf.append("WARNING"); //$NON-NLS-1$
		} else if (severity == NOT_NEEDED) {
			buf.append("Not Needed"); //$NON-NLS-1$
		} else if (severity == NOT_POSSIBLE) {
			buf.append("Not Possible"); //$NON-NLS-1$
		} else {
			buf.append("severity="); //$NON-NLS-1$
			buf.append(severity);
		}
		buf.append(' ');
		buf.append(message);
		return buf.toString();
	}
	
	/**
	 * Append the paramater to this status, and return the resultant status;
	 * clients must be careful to set their cached status to the return value, 
	 * as it may be a new instance.
	 */
	public J2EEMigrationStatus append(J2EEMigrationStatus aStatus) {
		if (aStatus == null)
			return this;
		J2EEMigrationMultiStatus multi = new J2EEMigrationMultiStatus();
		multi.merge(this);
		multi.merge(aStatus);
		return multi;
	}
}

Back to the top