/*
* 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.any;
import com.evolveum.midpoint.prism.PrismReferenceValue;
import com.evolveum.midpoint.repo.sql.data.common.RObject;
import com.evolveum.midpoint.repo.sql.data.common.id.ROExtReferenceId;
import com.evolveum.midpoint.repo.sql.data.common.other.RObjectType;
import com.evolveum.midpoint.repo.sql.data.common.type.RObjectExtensionType;
import com.evolveum.midpoint.repo.sql.query2.definition.NotQueryable;
import com.evolveum.midpoint.repo.sql.util.ClassMapper;
import com.evolveum.midpoint.repo.sql.util.RUtil;
import org.hibernate.annotations.ForeignKey;
import org.hibernate.annotations.Index;
import javax.persistence.*;
/**
* @author lazyman
*/
@Entity
@IdClass(ROExtReferenceId.class)
@Table(name = "m_object_ext_reference")
@org.hibernate.annotations.Table(appliesTo = "m_object_ext_reference",
indexes = {@Index(name = "iExtensionReference", columnNames = {"ownerType", "eName", "targetoid"}),
@Index(name = "iExtensionReferenceDef", columnNames = {"owner_oid", "ownerType"})})
public class ROExtReference implements ROExtValue {
public static final String F_TARGET_OID = "value";
public static final String F_RELATION = "relation";
public static final String F_TARGET_TYPE = "targetType";
//owner entity
private RObject owner;
private String ownerOid;
private RObjectExtensionType ownerType;
private boolean dynamic;
private String name;
private String type;
private RValueType valueType;
//this is target oid
private String value;
//this is type attribute
private RObjectType targetType;
private String relation;
public ROExtReference() {
}
@Id
@ForeignKey(name = "fk_object_ext_reference")
@MapsId("owner")
@ManyToOne(fetch = FetchType.LAZY)
@NotQueryable
public RObject getOwner() {
return owner;
}
@Id
@Column(name = "owner_oid", length = RUtil.COLUMN_LENGTH_OID)
public String getOwnerOid() {
if (ownerOid == null && owner != null) {
ownerOid = owner.getOid();
}
return ownerOid;
}
@Id
@Column(name = "ownerType")
@Enumerated(EnumType.ORDINAL)
public RObjectExtensionType getOwnerType() {
return ownerType;
}
@Id
@Column(name = "eName", length = RUtil.COLUMN_LENGTH_QNAME)
public String getName() {
return name;
}
@Column(name = "eType", length = RUtil.COLUMN_LENGTH_QNAME)
public String getType() {
return type;
}
@Enumerated(EnumType.ORDINAL)
public RValueType getValueType() {
return valueType;
}
/**
* @return true if this property has dynamic definition
*/
@Column(name = "dynamicDef")
public boolean isDynamic() {
return dynamic;
}
@Column(name = "targetoid", length = RUtil.COLUMN_LENGTH_OID)
public String getValue() {
return value;
}
@Enumerated(EnumType.ORDINAL)
public RObjectType getTargetType() {
return targetType;
}
@Column(name = "relation", length = RUtil.COLUMN_LENGTH_QNAME)
public String getRelation() {
return relation;
}
public void setValue(String value) {
this.value = value;
}
public void setValueType(RValueType valueType) {
this.valueType = valueType;
}
public void setName(String name) {
this.name = name;
}
public void setType(String type) {
this.type = type;
}
public void setDynamic(boolean dynamic) {
this.dynamic = dynamic;
}
public void setOwner(RObject owner) {
this.owner = owner;
}
public void setOwnerOid(String ownerOid) {
this.ownerOid = ownerOid;
}
public void setOwnerType(RObjectExtensionType ownerType) {
this.ownerType = ownerType;
}
public void setTargetType(RObjectType targetType) {
this.targetType = targetType;
}
public void setRelation(String relation) {
this.relation = relation;
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
ROExtReference that = (ROExtReference) o;
if (dynamic != that.dynamic) return false;
if (name != null ? !name.equals(that.name) : that.name != null) return false;
if (relation != null ? !relation.equals(that.relation) : that.relation != null) return false;
if (targetType != that.targetType) return false;
if (type != null ? !type.equals(that.type) : that.type != null) return false;
if (value != null ? !value.equals(that.value) : that.value != null) return false;
if (valueType != that.valueType) return false;
return true;
}
@Override
public int hashCode() {
int result = (dynamic ? 1 : 0);
result = 31 * result + (name != null ? name.hashCode() : 0);
result = 31 * result + (type != null ? type.hashCode() : 0);
result = 31 * result + (valueType != null ? valueType.hashCode() : 0);
result = 31 * result + (value != null ? value.hashCode() : 0);
result = 31 * result + (targetType != null ? targetType.hashCode() : 0);
result = 31 * result + (relation != null ? relation.hashCode() : 0);
return result;
}
public static PrismReferenceValue createReference(ROExtReference repo) {
PrismReferenceValue value = new PrismReferenceValue();
value.setOid(repo.getValue());
value.setRelation(RUtil.stringToQName(repo.getRelation()));
value.setTargetType(ClassMapper.getQNameForHQLType(repo.getTargetType()));
return value;
}
public static ROExtReference createReference(PrismReferenceValue jaxb) {
ROExtReference repo = new ROExtReference();
repo.setValue(jaxb.getOid());
repo.setRelation(RUtil.qnameToString(jaxb.getRelation()));
repo.setTargetType(ClassMapper.getHQLTypeForQName(jaxb.getTargetType()));
return repo;
}
}