Clover icon

sunshower-core

  1. Project Clover database Fri Apr 6 2018 03:27:42 UTC
  2. Package io.sunshower.service.model

File PropertyAwareObject.java

 

Coverage histogram

../../../../img/srcFileCovDistChart5.png
36% of files have more coverage

Code metrics

20
35
14
1
126
107
25
0.71
2.5
14
1.79

Classes

Class Line # Actions
PropertyAwareObject 17 35 25
0.4782608747.8%
 

Contributing tests

This file is covered by 12 tests. .

Source view

1    package io.sunshower.service.model;
2   
3    import io.sunshower.common.rs.ClassAdapter;
4    import io.sunshower.common.rs.TypeAttributeClassExtractor;
5    import java.util.*;
6    import javax.persistence.*;
7    import javax.xml.bind.annotation.XmlAttribute;
8    import javax.xml.bind.annotation.XmlElement;
9    import javax.xml.bind.annotation.XmlElementWrapper;
10    import javax.xml.bind.annotation.XmlRootElement;
11    import javax.xml.bind.annotation.adapters.XmlJavaTypeAdapter;
12    import org.eclipse.persistence.oxm.annotations.XmlClassExtractor;
13   
14    @MappedSuperclass
15    @XmlRootElement(name = "property-aware")
16    @XmlClassExtractor(TypeAttributeClassExtractor.class)
 
17    public class PropertyAwareObject<T extends PropertyAwareObject<T>> extends BaseModelObject {
18   
19    @XmlAttribute(name = "type")
20    @XmlJavaTypeAdapter(ClassAdapter.class)
21    private Class<?> type;
22   
23    @OneToMany(fetch = FetchType.LAZY, cascade = CascadeType.ALL)
24    @JoinTable(
25    schema = "SUNSHOWER",
26    name = "ENTITY_TO_PROPERTIES",
27    joinColumns = {@JoinColumn(name = "entity_id", referencedColumnName = "id")},
28    inverseJoinColumns = @JoinColumn(name = "property_id", referencedColumnName = "id")
29    )
30    @XmlElement(name = "property")
31    @XmlElementWrapper(name = "properties")
32    @MapKeyJoinColumn(name = "properties_key")
33    private Map<String, Property> properties;
34   
 
35  3 toggle protected PropertyAwareObject() {}
36   
 
37  56 toggle protected PropertyAwareObject(Class<T> type) {
38  56 this.type = type;
39    }
40   
 
41  22 toggle public void addProperty(Property property) {
42  22 if (property == null) {
43  0 return;
44    }
45  22 if (properties == null) {
46  20 properties = new LinkedHashMap<>();
47    }
48  22 properties.put(property.getKey(), property);
49    }
50   
 
51  0 toggle public void setProperties(List<Property> properties) {
52  0 if (properties == null) {
53  0 clearProperties();
54    }
55  0 for (Property property : properties) {
56  0 addProperty(property);
57    }
58    }
59   
 
60  0 toggle public void removeProperty(Property property) {
61  0 if (property == null) {
62  0 return;
63    }
64  0 if (properties == null) {
65  0 return;
66    }
67  0 properties.remove(property.getKey());
68    }
69   
 
70  3 toggle public Property getProperty(String name) {
71  3 if (properties != null) {
72  3 return properties.get(name);
73    }
74  0 return null;
75    }
76   
 
77  1 toggle public <T> void setRole(Class<T> role) {
78  1 if (role == null) {
79  0 if (properties != null) {
80  0 properties.remove("role");
81    }
82    } else {
83  1 addProperty(new Property(Property.Type.Class, "role", "role", role.getName()));
84    }
85    }
86   
 
87  1 toggle public <T> void setProperty(Class<T> property, String value) {
88  1 addProperty(new Property(Property.Type.Class, property.getName(), "", value));
89    }
90   
 
91  2 toggle public List<Property> getProperties() {
92  2 return properties == null ? new ArrayList<>() : new ArrayList<>(properties.values());
93    }
94   
 
95  0 toggle @SuppressWarnings("unchecked")
96    public Class<T> getType() {
97  0 return (Class<T>) type;
98    }
99   
 
100  0 toggle public void setType(Class<T> type) {
101  0 this.type = type;
102    }
103   
 
104  0 toggle public void clearProperties() {
105  0 properties = new LinkedHashMap<>();
106    }
107   
 
108  1 toggle public <T> Property getProperty(Class<T> property) {
109  1 return getProperty(property.getName());
110    }
111   
 
112  1 toggle @SuppressWarnings("unchecked")
113    public <T> Class<T> getRole() {
114  1 final Property role = getProperty("role");
115  1 if (role == null) {
116  0 return null;
117    }
118  1 final String r = role.getValue();
119   
120  1 try {
121  1 return (Class<T>) Class.forName(r, false, Thread.currentThread().getContextClassLoader());
122    } catch (ClassNotFoundException e) {
123  0 return null;
124    }
125    }
126    }