/*
 * Copyright 2009-2012 the original author or authors.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package info.joseluismartin.gui.bind;

import info.joseluismartin.annotations.Reference;
import info.joseluismartin.gui.list.ListComboBoxModel;
import info.joseluismartin.service.PersistentService;
import info.joseluismartin.util.BeanUtils;

import java.io.Serializable;
import java.lang.annotation.Annotation;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import javax.persistence.ManyToOne;
import javax.swing.JComboBox;

import org.apache.commons.lang.ArrayUtils;
import org.apache.commons.lang.StringUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.util.ReflectionUtils;


/**
 * Initialize control by JPA Annotations.
 * 
 * @author Jose Luis Martin - (jlm@joseluismartin.info)
 */
@SuppressWarnings({"rawtypes", "unchecked"})
public class AnnotationControlInitializer implements ControlInitializer {
	
	private static final Log log = LogFactory.getLog(AnnotationControlInitializer.class);
	private PersistentService<Object, ?extends Serializable> persistentService;
	private boolean initializeEntities = false;
	private boolean firstNull = false;
	
	/**
	 * {@inheritDoc}
	 */
	public void initialize(Object control, String property, Class<?> clazz) {
		if (persistentService == null) {
			log.warn("Nothing to do without persistent service");
			return;
		}
		Class<?> propertyType = BeanUtils.getPropertyDescriptor(clazz, property).getPropertyType();
		Annotation[] annotations = getAnnotations(property, clazz);
		for (Annotation a : annotations) {
			if (ManyToOne.class.equals(a.annotationType()) && control instanceof JComboBox) {
				// fill combo from persistent service
				List<Object> entities = getEntityList(propertyType);
				((JComboBox) control).setModel(new ListComboBoxModel(entities));
			}
			if (Reference.class.equals(a.annotationType()) && control instanceof JComboBox) {
				Reference r = (Reference) a;
				Class type = void.class.equals(r.target()) ? propertyType : r.target();
				List entities = getEntityList(type);
				List values = StringUtils.isEmpty(r.property()) ?  entities : 
					getValueList(entities, r.property());
				
				((JComboBox) control).setModel(new ListComboBoxModel(values));
			}
		}
	}

	private List<Object> getEntityList(Class<?> propertyType) {
		List entities =  persistentService.getAll(propertyType);
		if (isInitializeEntities()) {
			for (Object entity : entities)
				persistentService.initialize(entity);
		}
		if (isFirstNull())
			entities.add(0, null);
		
		return entities;
	}

	/**
	 * @param entities
	 * @return
	 */
	private List getValueList(List entities, String propertyName) {
		List values = new ArrayList(entities.size());
		Iterator iter = entities.iterator();
	
		while (iter.hasNext()) {
			Object value = iter.next();
			if  (value == null)
				values.add(null);
			else {
				values.add(BeanUtils.getProperty(value, propertyName));
			}
		}
		
		return values;
	}

	/**
	 * Get field or method annotations
	 * @param property
	 * @param clazz
	 * @return
	 */
	private Annotation[] getAnnotations(String property, Class<?> clazz) {
		Field field = ReflectionUtils.findField(clazz, property);
		Annotation[] fa = new Annotation[] {};
		if (field != null) {
			fa = field.getAnnotations();
		}
		Method method = BeanUtils.getPropertyDescriptor(clazz, property).getReadMethod();
		Annotation[] ma = method.getAnnotations();
		Annotation[] annotations = (Annotation[]) ArrayUtils.addAll(fa, ma);
		return annotations;
	}
	
	
	/**
	 * @return the persistentService
	 */
	public PersistentService<Object, ? extends Serializable> getPersistentService() {
		return persistentService;
	}
	
	/**
	 * @param persistentService the persistentService to set
	 */
	public void setPersistentService(PersistentService<Object, ? extends Serializable> persistentService) {
		this.persistentService = persistentService;
	}

	/**
	 * @return the initializeEntities
	 */
	public boolean isInitializeEntities() {
		return initializeEntities;
	}

	/**
	 * @param initializeEntities the initializeEntities to set
	 */
	public void setInitializeEntities(boolean initializeEntities) {
		this.initializeEntities = initializeEntities;
	}

	/**
	 * @return the firstNull
	 */
	public boolean isFirstNull() {
		return firstNull;
	}

	/**
	 * @param firstNull the firstNull to set
	 */
	public void setFirstNull(boolean firstNull) {
		this.firstNull = firstNull;
	}

}

@@ -758,16 +758,67 @@
 xModel;%0A
+import info.joseluismartin.gui.list.ListListModel;%0A
 import i
@@ -1185,16 +1185,42 @@
 omboBox;
+%0Aimport javax.swing.JList;
 %0A%0Aimport
@@ -2370,85 +2370,11 @@
 e())
- && control instanceof JComboBox) %7B%0A%09%09%09%09// fill combo from persistent service
+) %7B
 %0A%09%09%09
@@ -2423,24 +2423,65 @@
 pertyType);%0A
+%09%09%09%09if (control instanceof JComboBox) %7B%0A%09
 %09%09%09%09((JCombo
@@ -2540,17 +2540,137 @@
 s));%0A%09%09%09
-%7D
+%09%7D%0A%09%09%09%09else if (control instanceof JList) %7B%0A%09%09%09%09%09((JList) control).setModel(new ListListModel(entities));%0A%09%09%09%09%7D%0A%09%09%09%7D%0A%09%09%09%09
 %0A%09%09%09if (

Initialize JLists