// Path: src/main/java/cz/sigler/android/aavalidation/api/error/ValidationProcessingException.java
// public class ValidationProcessingException extends RuntimeException {
// 
// 	public ValidationProcessingException(String message, Throwable cause) {
// 		super(message, cause);
// 	}
// 
// }
// 
// Path: src/main/java/cz/sigler/android/aavalidation/api/model/IModelSupport.java
// public interface IModelSupport <T extends Model> extends IValidatingModel {
// 
// 	/** @return model id. */
// 	Long getId();
// 
// 	/**
// 	 * Gets field value from underlying model.
// 	 *
// 	 * @param name
// 	 * @return field value
// 	 * @throws IllegalArgumentException
// 	 *					in case of a invalid field name
// 	 */
// 	Object getFieldValue(String name);
// 
// 	/**
// 	 * Sets field value on the underlying model.
// 	 *
// 	 * @param name
// 	 *					field name
// 	 * @param value
// 	 *					value to set
// 	 * @throws IllegalArgumentException if field name is invalid
// 	 */
// 	void setFieldValue(String name, Object value);
// 
// 	/**
// 	 * Delegates the save call to the underlying model.
// 	 */
// 	void save();
// 
// 	/**
// 	 * Delegates the delete save call to the underlying model.
// 	 */
// 	void delete();
// 
// 	/**
// 	 * Delegates the loadFromCursor call to the underlying model.
// 	 */
// 	void loadFromCursor(Cursor cursor);
// 
// 	/**
// 	 * @return underlying model
// 	 */
// 	T getModel();
// }
// 
// Path: src/main/java/cz/sigler/android/aavalidation/internal/ModelSupportImpl.java
// public class ModelSupportImpl<T extends Model> implements IModelSupport<T> {
// 
// 	/** Wrapped model. */
// 	private T model;
// 
// 	/** List of validation errors. */
// 	private List<ValidationError> errors = new LinkedList<ValidationError>();
// 
// 	/**
// 	 * Constructor.
// 	 *
// 	 * @param model
// 	 *					model to wrap
// 	 */
// 	public ModelSupportImpl(final T model) {
// 		this.model = model;
// 	}
// 
// 	/**
// 	 * {@inheritDoc }
// 	 */
// 	@Override
// 	public boolean isValid() {
// 		ValidationExecutor executor = new ValidationExecutor(model);
// 
// 		errors = executor.execute();
// 		return errors.isEmpty();
// 	}
// 
// 	/**
// 	 * {@inheritDoc }
// 	 */
// 	@Override
// 	public void saveOrThrow() throws ValidationException {
// 		if (isValid()) {
// 			model.save();
// 		} else {
// 			throw new ValidationException(getErrors());
// 		}
// 	}
// 
// 	/**
// 	 * {@inheritDoc }
// 	 */
// 	@Override
// 	public List<ValidationError> getErrors() {
// 		return new LinkedList<ValidationError>(errors);
// 	}
// 
// 	@Override
// 	public Long getId() {
// 		return getModel().getId();
// 	}
// 
// 	@Override
// 	public Object getFieldValue(final String name) {
// 		return FieldUtils.getFieldValue(getModel(), name);
// 	}
// 
// 	@Override
// 	public void setFieldValue(final String name, final Object value) {
// 		FieldUtils.setFieldValue(getModel(), name, value);
// 	}
// 
// 	@Override
// 	public void save() {
// 		getModel().save();
// 	}
// 
// 	@Override
// 	public void delete() {
// 		getModel().delete();
// 	}
// 
// 	@Override
// 	public void loadFromCursor(Cursor cursor) {
// 		getModel().loadFromCursor(cursor);
// 	}
// 
// 	@Override
// 	public T getModel() {
// 		return model;
// 	}
// 	
// }


import com.activeandroid.Model;
import cz.sigler.android.aavalidation.api.error.ValidationProcessingException;
import cz.sigler.android.aavalidation.api.model.IModelSupport;
import cz.sigler.android.aavalidation.internal.ModelSupportImpl;
/*
 * Copyright (C) 2013 Vojtech Sigler.
 *
 * 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 cz.sigler.android.aavalidation;


/**
 * Convenience factory to ease the use of this library in projects where
 * the models have been subclassed or the user does not want to use ValidatingModel
 * base class.
 */
public class ModelSupportFactory {

	/**
	 * Creates a new instance of the model and wraps the validating layer around it.
	 *
	 * @param <T>
	 *					subclass of Model
	 * @param modelClass
	 *					model class
	 * @return wrapped model instance
	 */
	public static <T extends Model> IModelSupport<T> createModel(final Class<T> modelClass) {
		try {
			T model = modelClass.newInstance();

			return wrapModel(model);
		} catch (Exception e) {
			throw new ValidationProcessingException("Failed creating new model instance.", e);
		}
	}

	/**
	 * Wraps an existing model with a validating layer. Null safe.
	 *
	 * @param <T>
	 *					subclass of Model
	 * @param model
	 *					model instance
	 * @return wrapped model
	 */
// Path: src/main/java/cz/sigler/android/aavalidation/api/error/ValidationProcessingException.java
// public class ValidationProcessingException extends RuntimeException {
// 
// 	public ValidationProcessingException(String message, Throwable cause) {
// 		super(message, cause);
// 	}
// 
// }
// 
// Path: src/main/java/cz/sigler/android/aavalidation/api/model/IModelSupport.java
// public interface IModelSupport <T extends Model> extends IValidatingModel {
// 
// 	/** @return model id. */
// 	Long getId();
// 
// 	/**
// 	 * Gets field value from underlying model.
// 	 *
// 	 * @param name
// 	 * @return field value
// 	 * @throws IllegalArgumentException
// 	 *					in case of a invalid field name
// 	 */
// 	Object getFieldValue(String name);
// 
// 	/**
// 	 * Sets field value on the underlying model.
// 	 *
// 	 * @param name
// 	 *					field name
// 	 * @param value
// 	 *					value to set
// 	 * @throws IllegalArgumentException if field name is invalid
// 	 */
// 	void setFieldValue(String name, Object value);
// 
// 	/**
// 	 * Delegates the save call to the underlying model.
// 	 */
// 	void save();
// 
// 	/**
// 	 * Delegates the delete save call to the underlying model.
// 	 */
// 	void delete();
// 
// 	/**
// 	 * Delegates the loadFromCursor call to the underlying model.
// 	 */
// 	void loadFromCursor(Cursor cursor);
// 
// 	/**
// 	 * @return underlying model
// 	 */
// 	T getModel();
// }
// 
// Path: src/main/java/cz/sigler/android/aavalidation/internal/ModelSupportImpl.java
// public class ModelSupportImpl<T extends Model> implements IModelSupport<T> {
// 
// 	/** Wrapped model. */
// 	private T model;
// 
// 	/** List of validation errors. */
// 	private List<ValidationError> errors = new LinkedList<ValidationError>();
// 
// 	/**
// 	 * Constructor.
// 	 *
// 	 * @param model
// 	 *					model to wrap
// 	 */
// 	public ModelSupportImpl(final T model) {
// 		this.model = model;
// 	}
// 
// 	/**
// 	 * {@inheritDoc }
// 	 */
// 	@Override
// 	public boolean isValid() {
// 		ValidationExecutor executor = new ValidationExecutor(model);
// 
// 		errors = executor.execute();
// 		return errors.isEmpty();
// 	}
// 
// 	/**
// 	 * {@inheritDoc }
// 	 */
// 	@Override
// 	public void saveOrThrow() throws ValidationException {
// 		if (isValid()) {
// 			model.save();
// 		} else {
// 			throw new ValidationException(getErrors());
// 		}
// 	}
// 
// 	/**
// 	 * {@inheritDoc }
// 	 */
// 	@Override
// 	public List<ValidationError> getErrors() {
// 		return new LinkedList<ValidationError>(errors);
// 	}
// 
// 	@Override
// 	public Long getId() {
// 		return getModel().getId();
// 	}
// 
// 	@Override
// 	public Object getFieldValue(final String name) {
// 		return FieldUtils.getFieldValue(getModel(), name);
// 	}
// 
// 	@Override
// 	public void setFieldValue(final String name, final Object value) {
// 		FieldUtils.setFieldValue(getModel(), name, value);
// 	}
// 
// 	@Override
// 	public void save() {
// 		getModel().save();
// 	}
// 
// 	@Override
// 	public void delete() {
// 		getModel().delete();
// 	}
// 
// 	@Override
// 	public void loadFromCursor(Cursor cursor) {
// 		getModel().loadFromCursor(cursor);
// 	}
// 
// 	@Override
// 	public T getModel() {
// 		return model;
// 	}
// 	
// }


// Path: src/main/java/cz/sigler/android/aavalidation/ModelSupportFactory.java
import com.activeandroid.Model;
import cz.sigler.android.aavalidation.api.error.ValidationProcessingException;
import cz.sigler.android.aavalidation.api.model.IModelSupport;
import cz.sigler.android.aavalidation.internal.ModelSupportImpl;

/*
 * Copyright (C) 2013 Vojtech Sigler.
 *
 * 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 cz.sigler.android.aavalidation;


/**
 * Convenience factory to ease the use of this library in projects where
 * the models have been subclassed or the user does not want to use ValidatingModel
 * base class.
 */
public class ModelSupportFactory {

	/**
	 * Creates a new instance of the model and wraps the validating layer around it.
	 *
	 * @param <T>
	 *					subclass of Model
	 * @param modelClass
	 *					model class
	 * @return wrapped model instance
	 */
	public static <T extends Model> IModelSupport<T> createModel(final Class<T> modelClass) {
		try {
			T model = modelClass.newInstance();

			return wrapModel(model);
		} catch (Exception e) {
			throw new ValidationProcessingException("Failed creating new model instance.", e);
		}
	}

	/**
	 * Wraps an existing model with a validating layer. Null safe.
	 *
	 * @param <T>
	 *					subclass of Model
	 * @param model
	 *					model instance
	 * @return wrapped model
	 */
	public static <T extends Model> IModelSupport<T> wrapModel(final T model) {