Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 31d2e3a3842a1e0f3bd770a9dad25dd912c4788d (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
/*******************************************************************************
 * <copyright>
 *
 * Copyright (c) 2012 SAP AG 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:
 *    Petya Sabeva - initial API, implementation and documentation
 *
 * </copyright>
 *
 *******************************************************************************/

package org.eclipse.jpt.jpadiagrameditor.ui.internal.command;

import java.util.Iterator;
import java.util.List;
import java.util.Locale;
import java.util.Properties;

import org.eclipse.core.resources.IProject;
import org.eclipse.core.runtime.NullProgressMonitor;
import org.eclipse.jdt.core.ICompilationUnit;
import org.eclipse.jdt.core.IType;
import org.eclipse.jdt.core.JavaModelException;
import org.eclipse.jpt.common.core.resource.java.JavaResourceType;
import org.eclipse.jpt.common.utility.command.Command;
import org.eclipse.jpt.jpa.core.context.PersistentType;
import org.eclipse.jpt.jpadiagrameditor.ui.internal.JPADiagramEditorPlugin;
import org.eclipse.jpt.jpadiagrameditor.ui.internal.propertypage.JPADiagramPropertyPage;
import org.eclipse.jpt.jpadiagrameditor.ui.internal.provider.IJPAEditorFeatureProvider;
import org.eclipse.jpt.jpadiagrameditor.ui.internal.util.JPAEditorConstants;
import org.eclipse.jpt.jpadiagrameditor.ui.internal.util.JPAEditorUtil;
import org.eclipse.jpt.jpadiagrameditor.ui.internal.util.JpaArtifactFactory;

/**
 * A new {@link Command} class that is called to create a new attribute in the
 * selected persistent type.
 * 
 * @author i045693
 * 
 */
public class AddAttributeCommand implements Command {

	private IJPAEditorFeatureProvider fp;
	private PersistentType jpt;
	private String attributeType;
	private String mapKeyType;
	private String attributeName;
	private String actName;
	private String[] attrTypes;
	private List<String> annotations;
	private boolean isCollection;
	private ICompilationUnit cu;

	/**
	 * Constructor for the create new attribute command.
	 * 
	 * @param fp
	 * @param jpt
	 * @param attributeType
	 * @param mapKeyType
	 * @param attributeName
	 * @param actName
	 * @param isCollection
	 * @param cu
	 */
	public AddAttributeCommand(IJPAEditorFeatureProvider fp,
			PersistentType jpt, String attributeType, String mapKeyType,
			String attributeName, String actName, String[] attrTypes,
			List<String> annotations,
			boolean isCollection, ICompilationUnit cu) {
		super();
		this.fp = fp;
		this.jpt = jpt;
		this.attributeType = attributeType;
		this.mapKeyType = mapKeyType;
		this.attributeName = attributeName;
		this.actName = actName;
		this.attrTypes = attrTypes;
		this.annotations = annotations;
		this.isCollection = isCollection;
		this.cu = cu;
	}

	/**
	 * Creates a new attribute.
	 */
	public void execute() {
		IType type = null;
		try {
//			if(jpt != null){
//				cu = JPAEditorUtil.getCompilationUnit(jpt);
//			}
			JPAEditorUtil.createImport(cu, attributeType);
			attributeType = JPAEditorUtil.returnSimpleName(attributeType);
			type = cu.findPrimaryType();

			if ((attrTypes != null) && (attrTypes.length > 0)) {
				JPAEditorUtil.createImports(cu, attrTypes);
			}

			String contents = ""; //$NON-NLS-1$
			if (annotations != null) {
				Iterator<String> it = annotations.iterator();
				while (it.hasNext()) {
					String an = it.next();
					contents += "   " + an + "\n"; //$NON-NLS-1$ //$NON-NLS-2$
				}
			}
			
			createAttribute(fp, jpt, attributeType, mapKeyType, attributeName,
					actName, cu, type, isCollection, attrTypes, contents);

			if(jpt != null) {
				jpt.getJpaProject().getContextModelRoot().synchronizeWithResourceModel();
				JavaResourceType jrt = jpt.getJavaResourceType();
				jrt.getJavaResourceCompilationUnit().synchronizeWithJavaSource();
				jpt.update();
			}

		} catch (JavaModelException e) {
			JPADiagramEditorPlugin
					.logError(
							"Cannnot create a new attribute with name " + attributeName, e); //$NON-NLS-1$				
		}
	}

	/**
	 * Creates an attribute the persistent type.
	 * 
	 * @param fp
	 * @param jpt
	 * @param attrTypeName
	 * @param mapKeyType
	 * @param attrName
	 * @param actName
	 * @param cu
	 * @param type
	 * @param isCollection
	 * @throws JavaModelException
	 */
	private void createAttribute(IJPAEditorFeatureProvider fp,
			PersistentType jpt, String attrTypeName, String mapKeyType,
			String attrName, String actName, ICompilationUnit cu, IType type,
			boolean isCollection, String[] attrTypeElementNames, String annotationContents) throws JavaModelException {

			if (isCollection) {
				createAttributeOfCollectiontype(fp, jpt, attrTypeName,
						mapKeyType, attrName, actName, cu, type);
			} else {
				createSimpleAttribute(attrTypeName, attrName, actName,
						isCollection, type, attrTypeElementNames, annotationContents);
			}
	}

	/**
	 * Creates a new attribute of a basic type.
	 * 
	 * @param attributeType
	 * @param attributeName
	 * @param actName
	 * @param isCollection
	 * @param type
	 * @throws JavaModelException
	 */
	private void createSimpleAttribute(String attributeType,
			String attributeName, String actName, boolean isCollection,
			IType type, String[] attrTypeElementNames, String annotationContents) throws JavaModelException {
		
		
		String attrFieldContent = "    private " + attributeType + //$NON-NLS-1$
				((attrTypes == null) ? "" : ("<" + JPAEditorUtil.createCommaSeparatedListOfSimpleTypeNames(attrTypes) + ">")) + //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
				" " + JPAEditorUtil.decapitalizeFirstLetter(actName) + ";"; //$NON-NLS-1$ //$NON-NLS-2$
		
		String contents = ""; //$NON-NLS-1$
		if(jpt!= null && !JpaArtifactFactory.instance().isMethodAnnotated(jpt)){
			contents = annotationContents + attrFieldContent;
		} else {
			contents = attrFieldContent;
		}
		
		type.createField(contents, null, false, new NullProgressMonitor());
		
		type.createMethod(
				genGetterContents(attributeName, attributeType, actName, attrTypeElementNames, annotationContents), null,
				false, new NullProgressMonitor());
		type.createMethod(
				genSetterContents(attributeName, attributeType, actName, attrTypeElementNames), null,
				false, new NullProgressMonitor());
	}

	/**
	 * Creates a new attribute of a collection type, depending on the specified
	 * collection type in the Preference/Properties page.
	 * 
	 * @param fp
	 * @param jpt
	 * @param attributeType
	 * @param mapKeyType
	 * @param attributeName
	 * @param actName
	 * @param cu
	 * @param type
	 * @throws JavaModelException
	 */
	private void createAttributeOfCollectiontype(IJPAEditorFeatureProvider fp,
			PersistentType jpt, String attributeType, String mapKeyType,
			String attributeName, String actName, ICompilationUnit cu,
			IType type) throws JavaModelException {
		IProject project = jpt.getJpaProject().getProject();
		Properties props = fp.loadProperties(project);
		if (JPADiagramPropertyPage.isCollectionType(project, props)) {
			createAttributeByCollectionMethodType(attributeType, null,
					attributeName, actName, cu, type,
					JPAEditorConstants.COLLECTION_TYPE);
		} else if (JPADiagramPropertyPage.isListType(project, props)) {
			createAttributeByCollectionMethodType(attributeType, null,
					attributeName, actName, cu, type,
					JPAEditorConstants.LIST_TYPE);
		} else if (JPADiagramPropertyPage.isSetType(project, props)) {
			createAttributeByCollectionMethodType(attributeType, null,
					attributeName, actName, cu, type,
					JPAEditorConstants.SET_TYPE);
		} else {
			createAttributeByCollectionMethodType(attributeType, mapKeyType,
					attributeName, actName, cu, type,
					JPAEditorConstants.MAP_TYPE);
		}
	}

	/**
	 * Create attribute by the specified collection type in the
	 * Preference/Properties page.
	 * 
	 * @param attributeType
	 * @param mapKeyType
	 * @param attributeName
	 * @param actName
	 * @param cu
	 * @param type
	 * @param collectionType
	 * @throws JavaModelException
	 */
	private void createAttributeByCollectionMethodType(String attributeType,
			String mapKeyType, String attributeName, String actName,
			ICompilationUnit cu, IType type, String collectionType)
			throws JavaModelException {
		mapKeyType = createContentType(mapKeyType, attributeType, actName, cu,
				type, collectionType);
		type.createMethod(
				genGetterWithAppropriateType(attributeName, mapKeyType,
						attributeType, actName, collectionType), null, false,
				new NullProgressMonitor());
		type.createMethod(
				genSetterWithAppropriateType(attributeName, mapKeyType,
						attributeType, actName, collectionType), null, false,
				new NullProgressMonitor());
	}

	/**
	 * Create field in the entity by the specified collection type.
	 * 
	 * @param mapKeyType
	 * @param attributeType
	 * @param actName
	 * @param cu
	 * @param type
	 * @param collectionType
	 * @return string representation of the field's collection type.
	 * @throws JavaModelException
	 */
	private String createContentType(String mapKeyType, String attributeType,
			String actName, ICompilationUnit cu, IType type,
			String collectionType) throws JavaModelException {

		if (mapKeyType != null) {
			mapKeyType = JPAEditorUtil.createImport(cu, mapKeyType);
		}
		JPAEditorUtil.createImport(cu, collectionType);
		type.createField(
				"  private " + JPAEditorUtil.returnSimpleName(collectionType) + "<" + //$NON-NLS-1$ //$NON-NLS-2$
						((mapKeyType != null) ? (mapKeyType + ", ") : "") + //$NON-NLS-1$ //$NON-NLS-2$
						attributeType
						+ "> " + JPAEditorUtil.decapitalizeFirstLetter(actName) + //$NON-NLS-1$
						";", null, false, new NullProgressMonitor()); //$NON-NLS-1$ 
		return mapKeyType;
	}

	/**
	 * Create the attribute's getter method in entity's compilation unit with
	 * the appropriate collection type, selected in the Preference/Properties
	 * page.
	 * 
	 * @param attrName
	 *            - the name of the attribute
	 * @param attrType
	 *            - the type of the attribute
	 * @param attrTypeElementNames
	 * @param actName
	 * @param isCollection
	 * @return the string representation of the attribute's getter method.
	 */
	private String genGetterWithAppropriateType(String attrName,
			String mapKeyType, String attrType, String actName, String type) {

		String attrNameWithCapitalA = actName.substring(0, 1).toUpperCase(
				Locale.ENGLISH)
				+ actName.substring(1);
		String contents = "    public " + JPAEditorUtil.returnSimpleName(type) + //$NON-NLS-1$
				"<" //$NON-NLS-1$
				+ ((mapKeyType != null) ? (mapKeyType + ", ") : "") + attrType + "> " + //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
				"get" + attrNameWithCapitalA + "() {\n" + //$NON-NLS-1$ //$NON-NLS-2$
				"        return " //$NON-NLS-1$
				+ JPAEditorUtil.decapitalizeFirstLetter(actName) + ";\n" + //$NON-NLS-1$
				"    }\n"; //$NON-NLS-1$
		return contents;
	}

	/**
	 * Create the attribute's setter method in entity's compilation unit with
	 * the appropriate collection type, selected in the Preference/Properties
	 * page.
	 * 
	 * @param attrName
	 *            - the name of the attribute
	 * @param attrType
	 *            - the type of the attribute
	 * @param attrTypeElementNames
	 * @param actName
	 * @param isCollection
	 * @return the string representation of the attribute's setter method.
	 */
	private String genSetterWithAppropriateType(String attrName,
			String mapKeyType, String attrType, String actName, String type) {

		String attrNameWithCapitalA = actName.substring(0, 1).toUpperCase(
				Locale.ENGLISH)
				+ actName.substring(1);
		String contents = "    public void set" + attrNameWithCapitalA + //$NON-NLS-1$
				"(" //$NON-NLS-1$
				+ JPAEditorUtil.returnSimpleName(type)
				+ 
				"<" //$NON-NLS-1$
				+ ((mapKeyType != null) ? (mapKeyType + ", ") : "") + attrType + "> param) " + //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
				"{\n" //$NON-NLS-1$
				+ 
				"        this." //$NON-NLS-1$
				+ JPAEditorUtil.decapitalizeFirstLetter(actName)
				+ " = param;\n" + //$NON-NLS-1$
				"    }\n"; //$NON-NLS-1$
		return contents;
	}

	/**
	 * Create the attribute's getter method in entity's compilation unit.
	 * 
	 * @param attrName
	 *            - the name of the attribute
	 * @param attrType
	 *            - the type of the attribute
	 * @param actName
	 * @param annotationContents - the annotations as string representation
	 * @return the string representation of the attribute's getter method.
	 */
	private String genGetterContents(String attrName, String attrType,
			String actName, String[] attrTypeElementNames, String annotationContents) {
		
		String attrNameWithCapitalA = actName.substring(0, 1).toUpperCase(
				Locale.ENGLISH)
				+ actName.substring(1);
		
		String contents = ""; //$NON-NLS-1$
		if(jpt != null && JpaArtifactFactory.instance().isMethodAnnotated(jpt)){
			contents += annotationContents;
		}
		contents += "    public " + attrType + //$NON-NLS-1$
				((attrTypeElementNames == null) ? "" : ("<" + JPAEditorUtil.createCommaSeparatedListOfSimpleTypeNames(attrTypeElementNames) + ">")) + //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
				(attrType.equals("boolean") ? " is" : " get") + attrNameWithCapitalA + "() {\n" + //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ //$NON-NLS-4$ 
				"        return " //$NON-NLS-1$
				+ JPAEditorUtil.decapitalizeFirstLetter(actName) + ";\n" + //$NON-NLS-1$ 
				"    }\n"; //$NON-NLS-1$

		return contents;
	}

	/**
	 * Create the attribute's setter method in entity's compilation unit.
	 * 
	 * @param attrName
	 *            - the name of the attribute
	 * @param attrType
	 *            - the type of the attribute
	 * @return the string representation of the attribute's setter method.
	 */
	private String genSetterContents(String attrName, String attrType,
			String actName, String[] attrTypeElementNames) {
		
		String attrNameWithCapitalA = actName.substring(0, 1).toUpperCase(
				Locale.ENGLISH) + actName.substring(1);

		String contents = "    public void set" + attrNameWithCapitalA + "(" + attrType + //$NON-NLS-1$ //$NON-NLS-2$
				((attrTypeElementNames == null) ? "" : ("<" + JPAEditorUtil.createCommaSeparatedListOfSimpleTypeNames(attrTypeElementNames) + ">")) + //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
				" param) {\n" //$NON-NLS-1$
				+ "        this." //$NON-NLS-1$
				+ JPAEditorUtil.decapitalizeFirstLetter(actName)
				+ " = param;\n" + //$NON-NLS-1$ 
				"    }\n"; //$NON-NLS-1$
		return contents;
	}

}

Back to the top