001 package com.khubla.pragmatach.plugin.mongodb.util; 002 003 import java.lang.reflect.Field; 004 005 import javax.persistence.CascadeType; 006 import javax.persistence.FetchType; 007 import javax.persistence.GeneratedValue; 008 import javax.persistence.Id; 009 import javax.persistence.ManyToOne; 010 import javax.persistence.OneToMany; 011 012 import org.apache.commons.beanutils.BeanUtils; 013 014 import com.khubla.pragmatach.framework.api.PragmatachException; 015 016 /** 017 * @author tom 018 */ 019 public class ClassTypeUtils { 020 /** 021 * check if its cascade delete field 022 */ 023 public static boolean isCascadeDelete(Field field) { 024 /* 025 * get the column annotation, if it exists 026 */ 027 final OneToMany OneToManyAnnotation = field.getAnnotation(OneToMany.class); 028 if (null != OneToManyAnnotation) { 029 final CascadeType[] cascadetypes = OneToManyAnnotation.cascade(); 030 for (final CascadeType cascadeType : cascadetypes) { 031 if ((cascadeType == CascadeType.REMOVE) || (cascadeType == CascadeType.ALL)) { 032 return true; 033 } 034 } 035 } 036 final ManyToOne ManyToOneAnnotation = field.getAnnotation(ManyToOne.class); 037 if (null != ManyToOneAnnotation) { 038 final CascadeType[] cascadetypes = ManyToOneAnnotation.cascade(); 039 for (final CascadeType cascadeType : cascadetypes) { 040 if ((cascadeType == CascadeType.REMOVE) || (cascadeType == CascadeType.ALL)) { 041 return true; 042 } 043 } 044 } 045 return false; 046 } 047 048 /** 049 * check if its cascade saved field 050 */ 051 public static boolean isCascadeSave(Field field) { 052 /* 053 * get the column annotation, if it exists 054 */ 055 final OneToMany OneToManyAnnotation = field.getAnnotation(OneToMany.class); 056 if (null != OneToManyAnnotation) { 057 final CascadeType[] cascadetypes = OneToManyAnnotation.cascade(); 058 for (final CascadeType cascadeType : cascadetypes) { 059 if ((cascadeType == CascadeType.PERSIST) || (cascadeType == CascadeType.ALL)) { 060 return true; 061 } 062 } 063 } 064 final ManyToOne ManyToOneAnnotation = field.getAnnotation(ManyToOne.class); 065 if (null != ManyToOneAnnotation) { 066 final CascadeType[] cascadetypes = ManyToOneAnnotation.cascade(); 067 for (final CascadeType cascadeType : cascadetypes) { 068 if ((cascadeType == CascadeType.PERSIST) || (cascadeType == CascadeType.ALL)) { 069 return true; 070 } 071 } 072 } 073 return false; 074 } 075 076 /** 077 * check if its an eager loaded field 078 */ 079 public static boolean isEagerLoad(Field field) { 080 /* 081 * get the column annotation, if it exists 082 */ 083 final OneToMany OneToManyAnnotation = field.getAnnotation(OneToMany.class); 084 if (null != OneToManyAnnotation) { 085 return FetchType.EAGER == OneToManyAnnotation.fetch(); 086 } 087 final ManyToOne ManyToOneAnnotation = field.getAnnotation(ManyToOne.class); 088 if (null != ManyToOneAnnotation) { 089 if (FetchType.EAGER == ManyToOneAnnotation.fetch()) { 090 return true; 091 } 092 } 093 return false; 094 } 095 096 /** 097 * check if its an eager loaded field 098 */ 099 public static boolean isLazyLoad(Field field) { 100 /* 101 * get the column annotation, if it exists 102 */ 103 final OneToMany OneToManyAnnotation = field.getAnnotation(OneToMany.class); 104 if (null != OneToManyAnnotation) { 105 if (FetchType.LAZY == OneToManyAnnotation.fetch()) { 106 return true; 107 } 108 } 109 final ManyToOne ManyToOneAnnotation = field.getAnnotation(ManyToOne.class); 110 if (null != ManyToOneAnnotation) { 111 if (FetchType.LAZY == ManyToOneAnnotation.fetch()) { 112 return true; 113 } 114 } 115 return false; 116 } 117 118 /** 119 * the type 120 */ 121 private final Class<?> typeClazz; 122 123 public ClassTypeUtils(Class<?> typeClazz) { 124 this.typeClazz = typeClazz; 125 } 126 127 /** 128 * get id 129 */ 130 public String getId(Object t) throws PragmatachException { 131 try { 132 final String idField = getIdFieldName(); 133 return BeanUtils.getProperty(t, idField); 134 } catch (final Exception e) { 135 throw new PragmatachException("Exception in getId", e); 136 } 137 } 138 139 /** 140 * get the id field 141 */ 142 public String getIdFieldName() { 143 for (final Field field : typeClazz.getDeclaredFields()) { 144 if (null != field.getAnnotation(Id.class)) { 145 return field.getName(); 146 } 147 } 148 return null; 149 } 150 151 /** 152 * Entity has an @GeneratedValue? 153 */ 154 public boolean isGeneratedId() throws PragmatachException { 155 try { 156 final Field field = typeClazz.getDeclaredField(getIdFieldName()); 157 if (null != field.getAnnotation(GeneratedValue.class)) { 158 return true; 159 } 160 return false; 161 } catch (final Exception e) { 162 throw new PragmatachException("Exception in isGeneratedId", e); 163 } 164 } 165 166 /** 167 * set id 168 */ 169 public void setId(Object t, String i) throws PragmatachException { 170 try { 171 final String idField = getIdFieldName(); 172 BeanUtils.setProperty(t, idField, i); 173 } catch (final Exception e) { 174 throw new PragmatachException("Exception in setId", e); 175 } 176 } 177 }