Advertisement
Guest User

Untitled

a guest
Sep 30th, 2014
186
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.17 KB | None | 0 0
  1. package com.meetecho.informagiovani.model;
  2.  
  3. import com.google.common.collect.Iterables;
  4. import com.meetecho.informagiovani.exception.FieldValidationException;
  5. import com.meetecho.informagiovani.util.DBHandler;
  6. import org.hibernate.Criteria;
  7. import org.hibernate.Session;
  8. import org.hibernate.criterion.Projections;
  9. import org.hibernate.criterion.Restrictions;
  10. import org.reflections.ReflectionUtils;
  11.  
  12. import javax.persistence.Id;
  13. import java.lang.reflect.Field;
  14. import java.lang.reflect.InvocationTargetException;
  15. import java.lang.reflect.Method;
  16. import java.util.List;
  17. import java.util.Set;
  18.  
  19. /**
  20.  * informagiovani
  21.  * Created by Pasquale Boemio <boemianrapsodi@gmail.com>
  22.  * <p/>
  23.  * 16 September 2014.
  24.  */
  25. public abstract class Model {
  26.  
  27.   public static <Type> Type validate(Class<Type> typeOfField, String value) throws FieldValidationException {
  28.     Type validatedValue = null;
  29.  
  30.     try {
  31.       if(value == null || value.isEmpty())
  32.         throw new FieldValidationException(FieldValidationException.INVALID_VALUE, value);
  33.  
  34.       if(typeOfField.equals(String.class))
  35.         validatedValue = (Type) value;
  36.       else if(Model.class.isAssignableFrom(typeOfField)) {
  37.         Set<Field> fields = ReflectionUtils.getAllFields(typeOfField, ReflectionUtils.withAnnotation(Id.class));
  38.  
  39.         if(fields.size() != 1)
  40.           throw new FieldValidationException(FieldValidationException.ID_FIELD_NOT_FOUND, typeOfField.getName());
  41.  
  42.         Object theId;
  43.  
  44.         if(Iterables.get(fields, 0).getType().getName().equals("java.lang.String"))
  45.           theId = value;
  46.         else
  47.           theId = Long.valueOf(value);
  48.  
  49.         validatedValue = Model.find(typeOfField, theId);
  50.       }
  51.       else {
  52.         Method valueOf = typeOfField.getDeclaredMethod("valueOf", String.class);
  53.         validatedValue = (Type) valueOf.invoke(null, value);
  54.       }
  55.     } catch (NoSuchMethodException e) {
  56.       throw new FieldValidationException(FieldValidationException.VALUEOF_METHOD_MISSING, e);
  57.     } catch (InvocationTargetException e) {
  58.       // called when valueOf method fails
  59.       e.printStackTrace();
  60.       throw new FieldValidationException(FieldValidationException.INVALID_VALUE, e);
  61.     } catch (IllegalAccessException e) {
  62.       e.printStackTrace();
  63.     }
  64.  
  65.     return validatedValue;
  66.   }
  67.  
  68.   public static <Type> Type find(Class<Type> modelClass, Object id) {
  69.     Type theModel = null;
  70.     Session dbSession = DBHandler.getDbSession();
  71.  
  72.     if(id.getClass().getName().equals("java.lang.String"))
  73.       theModel = (Type) dbSession.get(modelClass, (String) id);
  74.     else
  75.       theModel = (Type) dbSession.get(modelClass, (Long) id);
  76.  
  77.     dbSession.close();
  78.  
  79.     return theModel;
  80.   }
  81.  
  82.   public static List where(Class modelClass, String field, Object value, Integer firstResult, Integer maxResult) {
  83.     Session dbSession = DBHandler.getDbSession();
  84.  
  85.     Criteria query = dbSession.createCriteria(modelClass);
  86.     query.add(Restrictions.eq(field, value));
  87.     if(firstResult != null)
  88.       query.setFirstResult(firstResult);
  89.     if(maxResult != null)
  90.       query.setMaxResults(maxResult);
  91.  
  92.     List modelList = query.list();
  93.  
  94.     dbSession.close();
  95.  
  96.     return modelList;
  97.   }
  98.  
  99.   public static <Type> Type first(Class modelClass, String field, Object value) {
  100.     Session dbSession = DBHandler.getDbSession();
  101.  
  102.     Criteria query = dbSession.createCriteria(modelClass);
  103.     query.add(Restrictions.eq(field, value));
  104.     Type model = (Type) query.uniqueResult();
  105.  
  106.     dbSession.close();
  107.  
  108.     return model;
  109.   }
  110.  
  111.   public static Long count(Class modelClass, String fieldRestriction, Object value) {
  112.     Session session = DBHandler.getDbSession();
  113.  
  114.     Criteria query = session.createCriteria(modelClass);
  115.     query.setProjection(Projections.rowCount());
  116.     if(fieldRestriction != null && value != null)
  117.       query.add(Restrictions.eq(fieldRestriction, value));
  118.     Long count = (Long) query.uniqueResult();
  119.  
  120.     session.close();
  121.  
  122.     return count;
  123.   }
  124.  
  125.   public void save() {
  126.     Session dbSession = DBHandler.getDbSession();
  127.  
  128.     dbSession.beginTransaction();
  129.     dbSession.save(this);
  130.     dbSession.getTransaction().commit();
  131.  
  132.     dbSession.close();
  133.   }
  134. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement