-
Notifications
You must be signed in to change notification settings - Fork 188
/
RResource.java
178 lines (152 loc) · 6.59 KB
/
RResource.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
/*
* Copyright (c) 2010-2013 Evolveum
*
* 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 com.evolveum.midpoint.repo.sql.data.common;
import com.evolveum.midpoint.prism.PrismContext;
import com.evolveum.midpoint.repo.sql.data.common.embedded.REmbeddedReference;
import com.evolveum.midpoint.repo.sql.data.common.embedded.ROperationalState;
import com.evolveum.midpoint.repo.sql.data.common.embedded.RPolyString;
import com.evolveum.midpoint.repo.sql.data.common.enums.RResourceAdministrativeState;
import com.evolveum.midpoint.repo.sql.data.common.other.RReferenceOwner;
import com.evolveum.midpoint.repo.sql.util.DtoTranslationException;
import com.evolveum.midpoint.repo.sql.util.IdGeneratorResult;
import com.evolveum.midpoint.repo.sql.util.RUtil;
import com.evolveum.midpoint.schema.GetOperationOptions;
import com.evolveum.midpoint.schema.SelectorOptions;
import com.evolveum.midpoint.util.logging.Trace;
import com.evolveum.midpoint.util.logging.TraceManager;
import com.evolveum.midpoint.xml.ns._public.common.common_3.ResourceBusinessConfigurationType;
import com.evolveum.midpoint.xml.ns._public.common.common_3.ResourceType;
import org.hibernate.annotations.Cascade;
import org.hibernate.annotations.ForeignKey;
import org.hibernate.annotations.Where;
import javax.persistence.*;
import java.util.Collection;
import java.util.HashSet;
import java.util.Set;
/**
* @author lazyman
*/
@Entity
@ForeignKey(name = "fk_resource")
@Table(uniqueConstraints = @UniqueConstraint(name = "uc_resource_name", columnNames = {"name_norm"}))
public class RResource extends RObject<ResourceType> {
private static final Trace LOGGER = TraceManager.getTrace(RResource.class);
private RPolyString name;
private REmbeddedReference connectorRef;
private ROperationalState operationalState;
//resource business configuration, embedded component can't be used, because then it couldn't use
//non embedded approverRef relationship
private RResourceAdministrativeState administrativeState;
private Set<RObjectReference> approverRef;
//end of resource business configuration
@Enumerated(EnumType.ORDINAL)
@Column(nullable = true)
public RResourceAdministrativeState getAdministrativeState() {
return administrativeState;
}
@Where(clause = RObjectReference.REFERENCE_TYPE + "= 2")
@OneToMany(mappedBy = "owner", orphanRemoval = true)
@ForeignKey(name = "none")
@Cascade({org.hibernate.annotations.CascadeType.ALL})
public Set<RObjectReference> getApproverRef() {
if (approverRef == null) {
approverRef = new HashSet<RObjectReference>();
}
return approverRef;
}
@Embedded
public REmbeddedReference getConnectorRef() {
return connectorRef;
}
@Embedded
public ROperationalState getOperationalState() {
return operationalState;
}
@Embedded
public RPolyString getName() {
return name;
}
public void setAdministrativeState(RResourceAdministrativeState administrativeState) {
this.administrativeState = administrativeState;
}
public void setApproverRef(Set<RObjectReference> approverRef) {
this.approverRef = approverRef;
}
public void setName(RPolyString name) {
this.name = name;
}
public void setOperationalState(ROperationalState operationalState) {
this.operationalState = operationalState;
}
public void setConnectorRef(REmbeddedReference connectorRef) {
this.connectorRef = connectorRef;
}
@Override
public boolean equals(Object o) {
if (this == o)
return true;
if (o == null || getClass() != o.getClass())
return false;
if (!super.equals(o))
return false;
RResource rResource = (RResource) o;
if (name != null ? !name.equals(rResource.name) : rResource.name != null)
return false;
if (connectorRef != null ? !connectorRef.equals(rResource.connectorRef) : rResource.connectorRef != null)
return false;
return true;
}
@Override
public int hashCode() {
int result = super.hashCode();
result = 31 * result + (name != null ? name.hashCode() : 0);
return result;
}
public static void copyFromJAXB(ResourceType jaxb, RResource repo, PrismContext prismContext,
IdGeneratorResult generatorResult)
throws DtoTranslationException {
RObject.copyFromJAXB(jaxb, repo, prismContext, generatorResult);
repo.setName(RPolyString.copyFromJAXB(jaxb.getName()));
repo.setConnectorRef(RUtil.jaxbRefToEmbeddedRepoRef(jaxb.getConnectorRef(), prismContext));
if (jaxb.getConnector() != null) {
LOGGER.warn("Connector from resource type won't be saved. It should be translated to connector reference.");
}
try {
if (jaxb.getBusiness() != null) {
ResourceBusinessConfigurationType business = jaxb.getBusiness();
repo.getApproverRef().addAll(RUtil.safeListReferenceToSet(business.getApproverRef(),
prismContext, repo, RReferenceOwner.RESOURCE_BUSINESS_CONFIGURATON_APPROVER));
repo.setAdministrativeState(RUtil.getRepoEnumValue(business.getAdministrativeState(),
RResourceAdministrativeState.class));
}
if (jaxb.getOperationalState() != null) {
ROperationalState repoOpState = new ROperationalState();
ROperationalState.copyFromJAXB(jaxb.getOperationalState(), repoOpState, prismContext);
repo.setOperationalState(repoOpState);
}
} catch (Exception ex) {
throw new DtoTranslationException(ex.getMessage(), ex);
}
}
@Override
public ResourceType toJAXB(PrismContext prismContext, Collection<SelectorOptions<GetOperationOptions>> options)
throws DtoTranslationException {
ResourceType object = new ResourceType();
RUtil.revive(object, prismContext);
RResource.copyToJAXB(this, object, prismContext, options);
return object;
}
}