1 package com.vladmihalcea.hibernate.type.array;
2
3 import com.vladmihalcea.hibernate.type.array.internal.AbstractArrayType;
4 import com.vladmihalcea.hibernate.type.array.internal.AbstractArrayTypeDescriptor;
5 import com.vladmihalcea.hibernate.type.array.internal.EnumArrayTypeDescriptor;
6 import com.vladmihalcea.hibernate.type.util.Configuration;
7 import com.vladmihalcea.hibernate.type.util.ParameterizedParameterType;
8 import org.hibernate.annotations.Type;
9 import org.hibernate.usertype.DynamicParameterizedType;
10
11 import java.lang.annotation.Annotation;
12 import java.util.Properties;
13
14 /**
15  * Maps an {@code Enum[]} array on a database ARRAY type. Multidimensional arrays are supported as well, as explained in <a href="https://vladmihalcea.com/multidimensional-array-jpa-hibernate/">this article</a>.
16  * <p>
17  * The {@code SQL_ARRAY_TYPE} parameter is used to define the enum type name in the database.
18  * <p>
19  * For more details about how to use it, check out <a href="https://vladmihalcea.com/map-postgresql-enum-array-jpa-entity-property-hibernate/">this article</a> on <a href="https://vladmihalcea.com/">vladmihalcea.com</a>.
20  *
21  * @author Nazir El-Kayssi
22  * @author Vlad Mihalcea
23  */

24 public class EnumArrayType extends AbstractArrayType<Enum[]> {
25
26     public static final EnumArrayType INSTANCE = new EnumArrayType();
27     private static final String DEFAULT_TYPE_NAME = "%s_enum_array_type";
28
29     private String name;
30
31     public EnumArrayType() {
32         super(
33             new EnumArrayTypeDescriptor()
34         );
35     }
36
37     public EnumArrayType(Configuration configuration) {
38         super(
39             new EnumArrayTypeDescriptor(),
40             configuration
41         );
42     }
43
44     public EnumArrayType(Class arrayClass, String sqlArrayType) {
45         this();
46         Properties parameters = new Properties();
47         parameters.setProperty(SQL_ARRAY_TYPE, sqlArrayType);
48         parameters.put(DynamicParameterizedType.PARAMETER_TYPE, new ParameterizedParameterType(arrayClass));
49         setParameterValues(parameters);
50     }
51
52     public String getName() {
53         return name;
54     }
55
56     @Override
57     public void setParameterValues(Properties parameters) {
58         DynamicParameterizedType.ParameterType parameterType = (ParameterType) parameters.get(DynamicParameterizedType.PARAMETER_TYPE);
59         Annotation[] annotations = parameterType.getAnnotationsMethod();
60         if (annotations != null) {
61             for (int i = 0; i < annotations.length; i++) {
62                 Annotation annotation = annotations[i];
63                 if (Type.class.isAssignableFrom(annotation.annotationType())) {
64                     Type type = (Type) annotation;
65                     name = type.type();
66                     break;
67                 }
68             }
69         }
70         if (name == null) {
71             name = String.format(DEFAULT_TYPE_NAME, parameters.getProperty(SQL_ARRAY_TYPE));
72         }
73         ((AbstractArrayTypeDescriptor) getJavaTypeDescriptor()).setParameterValues(parameters);
74     }
75
76 }