/******************************************************************************* * Copyright (c) 2012 OpenLegacy Inc. * 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: * OpenLegacy Inc. - initial API and implementation *******************************************************************************/ package org.openlegacy.loaders.support; import org.openlegacy.EntitiesRegistry; import org.openlegacy.annotations.screen.ScreenBooleanField; import org.openlegacy.definitions.support.SimpleBooleanFieldTypeDefinition; import org.openlegacy.exceptions.RegistryException; import org.openlegacy.terminal.definitions.ScreenEntityDefinition; import org.openlegacy.terminal.definitions.ScreenPartEntityDefinition; import org.openlegacy.terminal.definitions.SimpleScreenFieldDefinition; import org.openlegacy.terminal.services.ScreenEntitiesRegistry; import org.springframework.stereotype.Component; import org.springframework.util.Assert; import java.lang.annotation.Annotation; import java.lang.reflect.Field; import java.text.MessageFormat; @Component public class ScreenBooleanFieldAnnotationLoader extends AbstractFieldAnnotationLoader { public boolean match(Annotation annotation) { return annotation.annotationType() == ScreenBooleanField.class; } @SuppressWarnings({ "rawtypes" }) public void load(EntitiesRegistry entitiesRegistry, Field field, Annotation annotation, Class<?> containingClass) { ScreenEntitiesRegistry screenEntitiesRegistry = (ScreenEntitiesRegistry)entitiesRegistry; ScreenBooleanField fieldAnnotation = (ScreenBooleanField)annotation; ScreenEntityDefinition screenEntityDefinition = screenEntitiesRegistry.get(containingClass); String fieldName = field.getName(); // look in screen entities if (screenEntityDefinition != null) { SimpleScreenFieldDefinition fieldDefinition = (SimpleScreenFieldDefinition)screenEntityDefinition.getFieldsDefinitions().get( fieldName); fillTypeDefinition(fieldAnnotation, fieldDefinition, fieldName); } else { // look in screen entities parts ScreenPartEntityDefinition screenPart = screenEntitiesRegistry.getPart(containingClass); if (screenPart != null) { fieldName = MessageFormat.format("{0}.{1}", screenPart.getPartName(), fieldName); SimpleScreenFieldDefinition fieldDefinition = (SimpleScreenFieldDefinition)screenPart.getFieldsDefinitions().get( fieldName); fillTypeDefinition(fieldAnnotation, fieldDefinition, fieldName); } } } private static void fillTypeDefinition(ScreenBooleanField fieldAnnotation, SimpleScreenFieldDefinition fieldDefinition, String fieldName) { Assert.notNull(fieldDefinition, MessageFormat.format( "Field definition for field {0} not found. Verify @ScreenBooleanField is defined along @ScreenField annotation", fieldName)); if (fieldDefinition.getJavaType() != Boolean.class) { throw (new RegistryException(MessageFormat.format( "Field {0} marked with @ScreenBooleanField must be of type java.lang.Boolean", fieldName))); } fieldDefinition.setFieldTypeDefinition(new SimpleBooleanFieldTypeDefinition(fieldAnnotation.trueValue(), fieldAnnotation.falseValue(), fieldAnnotation.treatEmptyAsNull())); } }