Skip to main content
summaryrefslogtreecommitdiffstats
blob: 2fafc27894ee109720d9531223f9184d640bc8e7 (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
/**
 * Copyright (c) 2011, 2015 - Lunifera GmbH (Gross Enzersdorf, Austria), Loetz GmbH&Co.KG (69115 Heidelberg, Germany)
 * 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:
 *         Florian Pirchner - Initial implementation
 */
package org.eclipse.osbp.ecview.dsl.validation

import com.google.inject.Inject
import java.util.List
import org.eclipse.osbp.ecview.core.common.IAccessible
import org.eclipse.osbp.ecview.dsl.extensions.BeanHelper
import org.eclipse.osbp.ecview.dsl.extensions.BindingInfoHelper
import org.eclipse.osbp.ecview.dsl.extensions.TypeHelper
import org.eclipse.osbp.ecview.dsl.lib.IECViewLibMarker
import org.eclipse.osbp.ecview.semantic.uimodel.UiBeanReferenceField
import org.eclipse.osbp.ecview.semantic.uimodel.UiBeanSlot
import org.eclipse.osbp.ecview.semantic.uimodel.UiBinding
import org.eclipse.osbp.ecview.semantic.uimodel.UiBindingEndpointAlias
import org.eclipse.osbp.ecview.semantic.uimodel.UiBindingEndpointAssignment
import org.eclipse.osbp.ecview.semantic.uimodel.UiComboBox
import org.eclipse.osbp.ecview.semantic.uimodel.UiList
import org.eclipse.osbp.ecview.semantic.uimodel.UiModelPackage
import org.eclipse.osbp.ecview.semantic.uimodel.UiOptionsGroup
import org.eclipse.osbp.ecview.semantic.uimodel.UiSearchField
import org.eclipse.osbp.ecview.semantic.uimodel.UiView
import org.eclipse.osbp.xtext.builder.types.loader.api.ITypeLoader
import org.eclipse.osbp.xtext.builder.types.loader.api.ITypeLoaderFactory
import org.eclipse.xtext.common.types.JvmEnumerationType
import org.eclipse.xtext.common.types.JvmGenericType
import org.eclipse.xtext.common.types.JvmType
import org.eclipse.xtext.common.types.util.TypeReferences
import org.eclipse.xtext.naming.IQualifiedNameProvider
import org.eclipse.xtext.validation.Check

//import org.eclipse.xtext.validation.Check
/**
 * Custom validation rules. 
 * 
 * see http://www.eclipse.org/Xtext/documentation.html#validation
 */
class UIGrammarValidator extends AbstractUIGrammarValidator {

	public static final String CODE__MISSING__JAVAX_PERSISTENCE = "0_107";
	public static final String CODE__MISSING__L_RUNTIME_COMMON = "0_108";
	public static final String CODE__MISSING__DATATYPE_LIB = "0_109";
	public static final String CODE__MISSING__DTO_LIB = "0_110";
	public static final String CODE__MISSING__XBASE_LIB = "0_111";
	public static final String CODE__MISSING__JDK_1_5 = "0_112";
	public static final String CODE__MISSING__L_ECVIEW_COMMON = "0_113";
	public static final String CODE__MISSING__L_ECVIEW_DSL_LIB = "0_114";

	@Inject
	ITypeLoaderFactory typeLoaderFactory;
	ITypeLoader typeLoader

	@Inject BindingInfoHelper bindingInfoHelper;

	@Inject extension TypeHelper
	@Inject extension IQualifiedNameProvider

	// public static val INVALID_NAME = 'invalidName'
	//
	// @Check
	// def checkGreetingStartsWithCapital(Greeting greeting) {
	// if (!Character.isUpperCase(greeting.name.charAt(0))) {
	// warning('Name should start with a capital', 
	// MyDslPackage.Literals.GREETING__NAME,
	// INVALID_NAME)
	// }
	// }
	@Check
	def checkSearchField(UiSearchField field) {
		val JvmType type = field.property.typeofLastSegment
		if (type.number) {
			// everything is fine
		} else if (type.string) {
			// everything is fine
		} else if (type.boolean) {
			// everything is fine
		} else {
			error(
				"Searchfields for type " + type.fullyQualifiedName.toString + " are not supported in this version",
				field,
				UiModelPackage.Literals.UI_SEARCH_FIELD__PROPERTY
			)
		}
	}

	@Check
	def checkCaptionProperty(UiOptionsGroup field) {
		var String result = null
		if (field.itemCaptionProperty !== null) {
			return
		}

		if (field.jvmType?.type instanceof JvmEnumerationType) {
			return
		}

		val typeLoader = typeLoaderFactory.createTypeLoader(field.eResource.resourceSet)
		val type = typeLoader.findTypeByName(field.jvmType?.qualifiedName)
		if (type !== null) {
			result = BeanHelper.findCaptionProperty(type)
		}
		typeLoader.dispose

		if (result === null) {
			warning("Caption property could not be calculated by Type.", field,
				UiModelPackage.Literals.UI_OPTIONS_GROUP__ITEM_CAPTION_PROPERTY)
		}
	}

	@Check
	def checkCaptionProperty(UiList field) {
		var String result = null
		if (field.itemCaptionProperty !== null) {
			return
		}

		if (field.jvmType?.type instanceof JvmEnumerationType) {
			return
		}

		val typeLoader = typeLoaderFactory.createTypeLoader(field.eResource.resourceSet)
		val type = typeLoader.findTypeByName(field.jvmType?.qualifiedName)
		if (type !== null) {
			result = BeanHelper.findCaptionProperty(type)
		}
		typeLoader.dispose

		if (result === null) {
			warning("Caption property could not be calculated by Type.", field,
				UiModelPackage.Literals.UI_LIST__ITEM_CAPTION_PROPERTY)
		}
	}

	@Check
	def checkCaptionProperty(UiComboBox field) {
		var String result = null
		if (field.itemCaptionProperty !== null) {
			return
		}

		if (field.jvmType?.type instanceof JvmEnumerationType) {
			return
		}

		val typeLoader = typeLoaderFactory.createTypeLoader(field.eResource.resourceSet)
		val type = typeLoader.findTypeByName(field.jvmType?.qualifiedName)
		if (type !== null) {
			result = BeanHelper.findCaptionProperty(type)
		}
		typeLoader.dispose

		if (result === null) {
			warning("Caption property could not be calculated by Type.", field,
				UiModelPackage.Literals.UI_COMBO_BOX__ITEM_CAPTION_PROPERTY)
		}
	}

	@Check
	def checkBeanSlot(UiBeanSlot beanSlot) {
		val type = beanSlot.jvmType.type
		if (type === null) {
			return
		}

		if (type instanceof JvmEnumerationType) {
			warning("You are using an Enum for the bean slot type.", beanSlot,
				UiModelPackage.Literals.UI_BEAN_SLOT__JVM_TYPE)
		}

	}

	@Check
	def checkCaptionProperty(UiBeanReferenceField field) {
		var String result = null
		if (field.captionProperty !== null) {
			return
		}

		val typeLoader = typeLoaderFactory.createTypeLoader(field.eResource.resourceSet)
		val type = typeLoader.findTypeByName(field.jvmType?.qualifiedName)

		if (type !== null) {
			result = BeanHelper.findCaptionProperty(type)
		}
		typeLoader.dispose

		if (result === null) {
			warning("Caption property could not be calculated by Type.", field,
				UiModelPackage.Literals.UI_BEAN_REFERENCE_FIELD__CAPTION_PROPERTY)
		}
	}

	@Check
	def void checkClassPath(UiView model) {
		val TypeReferences typeReferences = getServices().getTypeReferences();
		val JvmGenericType listType = typeReferences.findDeclaredType(typeof(List), model) as JvmGenericType;
		if (listType === null || listType.getTypeParameters().isEmpty()) {
			error("Couldn't find a JDK 1.5 or higher on the project's classpath.", model,
				UiModelPackage.Literals.UI_NAMED_ELEMENT__NAME, CODE__MISSING__JDK_1_5);
		}
		if (typeReferences.findDeclaredType("org.eclipse.osbp.runtime.common.annotations.Dispose", model) === null) {
			error("Couldn't find the mandatory library 'org.eclipse.osbp.runtime.common' on the project's classpath.",
				model, UiModelPackage.Literals.UI_NAMED_ELEMENT__NAME, CODE__MISSING__L_RUNTIME_COMMON);
		}
		if (typeReferences.findDeclaredType(typeof(Extension), model) === null) {
			error(
				"Couldn't find the mandatory library 'org.eclipse.xtext.xbase.lib' 2.11.0 or higher on the project's classpath.",
				model, UiModelPackage.Literals.UI_NAMED_ELEMENT__NAME, CODE__MISSING__XBASE_LIB);
		}
		if (typeReferences.findDeclaredType(typeof(IAccessible), model) === null) {
			error(
				"Couldn't find the mandatory library 'org.eclipse.osbp.ecview.core.common' on the project's classpath.",
				model, UiModelPackage.Literals.UI_NAMED_ELEMENT__NAME, CODE__MISSING__L_ECVIEW_COMMON);
		}
		if (typeReferences.findDeclaredType("org.eclipse.osbp.dsl.dto.lib.services.impl.AbstractDTOService", model) ===
			null) {
			warning(
				"Couldn't find the mandatory library 'org.eclipse.osbp.dsl.dto.lib' on the project's classpath. This may cause resolving problems.",
				model, UiModelPackage.Literals.UI_NAMED_ELEMENT__NAME, CODE__MISSING__DTO_LIB);
		}
		if (typeReferences.findDeclaredType(typeof(IECViewLibMarker), model) === null) {
			warning(
				"Couldn't find the mandatory library 'org.eclipse.osbp.ecview.dsl.lib' on the project's classpath. This may cause resolving problems.",
				model, UiModelPackage.Literals.UI_NAMED_ELEMENT__NAME, CODE__MISSING__L_ECVIEW_DSL_LIB);
		}

		// if (typeReferences.findDeclaredType("java.persistence.Persistence", model) === null) {
		// warning(
		// "Couldn't find the optional library 'javax.persistence' 2.1.0 or higher on the project's classpath. If you are using JPA-Dto-Services, the library is mandatory.",
		// model, UiModelPackage.Literals.UI_NAMED_ELEMENT__NAME, CODE__MISSING__JAVAX_PERSISTENCE);
		// }
		if (typeReferences.findDeclaredType("org.eclipse.osbp.dsl.common.datatypes.IDatatypeConstants", model) ===
			null) {
			warning(
				"Couldn't find the optional library 'org.eclipse.osbp.dsl.datatype.lib' on the project's classpath. This may cause resolving problems.",
				model, UiModelPackage.Literals.UI_NAMED_ELEMENT__NAME, CODE__MISSING__DATATYPE_LIB);
		}
	}

	@Check
	def void checkDatasources(UiView model) {
		val set = newHashSet()
		model.beanSlots.forEach [
			if (set.contains(it.name)) {
				error("Duplicate datasource name.", model, UiModelPackage.Literals.UI_CONTEXT__BEAN_SLOTS,
					model.beanSlots.indexOf(it))
			} else {
				set += it.name
			}
		]
	}

	@Check
	def void checkBinding(UiBinding object) {
		val UiBindingEndpointAlias sourceAlias = object.sourceAlias as UiBindingEndpointAlias
		val source = object.source
		val UiBindingEndpointAlias targetAlias = object.targetAlias as UiBindingEndpointAlias
		val target = object.target

		var UiBindingEndpointAssignment sourceEndpoint = source as UiBindingEndpointAssignment
		if (sourceAlias !== null) {
			sourceEndpoint = sourceAlias.endpoint as UiBindingEndpointAssignment
		}
		var UiBindingEndpointAssignment targetEndpoint = target as UiBindingEndpointAssignment
		if (targetAlias !== null) {
			targetEndpoint = targetAlias.endpoint as UiBindingEndpointAssignment
		}

		val BindingInfoHelper.BindingInfo sourceInfo = new BindingInfoHelper.BindingInfo
		bindingInfoHelper.collectBindingInfo(source, sourceInfo);

		val BindingInfoHelper.BindingInfo targetInfo = new BindingInfoHelper.BindingInfo
		bindingInfoHelper.collectBindingInfo(target, targetInfo);

		if (object.isListBinding) {
			if (sourceInfo.isNested && sourceInfo.isCollection) {
				error("No list binding here since the collection is nested!", object,
					UiModelPackage.Literals.UI_BINDING__SOURCE)
			}
			if (targetInfo.isNested && targetInfo.isCollection) {
				error("No list binding here since the collection is nested!", object,
					UiModelPackage.Literals.UI_BINDING__TARGET)
			}
		} else {
			if (!sourceInfo.isNested && !targetInfo.isNested && sourceInfo.isCollection && targetInfo.isCollection) {
				error("Use list binding here. You are binding two NON-nested collections!", object,
					UiModelPackage.Literals.UI_BINDING__SOURCE)
			}
		}
	}
}

Back to the top