[monasca-common] Hibernate support added

- refactored code to match java guidelines
- added missed indexes
- fixed invalid string lengths according to latest mon.sql from ansible-monasca-schema
- introduced relations
- using ordinal for enum fields

Change-Id: Ic84bf542c10ff714885c7a53bb9cbe3a2fb728cd
This commit is contained in:
Tomasz Trębski 2015-08-06 09:15:25 +02:00
parent fd5c41d720
commit 6c56dc799e
28 changed files with 2817 additions and 0 deletions

1
.gitignore vendored
View File

@ -9,3 +9,4 @@ test-output/
test-config.yml
*.swp
*.iml
bin/

View File

@ -0,0 +1,46 @@
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>monasca-common</groupId>
<artifactId>monasca-common</artifactId>
<version>${computedVersion}</version>
</parent>
<artifactId>monasca-common-hibernate</artifactId>
<packaging>jar</packaging>
<properties>
<hibernate-core.version>4.3.10.Final</hibernate-core.version>
<usertype.core.version>3.2.0.GA</usertype.core.version>
<joda-time.version>2.8.1</joda-time.version>
</properties>
<dependencies>
<dependency>
<groupId>monasca-common</groupId>
<artifactId>monasca-common-model</artifactId>
<version>${project.version}</version>
<exclusions>
<exclusion>
<groupId>joda-time</groupId>
<artifactId>joda-time</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-core</artifactId>
<version>${hibernate-core.version}</version>
</dependency>
<dependency>
<groupId>org.jadira.usertype</groupId>
<artifactId>usertype.core</artifactId>
<version>${usertype.core.version}</version>
</dependency>
<dependency>
<groupId>joda-time</groupId>
<artifactId>joda-time</artifactId>
<version>${joda-time.version}</version>
</dependency>
</dependencies>
</project>

View File

@ -0,0 +1,114 @@
/*
* Copyright 2015 FUJITSU LIMITED
*
* 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 monasca.common.hibernate.configuration;
import javax.validation.constraints.NotNull;
import com.fasterxml.jackson.annotation.JsonProperty;
import org.hibernate.validator.constraints.NotEmpty;
public class HibernateDbConfiguration {
private static final String DEFAULT_HBM2DDL_AUTO_VALUE = "validate";
@NotNull
@JsonProperty
boolean supportEnabled;
@NotEmpty
@JsonProperty
String providerClass;
@NotEmpty
@JsonProperty
String dataSourceClassName;
@NotEmpty
@JsonProperty
String user;
@NotEmpty
@JsonProperty
String password;
@JsonProperty
String initialConnections;
@JsonProperty
String maxConnections;
@JsonProperty
String autoConfig;
@JsonProperty
String dataSourceUrl;
@JsonProperty
String serverName;
@JsonProperty
String portNumber;
@JsonProperty
String databaseName;
public String getDataSourceUrl() {
return this.dataSourceUrl;
}
public boolean getSupportEnabled() {
return supportEnabled;
}
public String getProviderClass() {
return providerClass;
}
public void setProviderClass(String providerClass) {
this.providerClass = providerClass;
}
public String getDataSourceClassName() {
return dataSourceClassName;
}
public String getServerName() {
return serverName;
}
public String getPortNumber() {
return portNumber;
}
public String getDatabaseName() {
return databaseName;
}
public String getUser() {
return user;
}
public String getPassword() {
return password;
}
public String getInitialConnections() {
return initialConnections;
}
public String getMaxConnections() {
return maxConnections;
}
/**
* Returns {@code hbm2ddl.auto} telling hibernate how to handle schema. By default will return {@code validate}.
* For more information how each of the possible value works refer to official Hibernate documentation.
*
* @return {@link String} hbm2ddl.auto value
* @see <a href="https://docs.jboss.org/hibernate/orm/4.1/manual/en-US/html/ch03.html#configuration-optional">3.7. Miscellaneous Properties</a>
*/
public String getAutoConfig() {
return this.autoConfig == null ? DEFAULT_HBM2DDL_AUTO_VALUE : this.autoConfig;
}
}

View File

@ -0,0 +1,55 @@
/*
* Copyright 2015 FUJITSU LIMITED
*
* 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 monasca.common.hibernate.core;
import java.io.Serializable;
import org.joda.time.DateTime;
public interface AuditablePersistable<T extends Serializable>
extends Persistable<T> {
/**
* Returns {@link DateTime} when an entity was created
*
* @return creation time
*/
DateTime getCreatedAt();
/**
* Allows to set {@code createdAt} {@link DateTime}
*
* @param createdAt date instance
*
* @return {@code self}
*/
AuditablePersistable setCreatedAt(DateTime createdAt);
/**
* Returns {@link DateTime} when an entity was updated
*
* @return most recent update time
*/
DateTime getUpdatedAt();
/**
* Allows to set {@code updatedAt} {@link DateTime}
*
* @param updatedAt date instance
*
* @return {@code self}
*/
AuditablePersistable setUpdatedAt(DateTime updatedAt);
}

View File

@ -0,0 +1,50 @@
/*
* Copyright 2015 FUJITSU LIMITED
*
* 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 monasca.common.hibernate.core;
import java.io.Serializable;
/**
* Persistable class.
*
* Defines most basic interface <b>Hibernate</b> entities should implement.
*/
public interface Persistable<T extends Serializable>
extends Serializable {
/**
* Returns {@code is} of an entity
*
* @return ID of an entity
*/
T getId();
/**
* Allows to set {@code id}
*
* @param id primary key
*
* @return {@code self}
*/
Persistable setId(T id);
/**
* Evaluates if an entity is new or not.
* Returns {@link Boolean#FALSE} if entity was persisted at least once
*
* @return true if new, false otherwise
*/
boolean isNew();
}

View File

@ -0,0 +1,132 @@
/*
* Copyright 2015 FUJITSU LIMITED
*
* 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 monasca.common.hibernate.db;
import java.io.Serializable;
import javax.persistence.Column;
import javax.persistence.MappedSuperclass;
import javax.persistence.Version;
import com.google.common.base.Objects;
import org.hibernate.annotations.DynamicInsert;
import org.hibernate.annotations.DynamicUpdate;
import org.hibernate.annotations.Parameter;
import org.hibernate.annotations.Type;
import org.joda.time.DateTime;
import monasca.common.hibernate.core.AuditablePersistable;
@DynamicInsert
@DynamicUpdate
@MappedSuperclass
abstract class AbstractAuditablePersistable<T extends Serializable>
extends AbstractPersistable<T>
implements AuditablePersistable<T> {
static final String DATE_TIME_TYPE = "org.jadira.usertype.dateandtime.joda.PersistentDateTime";
static final String DB_ZONE = "UTC";
static final String JAVA_ZONE = "jvm";
private static final long serialVersionUID = 2335373173379564615L;
@Column(name = "created_at", nullable = false)
@Type(
type = DATE_TIME_TYPE,
parameters = {
@Parameter(name = "databaseZone", value = DB_ZONE),
@Parameter(name = "javaZone", value = JAVA_ZONE)
}
)
private DateTime createdAt;
@Version
@Column(name = "updated_at", nullable = false)
@Type(
type = DATE_TIME_TYPE,
parameters = {
@Parameter(name = "databaseZone", value = DB_ZONE),
@Parameter(name = "javaZone", value = JAVA_ZONE)
}
)
private DateTime updatedAt;
AbstractAuditablePersistable() {
this(null, null, null);
}
AbstractAuditablePersistable(final T id) {
this(id, null, null);
}
AbstractAuditablePersistable(final T id,
final DateTime createdAt,
final DateTime updatedAt) {
super(id);
this.setDates(createdAt, updatedAt);
}
@Override
public DateTime getCreatedAt() {
return createdAt;
}
@Override
public AuditablePersistable<T> setCreatedAt(DateTime createdAt) {
this.createdAt = createdAt;
return this;
}
@Override
public DateTime getUpdatedAt() {
return updatedAt;
}
@Override
public AuditablePersistable<T> setUpdatedAt(DateTime updatedAt) {
this.updatedAt = updatedAt;
return this;
}
/**
* Ensures that both {@link #createdAt} and {@link #updatedAt} will be
* set to the earliest possible value in case passssed values are {@code NULL}
*
* @param createdAt created date
* @param updatedAt updated date
*/
private void setDates(final DateTime createdAt,
final DateTime updatedAt) {
if (createdAt == null && updatedAt == null) {
this.updatedAt = DateTime.now();
this.createdAt = DateTime.now();
} else if (createdAt == null) {
this.createdAt = DateTime.now();
} else if (updatedAt == null) {
this.updatedAt = DateTime.now();
} else {
this.createdAt = createdAt;
this.updatedAt = updatedAt;
}
}
@Override
public String toString() {
return Objects.toStringHelper(this)
.add("id", id)
.add("createdAt", createdAt)
.add("updatedAt", updatedAt)
.toString();
}
}

View File

@ -0,0 +1,87 @@
/*
* Copyright 2015 FUJITSU LIMITED
*
* 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 monasca.common.hibernate.db;
import java.io.Serializable;
import javax.persistence.Column;
import javax.persistence.Id;
import javax.persistence.MappedSuperclass;
import com.google.common.base.Objects;
import org.hibernate.annotations.DynamicInsert;
import org.hibernate.annotations.DynamicUpdate;
import monasca.common.hibernate.core.Persistable;
@DynamicInsert
@DynamicUpdate
@MappedSuperclass
abstract class AbstractPersistable<T extends Serializable>
implements Persistable<T> {
private static final long serialVersionUID = -4841075518435739989L;
@Id
@Column(name = "id", length = 36)
protected T id;
AbstractPersistable() {
super();
}
AbstractPersistable(final T id) {
this();
this.id = id;
}
@Override
public T getId() {
return id;
}
@Override
public Persistable<T> setId(final T id) {
this.id = id;
return this;
}
@Override
public boolean isNew() {
return this.id == null;
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
AbstractPersistable that = (AbstractPersistable) o;
return Objects.equal(this.id, that.id);
}
@Override
public int hashCode() {
return Objects.hashCode(id);
}
@Override
public String toString() {
return Objects.toStringHelper(this)
.add("id", id)
.toString();
}
}

View File

@ -0,0 +1,97 @@
/*
* Copyright 2015 FUJITSU LIMITED
*
* 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 monasca.common.hibernate.db;
import javax.persistence.Column;
import javax.persistence.Id;
import javax.persistence.MappedSuperclass;
import com.google.common.base.Objects;
import org.hibernate.annotations.DynamicInsert;
import org.hibernate.annotations.DynamicUpdate;
import org.hibernate.annotations.Type;
import org.hibernate.annotations.TypeDef;
import monasca.common.hibernate.core.Persistable;
import monasca.common.hibernate.type.BinaryId;
import monasca.common.hibernate.type.BinaryIdType;
@DynamicInsert
@DynamicUpdate
@MappedSuperclass
@TypeDef(
name = "monasca.common.hibernate.type.BinaryId",
typeClass = BinaryIdType.class
)
abstract class AbstractUUIDPersistable
implements Persistable<BinaryId> {
private static final long serialVersionUID = 6192850092568538880L;
@Id
@Type(type = "monasca.common.hibernate.type.BinaryId")
@Column(name = "id", length = 20, updatable = false, nullable = false)
protected BinaryId id;
AbstractUUIDPersistable() {
super();
this.id = null;
}
AbstractUUIDPersistable(final BinaryId id) {
this.id = id;
}
protected AbstractUUIDPersistable(final byte[] id) {
this(new BinaryId(id));
}
@Override
public BinaryId getId() {
return this.id;
}
@Override
public Persistable<BinaryId> setId(final BinaryId id) {
this.id = id;
return this;
}
@Override
public boolean isNew() {
return this.id == null || this.id.getBytes() == null;
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
AbstractUUIDPersistable that = (AbstractUUIDPersistable) o;
return Objects.equal(this.id, that.id);
}
@Override
public int hashCode() {
return Objects.hashCode(id);
}
@Override
public String toString() {
return Objects.toStringHelper(this)
.add("id", id)
.toString();
}
}

View File

@ -0,0 +1,128 @@
/*
* Copyright 2015 FUJITSU LIMITED
*
* 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 monasca.common.hibernate.db;
import java.io.Serializable;
import javax.persistence.EmbeddedId;
import javax.persistence.Entity;
import javax.persistence.NamedQueries;
import javax.persistence.NamedQuery;
import javax.persistence.Table;
import com.google.common.base.Objects;
import monasca.common.model.alarm.AlarmState;
@Entity
@Table(name = "alarm_action")
@NamedQueries({
@NamedQuery(
name = AlarmActionDb.Queries.DELETE_BY_ALARMDEFINITION_ID,
query = "delete AlarmActionDb aa " +
"where aa.alarmActionId.alarmDefinition.id = :id"
),
@NamedQuery(
name = AlarmActionDb.Queries.DELETE_BY_ALARMDEFINITION_ID_AND_ALARMSTATE,
query = "delete AlarmActionDb aa " +
"where aa.alarmActionId.alarmDefinition.id = :id " +
"and aa.alarmActionId.alarmState = :alarmState"
),
@NamedQuery(
name = AlarmActionDb.Queries.FIND_BY_TENANT_ID_AND_ALARMDEFINITION_ID_DISTINCT,
query = "select distinct aa from AlarmActionDb aa, AlarmDefinitionDb ad " +
"where ad.id=aa.alarmActionId.alarmDefinition.id " +
"and ad.deletedAt is null " +
"and ad.tenantId= :tenantId " +
"and ad.id= :alarmDefId"
)
})
public class AlarmActionDb
implements Serializable {
private static final long serialVersionUID = -8138171887172601911L;
@EmbeddedId
private AlarmActionId alarmActionId;
public AlarmActionDb() {
this(null, AlarmState.UNDETERMINED, null);
}
public AlarmActionDb(final AlarmDefinitionDb alarmDefinition,
final AlarmState alarmState,
final String actionId) {
super();
this.alarmActionId = new AlarmActionId(alarmDefinition, alarmState, actionId);
}
public AlarmActionId getAlarmActionId() {
return alarmActionId;
}
public AlarmActionDb setAlarmActionId(AlarmActionId alarmActionId) {
this.alarmActionId = alarmActionId;
return this;
}
public boolean isInAlarmState(final AlarmState state) {
return this.alarmActionId != null && this.alarmActionId.getAlarmState().equals(state);
}
public AlarmActionDb setAlarmState(final AlarmState alarmState) {
this.requireAlarmActionId().setAlarmState(alarmState);
return this;
}
public AlarmActionDb setActionId(final String actionId) {
this.requireAlarmActionId().setActionId(actionId);
return this;
}
public AlarmActionDb setAlarmDefinition(final AlarmDefinitionDb alarmDefinition) {
this.requireAlarmActionId().setAlarmDefinition(alarmDefinition);
return this;
}
public AlarmState getAlarmState() {
return this.requireAlarmActionId().getAlarmState();
}
public AlarmDefinitionDb getAlarmDefinition() {
return this.requireAlarmActionId().getAlarmDefinition();
}
public String getActionId() {
return this.requireAlarmActionId().getActionId();
}
private AlarmActionId requireAlarmActionId() {
if (this.alarmActionId == null) {
this.alarmActionId = new AlarmActionId();
}
return this.alarmActionId;
}
@Override
public String toString() {
return Objects.toStringHelper(this)
.add("alarmActionId", alarmActionId)
.toString();
}
public interface Queries {
String DELETE_BY_ALARMDEFINITION_ID = "AlarmAction.deleteByAlarmDefinitionId";
String DELETE_BY_ALARMDEFINITION_ID_AND_ALARMSTATE = "AlarmAction.deleteByAlarmDefinitionIdAndAlarmState";
String FIND_BY_TENANT_ID_AND_ALARMDEFINITION_ID_DISTINCT = "AlarmAction.findByTenantIdAndAlarmDefinitionId.Distinct";
}
}

View File

@ -0,0 +1,101 @@
/*
* Copyright 2015 FUJITSU LIMITED
*
* 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 monasca.common.hibernate.db;
import java.io.Serializable;
import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Embeddable;
import javax.persistence.EnumType;
import javax.persistence.Enumerated;
import javax.persistence.FetchType;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import com.google.common.base.Objects;
import monasca.common.model.alarm.AlarmState;
@Embeddable
public class AlarmActionId
implements Serializable {
private static final long serialVersionUID = 919758576421181247L;
@JoinColumn(name = "alarm_definition_id", nullable = false)
@ManyToOne(cascade = CascadeType.REMOVE, fetch = FetchType.LAZY, optional = false)
private AlarmDefinitionDb alarmDefinition;
@Column(name = "alarm_state", nullable = false)
@Enumerated(EnumType.STRING)
private AlarmState alarmState;
@Column(name = "action_id", length = 36, nullable = false)
private String actionId;
public AlarmActionId() {
super();
}
public AlarmActionId(AlarmDefinitionDb alarmDefinition, AlarmState alarmState, String actionId) {
super();
this.alarmDefinition = alarmDefinition;
this.alarmState = alarmState;
this.actionId = actionId;
}
public AlarmActionId setAlarmDefinition(final AlarmDefinitionDb alarmDefinition) {
this.alarmDefinition = alarmDefinition;
return this;
}
public AlarmActionId setAlarmState(final AlarmState alarmState) {
this.alarmState = alarmState;
return this;
}
public AlarmActionId setActionId(final String actionId) {
this.actionId = actionId;
return this;
}
public AlarmDefinitionDb getAlarmDefinition() {
return this.alarmDefinition;
}
public AlarmState getAlarmState() {
return this.alarmState;
}
public String getActionId() {
return this.actionId;
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
AlarmActionId that = (AlarmActionId) o;
return Objects.equal(this.alarmDefinition, that.alarmDefinition) &&
Objects.equal(this.alarmState, that.alarmState) &&
Objects.equal(this.actionId, that.actionId);
}
@Override
public int hashCode() {
return Objects.hashCode(alarmDefinition, alarmState, actionId);
}
}

View File

@ -0,0 +1,272 @@
/*
* Copyright 2015 FUJITSU LIMITED
*
* 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 monasca.common.hibernate.db;
import java.util.Collection;
import javax.annotation.Nullable;
import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.EnumType;
import javax.persistence.Enumerated;
import javax.persistence.FetchType;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.NamedQueries;
import javax.persistence.NamedQuery;
import javax.persistence.OneToMany;
import javax.persistence.Table;
import com.google.common.base.Function;
import com.google.common.collect.FluentIterable;
import com.google.common.collect.Sets;
import org.hibernate.annotations.OnDelete;
import org.hibernate.annotations.OnDeleteAction;
import org.hibernate.annotations.Parameter;
import org.hibernate.annotations.Type;
import org.joda.time.DateTime;
import monasca.common.model.alarm.AlarmState;
@Entity
@Table(name = "alarm")
@NamedQueries({
@NamedQuery(
name = AlarmDb.Queries.DELETE_BY_ALARMDEFINITION_ID,
query = "delete from AlarmDb a where a.alarmDefinition.id = :alarmDefinitionId"
),
@NamedQuery(
name = AlarmDb.Queries.DELETE_BY_ID,
query = "delete from AlarmDb a where a.id = :id"
),
@NamedQuery(
name = AlarmDb.Queries.FIND_BY_ID,
query = "from AlarmDb a where a.id = :id"
)
})
public class AlarmDb
extends AbstractAuditablePersistable<String> {
private static final long serialVersionUID = -9084263584287898881L;
@JoinColumn(name = "alarm_definition_id", nullable = false)
@ManyToOne(cascade = CascadeType.REMOVE, fetch = FetchType.LAZY, optional = false)
private AlarmDefinitionDb alarmDefinition;
@Column(name = "state")
@Enumerated(EnumType.STRING)
private AlarmState state;
@Column(name = "lifecycle_state", length = 50)
private String lifecycleState;
@Column(name = "link", length = 512)
private String link;
@Column(name = "state_updated_at")
@Type(
type = DATE_TIME_TYPE,
parameters = {
@Parameter(name = "databaseZone", value = DB_ZONE),
@Parameter(name = "javaZone", value = JAVA_ZONE)
}
)
private DateTime stateUpdatedAt;
@OneToMany(mappedBy = "alarmMetricId.alarm", fetch = FetchType.LAZY, cascade = {
CascadeType.PERSIST,
CascadeType.REFRESH,
CascadeType.REMOVE
})
@OnDelete(action = OnDeleteAction.CASCADE)
private Collection<AlarmMetricDb> alarmMetrics;
@OneToMany(mappedBy = "alarm", fetch = FetchType.LAZY, cascade = {
CascadeType.PERSIST,
CascadeType.REFRESH,
CascadeType.REMOVE
})
@OnDelete(action = OnDeleteAction.CASCADE)
private Collection<SubAlarmDb> subAlarms;
public AlarmDb() {
super();
}
public AlarmDb(
String id,
AlarmDefinitionDb alarmDefinition,
AlarmState state,
String lifecycleState,
String link,
DateTime stateUpdatedAt,
DateTime created_at,
DateTime updated_at) {
super(id, created_at, updated_at);
this.setAlarmDefinition(alarmDefinition);
this.link = link;
this.state = state;
this.lifecycleState = lifecycleState;
this.stateUpdatedAt = stateUpdatedAt;
}
public AlarmState getState() {
return state;
}
public AlarmDb setState(AlarmState state) {
this.state = state;
return this;
}
public String getLifecycleState() {
return lifecycleState;
}
public AlarmDb setLifecycleState(String lifecycleState) {
this.lifecycleState = lifecycleState;
return this;
}
public String getLink() {
return link;
}
public AlarmDb setLink(String link) {
this.link = link;
return this;
}
public DateTime getStateUpdatedAt() {
return stateUpdatedAt;
}
public AlarmDb setStateUpdatedAt(DateTime stateUpdatedAt) {
this.stateUpdatedAt = stateUpdatedAt;
return this;
}
public AlarmDefinitionDb getAlarmDefinition() {
return alarmDefinition;
}
public AlarmDb setAlarmDefinition(final AlarmDefinitionDb alarmDefinition) {
if (!alarmDefinition.hasAlarm(this)) {
alarmDefinition.addAlarm(this);
}
this.alarmDefinition = alarmDefinition;
return this;
}
public Collection<AlarmMetricDb> getAlarmMetrics() {
return this.alarmMetrics != null ? this.alarmMetrics : (this.alarmMetrics = Sets.newHashSet());
}
public AlarmDb setAlarmMetrics(final Collection<AlarmMetricDb> alarmMetrics) {
if (alarmMetrics == null || alarmMetrics.isEmpty()) {
return this;
}
final AlarmDb self = this;
this.alarmMetrics = Sets.newHashSetWithExpectedSize(alarmMetrics.size());
FluentIterable.from(alarmMetrics)
.transform(new Function<AlarmMetricDb, AlarmMetricDb>() {
@Nullable
@Override
public AlarmMetricDb apply(@Nullable final AlarmMetricDb input) {
assert input != null;
input.setAlarm(self);
return input;
}
})
.copyInto(this.alarmMetrics);
return this;
}
public AlarmDb addAlarmMetric(final AlarmMetricDb alarmMetric) {
if (alarmMetric == null || this.hasAlarmMetric(alarmMetric)) {
return this;
}
this.getAlarmMetrics().add(alarmMetric);
alarmMetric.setAlarm(this);
return this;
}
public AlarmDb removeAlarmMetric(final AlarmMetricDb alarmDb) {
if (alarmDb == null || this.alarmMetrics == null) {
return this;
}
this.alarmMetrics.remove(alarmDb);
return this;
}
public boolean hasAlarmMetric(final AlarmMetricDb alarm) {
return alarm != null && (this.alarmMetrics != null && this.alarmMetrics.contains(alarm));
}
public Collection<SubAlarmDb> getSubAlarms() {
return this.subAlarms != null ? this.subAlarms : (this.subAlarms = Sets.newHashSet());
}
public AlarmDb setSubAlarms(final Collection<SubAlarmDb> subAlarms) {
if (subAlarms == null || subAlarms.isEmpty()) {
return this;
}
final AlarmDb self = this;
this.subAlarms = Sets.newHashSetWithExpectedSize(subAlarms.size());
FluentIterable.from(subAlarms)
.transform(new Function<SubAlarmDb, SubAlarmDb>() {
@Nullable
@Override
public SubAlarmDb apply(@Nullable final SubAlarmDb input) {
assert input != null;
input.setAlarm(self);
return input;
}
})
.copyInto(this.subAlarms);
return this;
}
public AlarmDb addSubAlarm(final SubAlarmDb subAlarm) {
if (subAlarm == null || this.hasSubAlarm(subAlarm)) {
return this;
}
this.getSubAlarms().add(subAlarm);
subAlarm.setAlarm(this);
return this;
}
public AlarmDb removeSubAlarm(final SubAlarmDb subAlarm) {
if (subAlarm == null || this.subAlarms == null) {
return this;
}
this.subAlarms.remove(subAlarm);
return this;
}
public boolean hasSubAlarm(final SubAlarmDb subAlarm) {
return subAlarm != null && (this.subAlarms != null && this.subAlarms.contains(subAlarm));
}
public interface Queries {
String DELETE_BY_ALARMDEFINITION_ID = "Alarm.deleteByAlarmDefinitionId";
String DELETE_BY_ID = "Alarm.deleteById";
String FIND_BY_ID = "Alarm.findById";
}
}

View File

@ -0,0 +1,273 @@
/*
* Copyright 2015 FUJITSU LIMITED
*
* 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 monasca.common.hibernate.db;
import java.util.Collection;
import java.util.Collections;
import javax.annotation.Nullable;
import javax.persistence.Basic;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.EnumType;
import javax.persistence.Enumerated;
import javax.persistence.FetchType;
import javax.persistence.Index;
import javax.persistence.Lob;
import javax.persistence.NamedQueries;
import javax.persistence.NamedQuery;
import javax.persistence.OneToMany;
import javax.persistence.Table;
import com.google.common.base.Function;
import com.google.common.collect.FluentIterable;
import com.google.common.collect.Lists;
import com.google.common.collect.Sets;
import org.hibernate.annotations.BatchSize;
import org.hibernate.annotations.Parameter;
import org.hibernate.annotations.Type;
import org.joda.time.DateTime;
import monasca.common.model.alarm.AlarmSeverity;
@Entity
@Table(name = "alarm_definition", indexes = {
@Index(name = "tenant_id", columnList = "tenant_id"),
@Index(name = "deleted_at", columnList = "deleted_at")
})
@NamedQueries({
@NamedQuery(
name = AlarmDefinitionDb.Queries.FIND_BY_TENANT_AND_ID_NOT_DELETED,
query = "from AlarmDefinitionDb ad " +
"where ad.tenantId = :tenant_id " +
"and ad.id = :id " +
"and ad.deletedAt is NULL " +
"group by ad.id"
),
@NamedQuery(
name = AlarmDefinitionDb.Queries.FIND_BY_TENANT_ID_AND_ID,
query = "from AlarmDefinitionDb alarm_definition " +
"where alarm_definition.tenantId=:tenantId " +
"and alarm_definition.id= :id"
)
})
public class AlarmDefinitionDb
extends AbstractAuditablePersistable<String> {
private static final long serialVersionUID = 2566210444329934008L;
private static final String DEFAULT_MATCH_BY = "";
private static final String DEFAULT_NAME = "";
private static final boolean DEFAULT_ACTIONS_ENABLED = true;
@Column(name = "tenant_id", length = 36, nullable = false)
private String tenantId;
@Column(name = "name", length = 255, nullable = false)
private String name = DEFAULT_NAME;
@Column(name = "description", length = 255)
private String description;
@Lob
@Type(type = "text")
@Basic(fetch = FetchType.LAZY)
@Column(name = "expression", nullable = false, length = 16777215)
private String expression;
@Column(name = "severity", nullable = false)
@Enumerated(EnumType.STRING)
private AlarmSeverity severity;
@Column(name = "match_by", length = 255)
private String matchBy = DEFAULT_MATCH_BY;
@Column(name = "actions_enabled", length = 1, nullable = false)
private boolean actionsEnabled = DEFAULT_ACTIONS_ENABLED;
@Column(name = "deleted_at")
@Type(
type = DATE_TIME_TYPE,
parameters = {
@Parameter(name = "databaseZone", value = DB_ZONE),
@Parameter(name = "javaZone", value = JAVA_ZONE)
}
)
private DateTime deletedAt;
@BatchSize(size = 50)
@OneToMany(mappedBy = "alarmDefinition", fetch = FetchType.LAZY)
private Collection<AlarmDb> alarms;
public AlarmDefinitionDb() {
super();
}
public AlarmDefinitionDb(String id,
String tenantId,
String name,
String description,
String expression,
AlarmSeverity severity,
String matchBy,
boolean actionsEnabled,
DateTime created_at,
DateTime updated_at,
DateTime deletedAt) {
super(id, created_at, updated_at);
this.id = id;
this.tenantId = tenantId;
this.name = name;
this.description = description;
this.expression = expression;
this.severity = severity;
this.matchBy = matchBy;
this.actionsEnabled = actionsEnabled;
this.deletedAt = deletedAt;
}
public AlarmDefinitionDb(String id,
String tenantId,
String expression,
AlarmSeverity severity,
DateTime created_at,
DateTime updated_at) {
this(id, tenantId, null, null, expression, severity, DEFAULT_MATCH_BY, DEFAULT_ACTIONS_ENABLED, created_at, updated_at, null);
}
public AlarmDefinitionDb setDeletedAt(final DateTime deletedAt) {
this.deletedAt = deletedAt;
return this;
}
public AlarmDefinitionDb setTenantId(final String tenantId) {
this.tenantId = tenantId;
return this;
}
public AlarmDefinitionDb setName(final String name) {
this.name = name;
return this;
}
public AlarmDefinitionDb setDescription(final String description) {
this.description = description;
return this;
}
public AlarmDefinitionDb setExpression(final String expression) {
this.expression = expression;
return this;
}
public AlarmDefinitionDb setSeverity(final AlarmSeverity severity) {
this.severity = severity;
return this;
}
public AlarmDefinitionDb setMatchBy(final String matchBy) {
this.matchBy = matchBy;
return this;
}
public AlarmDefinitionDb setActionsEnabled(final boolean actionsEnabled) {
this.actionsEnabled = actionsEnabled;
return this;
}
public String getTenantId() {
return tenantId;
}
public String getName() {
return name;
}
public String getDescription() {
return description;
}
public String getExpression() {
return expression;
}
public AlarmSeverity getSeverity() {
return severity;
}
public String getMatchBy() {
return matchBy;
}
public Collection<String> getMatchByAsCollection() {
if (this.matchBy == null) {
return Collections.emptyList();
}
return Lists.newArrayList(this.matchBy.split(","));
}
public boolean isActionsEnabled() {
return actionsEnabled;
}
public DateTime getDeletedAt() {
return deletedAt;
}
public boolean hasAlarm(final AlarmDb alarm) {
return alarm != null && (this.alarms != null && this.alarms.contains(alarm));
}
public Collection<AlarmDb> getAlarms() {
return this.alarms != null ? this.alarms : (this.alarms = Sets.newHashSet());
}
public AlarmDefinitionDb setAlarms(final Collection<AlarmDb> alarms) {
final AlarmDefinitionDb self = this;
this.alarms = Sets.newHashSetWithExpectedSize(alarms.size());
FluentIterable.from(alarms)
.transform(new Function<AlarmDb, AlarmDb>() {
@Nullable
@Override
public AlarmDb apply(@Nullable final AlarmDb input) {
assert input != null;
input.setAlarmDefinition(self);
return input;
}
})
.copyInto(this.alarms);
return this;
}
public AlarmDefinitionDb addAlarm(final AlarmDb alarmDb) {
if (alarmDb == null || this.hasAlarm(alarmDb)) {
return this;
}
this.getAlarms().add(alarmDb);
alarmDb.setAlarmDefinition(this);
return this;
}
public AlarmDefinitionDb removeAlarm(final AlarmDb alarmDb) {
if (alarmDb == null || this.alarms == null) {
return this;
}
this.getAlarms().remove(alarmDb);
return this;
}
public interface Queries {
String FIND_BY_TENANT_AND_ID_NOT_DELETED = "AlarmDefinition.byTenantAndIdNotDeleted";
String FIND_BY_TENANT_ID_AND_ID = "AlarmDefinition.byTenantIdAndId";
}
}

View File

@ -0,0 +1,88 @@
/*
* Copyright 2015 FUJITSU LIMITED
*
* 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 monasca.common.hibernate.db;
import java.io.Serializable;
import javax.persistence.EmbeddedId;
import javax.persistence.Entity;
import javax.persistence.Index;
import javax.persistence.Table;
import com.google.common.base.Objects;
@Entity
@Table(name = "alarm_metric", indexes = {
@Index(name = "metric_definition_dimensions_id", columnList = "metric_definition_dimensions_id"),
@Index(name = "alarm_id", columnList = "alarm_id")
})
public class AlarmMetricDb
implements Serializable {
private static final long serialVersionUID = 2852204906043180958L;
@EmbeddedId
private AlarmMetricId alarmMetricId;
public AlarmMetricDb() {
super();
}
public AlarmMetricDb(AlarmMetricId alarmMetricId) {
this();
this.alarmMetricId = alarmMetricId;
this.alarmMetricId.getAlarm().addAlarmMetric(this);
}
public AlarmMetricDb(final AlarmDb alarm, MetricDefinitionDimensionsDb mdd) {
this(new AlarmMetricId(alarm, mdd));
}
public AlarmMetricId getAlarmMetricId() {
return alarmMetricId;
}
public AlarmMetricDb setAlarmMetricId(AlarmMetricId alarmMetricId) {
this.alarmMetricId = alarmMetricId;
return this;
}
public AlarmMetricDb setAlarm(final AlarmDb alarm) {
if (alarm != null) {
if (!alarm.hasAlarmMetric(this)) {
alarm.addAlarmMetric(this);
}
this.requireAlarmMetricId().setAlarm(alarm);
}
return this;
}
public AlarmMetricDb setMetricDefinitionDimensionsId(final MetricDefinitionDimensionsDb mdd) {
this.requireAlarmMetricId().setMetricDefinitionDimensions(mdd);
return this;
}
private AlarmMetricId requireAlarmMetricId() {
if (this.alarmMetricId == null) {
this.alarmMetricId = new AlarmMetricId();
}
return this.alarmMetricId;
}
@Override
public String toString() {
return Objects.toStringHelper(this)
.add("alarmMetricId", alarmMetricId)
.toString();
}
}

View File

@ -0,0 +1,107 @@
/*
* Copyright 2015 FUJITSU LIMITED
*
* 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 monasca.common.hibernate.db;
import java.io.Serializable;
import javax.persistence.CascadeType;
import javax.persistence.Embeddable;
import javax.persistence.FetchType;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import com.google.common.base.Objects;
import org.hibernate.annotations.OnDelete;
import org.hibernate.annotations.OnDeleteAction;
@Embeddable
public class AlarmMetricId
implements Serializable {
private static final long serialVersionUID = -7672930363327018974L;
@JoinColumn(name = "metric_definition_dimensions_id", referencedColumnName = "id", nullable = false)
@OnDelete(action = OnDeleteAction.CASCADE)
@ManyToOne(cascade = {
CascadeType.PERSIST,
CascadeType.REFRESH,
CascadeType.REMOVE
})
private MetricDefinitionDimensionsDb metricDefinitionDimensions;
@JoinColumn(name = "alarm_id", referencedColumnName = "id", nullable = false)
@OnDelete(action = OnDeleteAction.CASCADE)
@ManyToOne(cascade = {
CascadeType.PERSIST,
CascadeType.REFRESH,
CascadeType.REMOVE
}, fetch = FetchType.LAZY, optional = false)
private AlarmDb alarm;
public AlarmMetricId() {
this(null, null);
}
public AlarmMetricId(final AlarmDb alarm,
MetricDefinitionDimensionsDb metricDefinitionDimensionsId) {
super();
this.alarm = alarm;
this.metricDefinitionDimensions = metricDefinitionDimensionsId;
}
public AlarmMetricId(final AlarmDb alarm) {
this(alarm, null);
}
public AlarmDb getAlarm() {
return alarm;
}
public AlarmMetricId setAlarm(final AlarmDb alarm) {
this.alarm = alarm;
return this;
}
public MetricDefinitionDimensionsDb getMetricDefinitionDimensions() {
return metricDefinitionDimensions;
}
public AlarmMetricId setMetricDefinitionDimensions(final MetricDefinitionDimensionsDb metricDefinitionDimensions) {
this.metricDefinitionDimensions = metricDefinitionDimensions;
return this;
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
AlarmMetricId that = (AlarmMetricId) o;
return Objects.equal(this.metricDefinitionDimensions, that.metricDefinitionDimensions) &&
Objects.equal(this.alarm, that.alarm);
}
@Override
public int hashCode() {
return Objects.hashCode(metricDefinitionDimensions, alarm);
}
@Override
public String toString() {
return Objects.toStringHelper(this)
.add("alarm", alarm)
.toString();
}
}

View File

@ -0,0 +1,80 @@
/*
* Copyright 2015 FUJITSU LIMITED
*
* 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 monasca.common.hibernate.db;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Table;
import monasca.common.hibernate.type.BinaryId;
@Entity
@Table(name = "metric_definition")
public class MetricDefinitionDb
extends AbstractUUIDPersistable {
private static final long serialVersionUID = 292896181025585969L;
@Column(name = "name", length = 255, nullable = false)
private String name;
@Column(name = "tenant_id", length = 36, nullable = false)
private String tenantId;
@Column(name = "region", length = 255, nullable = false)
private String region;
public MetricDefinitionDb() {
super();
}
public MetricDefinitionDb(BinaryId id, String name, String tenantId, String region) {
super(id);
this.name = name;
this.tenantId = tenantId;
this.region = region;
}
public MetricDefinitionDb(byte[] id, String name, String tenantId, String region) {
this(new BinaryId(id), name, tenantId, region);
}
public MetricDefinitionDb setRegion(final String region) {
this.region = region;
return this;
}
public MetricDefinitionDb setTenantId(final String tenantId) {
this.tenantId = tenantId;
return this;
}
public MetricDefinitionDb setName(final String name) {
this.name = name;
return this;
}
public String getName() {
return this.name;
}
public String getTenantId() {
return this.tenantId;
}
public String getRegion() {
return this.region;
}
}

View File

@ -0,0 +1,98 @@
/*
* Copyright 2015 FUJITSU LIMITED
*
* 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 monasca.common.hibernate.db;
import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.Index;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.Table;
import org.hibernate.annotations.OnDelete;
import org.hibernate.annotations.OnDeleteAction;
import org.hibernate.annotations.Type;
import monasca.common.hibernate.type.BinaryId;
@Entity
@Table(
name = "metric_definition_dimensions",
indexes = {
@Index(name = "metric_definition_id", columnList = "metric_definition_id"),
@Index(name = "metric_dimension_set_id", columnList = "metric_dimension_set_id")
}
)
public class MetricDefinitionDimensionsDb
extends AbstractUUIDPersistable {
private static final long serialVersionUID = -4902748436802939703L;
@JoinColumn(name = "metric_definition_id", referencedColumnName = "id", nullable = false)
@OnDelete(action = OnDeleteAction.CASCADE)
@ManyToOne(cascade = {
CascadeType.REMOVE,
CascadeType.PERSIST,
CascadeType.REFRESH
}, fetch = FetchType.LAZY, optional = false)
private MetricDefinitionDb metricDefinition;
@Type(type = "monasca.common.hibernate.type.BinaryId")
@Column(name = "metric_dimension_set_id", length = 20, nullable = false)
private BinaryId metricDimensionSetId;
public MetricDefinitionDimensionsDb() {
super();
}
public MetricDefinitionDimensionsDb(final BinaryId id,
final MetricDefinitionDb metricDefinition,
final BinaryId metricDimensionSetId) {
super(id);
this.metricDefinition = metricDefinition;
this.metricDimensionSetId = metricDimensionSetId;
}
public MetricDefinitionDimensionsDb(final byte[] id,
final MetricDefinitionDb metricDefinition,
final BinaryId metricDimensionSetId) {
this(new BinaryId(id), metricDefinition, metricDimensionSetId);
}
public MetricDefinitionDimensionsDb(final byte[] id,
final MetricDefinitionDb metricDefinition,
final byte[] metricDimensionSetId) {
this(new BinaryId(id), metricDefinition, new BinaryId(metricDimensionSetId));
}
public MetricDefinitionDb getMetricDefinition() {
return metricDefinition;
}
public MetricDefinitionDimensionsDb setMetricDefinition(MetricDefinitionDb metricDefinition) {
this.metricDefinition = metricDefinition;
return this;
}
public BinaryId getMetricDimensionSetId() {
return metricDimensionSetId;
}
public MetricDefinitionDimensionsDb setMetricDimensionSetId(final BinaryId metricDimensionSetId) {
this.metricDimensionSetId = metricDimensionSetId;
return this;
}
}

View File

@ -0,0 +1,114 @@
/*
* Copyright 2015 FUJITSU LIMITED
*
* 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 monasca.common.hibernate.db;
import java.io.Serializable;
import javax.persistence.Column;
import javax.persistence.EmbeddedId;
import javax.persistence.Entity;
import javax.persistence.Index;
import javax.persistence.Table;
import javax.persistence.UniqueConstraint;
import com.google.common.base.Objects;
import monasca.common.hibernate.type.BinaryId;
@Entity
@Table(
name = "metric_dimension",
indexes = {
@Index(name = "dimension_set_id", columnList = "dimension_set_id")
},
uniqueConstraints = {
@UniqueConstraint(name = "metric_dimension_key", columnNames = {
"dimension_set_id",
"name"
})
}
)
public class MetricDimensionDb
implements Serializable {
private static final long serialVersionUID = 4261654453776857159L;
@EmbeddedId
private MetricDimensionDbId id;
@Column(name = "value", length = 255, nullable = false)
private String value;
public MetricDimensionDb() {
super();
}
public MetricDimensionDb(final byte[] dimensionSetId, final String name) {
this(dimensionSetId, name, null);
}
public MetricDimensionDb(final byte[] dimensionSetId, final String name, final String value) {
this(new BinaryId(dimensionSetId), name, value);
}
public MetricDimensionDb(final BinaryId dimensionSetId, final String name, final String value) {
this(new MetricDimensionDbId(dimensionSetId, name), value);
}
public MetricDimensionDb(final MetricDimensionDbId id, final String value) {
this.id = id;
this.value = value;
}
public MetricDimensionDb setId(final MetricDimensionDbId id) {
this.id = id;
return this;
}
public MetricDimensionDbId getId() {
return id;
}
public String getValue() {
return value;
}
public MetricDimensionDb setValue(String value) {
this.value = value;
return this;
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
MetricDimensionDb that = (MetricDimensionDb) o;
return Objects.equal(this.id, that.id);
}
@Override
public int hashCode() {
return Objects.hashCode(id);
}
@Override
public String toString() {
return Objects.toStringHelper(this)
.add("id", id)
.add("value", value)
.toString();
}
}

View File

@ -0,0 +1,94 @@
/*
* Copyright 2015 FUJITSU LIMITED
*
* 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 monasca.common.hibernate.db;
import java.io.Serializable;
import javax.persistence.Column;
import javax.persistence.Embeddable;
import com.google.common.base.Objects;
import org.hibernate.annotations.Type;
import org.hibernate.annotations.TypeDef;
import monasca.common.hibernate.type.BinaryId;
import monasca.common.hibernate.type.BinaryIdType;
@Embeddable
@TypeDef(
name = "monasca.common.hibernate.type.BinaryId",
typeClass = BinaryIdType.class
)
public class MetricDimensionDbId
implements Serializable {
private static final long serialVersionUID = -594428923583460707L;
@Type(type = "monasca.common.hibernate.type.BinaryId")
@Column(name = "dimension_set_id", length = 20, nullable = false)
private BinaryId dimensionSetId;
@Column(name = "name", length = 255, nullable = false)
private String name;
public MetricDimensionDbId() {
}
public MetricDimensionDbId(final BinaryId dimensionSetId, final String name) {
this.dimensionSetId = dimensionSetId;
this.name = name;
}
public MetricDimensionDbId setDimensionSetId(final BinaryId dimensionSetId) {
this.dimensionSetId = dimensionSetId;
return this;
}
public MetricDimensionDbId setName(final String name) {
this.name = name;
return this;
}
public String getName() {
return name;
}
public BinaryId getDimensionSetId() {
return dimensionSetId;
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
MetricDimensionDbId that = (MetricDimensionDbId) o;
return Objects.equal(this.dimensionSetId, that.dimensionSetId) &&
Objects.equal(this.name, that.name);
}
@Override
public int hashCode() {
return Objects.hashCode(dimensionSetId, name);
}
@Override
public String toString() {
return Objects.toStringHelper(this)
.add("dimensionSetId", dimensionSetId)
.add("name", name)
.toString();
}
}

View File

@ -0,0 +1,121 @@
/*
* Copyright 2015 FUJITSU LIMITED
*
* 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 monasca.common.hibernate.db;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.EnumType;
import javax.persistence.Enumerated;
import javax.persistence.NamedQueries;
import javax.persistence.NamedQuery;
import javax.persistence.Table;
import org.joda.time.DateTime;
import monasca.common.model.alarm.AlarmNotificationMethodType;
@Entity
@Table(name = "notification_method")
@NamedQueries({
@NamedQuery(
name = NotificationMethodDb.Queries.NOTIFICATION_BY_TENANT_ID_AND_NAME,
query = "from NotificationMethodDb where tenant_id = :tenantId and name = :name"
),
@NamedQuery(
name = NotificationMethodDb.Queries.FIND_BY_TENANT_ID_AND_ID,
query = "from NotificationMethodDb where tenant_id = :tenantId and id = :id"
),
@NamedQuery(
name = NotificationMethodDb.Queries.DELETE_BY_ID,
query = "delete from NotificationMethodDb where id = :id"
)
})
public class NotificationMethodDb
extends AbstractAuditablePersistable<String> {
private static final long serialVersionUID = 106455752028781371L;
@Column(name = "tenant_id", length = 36, nullable = false)
private String tenantId;
@Column(name = "name", length = 250)
private String name;
@Column(name = "type", nullable = false)
@Enumerated(EnumType.STRING)
private AlarmNotificationMethodType type;
@Column(name = "address", length = 512, nullable = false)
private String address;
public NotificationMethodDb() {
super();
}
public NotificationMethodDb(String id,
String tenantId,
String name,
AlarmNotificationMethodType type,
String address,
DateTime created_at,
DateTime updated_at) {
super(id, created_at, updated_at);
this.tenantId = tenantId;
this.name = name;
this.type = type;
this.address = address;
}
public NotificationMethodDb setAddress(final String address) {
this.address = address;
return this;
}
public NotificationMethodDb setType(final AlarmNotificationMethodType type) {
this.type = type;
return this;
}
public NotificationMethodDb setName(final String name) {
this.name = name;
return this;
}
public NotificationMethodDb setTenantId(final String tenantId) {
this.tenantId = tenantId;
return this;
}
public String getTenantId() {
return this.tenantId;
}
public String getName() {
return this.name;
}
public AlarmNotificationMethodType getType() {
return this.type;
}
public String getAddress() {
return this.address;
}
public interface Queries {
String NOTIFICATION_BY_TENANT_ID_AND_NAME = "NotificationMethod.finByTenantIdAndName";
String DELETE_BY_ID = "NotificationMethod.deleteById";
String FIND_BY_TENANT_ID_AND_ID = "NotificationMethod.findByTenantIdAndId";
}
}

View File

@ -0,0 +1,133 @@
/*
* Copyright 2015 FUJITSU LIMITED
*
* 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 monasca.common.hibernate.db;
import javax.persistence.Basic;
import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.JoinColumn;
import javax.persistence.Lob;
import javax.persistence.ManyToOne;
import javax.persistence.NamedQueries;
import javax.persistence.NamedQuery;
import javax.persistence.Table;
import org.hibernate.annotations.OnDelete;
import org.hibernate.annotations.OnDeleteAction;
import org.hibernate.annotations.Type;
import org.joda.time.DateTime;
@Entity
@Table(name = "sub_alarm")
@NamedQueries({
@NamedQuery(
name = SubAlarmDb.Queries.BY_ALARMDEFINITION_ID,
query = "select sa from SubAlarmDb as sa, AlarmDb as a where sa.alarm.id=a.id and a.alarmDefinition.id = :id"
),
@NamedQuery(
name = SubAlarmDb.Queries.BY_ALARM_ID,
query = "from SubAlarmDb where alarm_id = :id"
),
@NamedQuery(
name = SubAlarmDb.Queries.UPDATE_EXPRESSION_BY_SUBEXPRESSION_ID,
query = "update SubAlarmDb set expression=:expression where subExpression.id=:alarmSubExpressionId"
)
})
public class SubAlarmDb
extends AbstractAuditablePersistable<String> {
private static final long serialVersionUID = 5719744905744636511L;
private static final String DEFAULT_EXPRESSION = "";
@JoinColumn(name = "alarm_id", nullable = false)
@ManyToOne(cascade = {CascadeType.REMOVE}, fetch = FetchType.LAZY, optional = false)
@OnDelete(action = OnDeleteAction.CASCADE)
private AlarmDb alarm;
@JoinColumn(name = "sub_expression_id")
@ManyToOne(cascade = CascadeType.REMOVE, fetch = FetchType.LAZY)
private SubAlarmDefinitionDb subExpression;
@Lob
@Type(type = "text")
@Basic(fetch = FetchType.LAZY)
@Column(name = "expression", nullable = false, length = 16777215)
private String expression = DEFAULT_EXPRESSION;
public SubAlarmDb() {
super();
}
public SubAlarmDb(String id,
AlarmDb alarm,
String expression,
DateTime created_at,
DateTime updated_at) {
this(id, alarm, null, expression, created_at, updated_at);
this.alarm = alarm;
this.expression = expression;
}
public SubAlarmDb(String id,
AlarmDb alarm,
SubAlarmDefinitionDb subExpression,
String expression,
DateTime created_at,
DateTime updated_at) {
super(id, created_at, updated_at);
this.alarm = alarm;
this.subExpression = subExpression;
this.expression = expression;
}
public SubAlarmDb setExpression(final String expression) {
this.expression = expression;
return this;
}
public SubAlarmDb setSubExpression(final SubAlarmDefinitionDb subExpression) {
this.subExpression = subExpression;
return this;
}
public SubAlarmDb setAlarm(final AlarmDb alarm) {
if (alarm != null) {
if (!alarm.hasSubAlarm(this)) {
alarm.addSubAlarm(this);
}
this.alarm = alarm;
}
return this;
}
public AlarmDb getAlarm() {
return this.alarm;
}
public SubAlarmDefinitionDb getSubExpression() {
return this.subExpression;
}
public String getExpression() {
return this.expression;
}
public interface Queries {
String BY_ALARMDEFINITION_ID = "SubAlarm.byAlarmDefinitionId";
String BY_ALARM_ID = "SubAlarm.byAlarmId";
String UPDATE_EXPRESSION_BY_SUBEXPRESSION_ID = "SubAlarm.updateExpressionBySubexpressionId";
}
}

View File

@ -0,0 +1,179 @@
/*
* Copyright 2015 FUJITSU LIMITED
*
* 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 monasca.common.hibernate.db;
import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.NamedQueries;
import javax.persistence.NamedQuery;
import javax.persistence.Table;
import monasca.common.model.alarm.AlarmOperator;
import org.hibernate.annotations.OnDelete;
import org.hibernate.annotations.OnDeleteAction;
import org.joda.time.DateTime;
@Entity
@Table(name = "sub_alarm_definition")
@NamedQueries({
@NamedQuery(
name = SubAlarmDefinitionDb.Queries.BY_ALARMDEFINITION_ID,
query = "from SubAlarmDefinitionDb sad where sad.alarmDefinition.id = :id order by sad.id"
),
@NamedQuery(
name = SubAlarmDefinitionDb.Queries.BY_ALARMDEFINITIONDIMENSION_SUBEXPRESSION_ID,
query = "SELECT sadd from " +
"SubAlarmDefinitionDb sad, " +
"SubAlarmDefinitionDimensionDb sadd " +
"where sadd.subAlarmDefinitionDimensionId.subExpression.id = sad.id " +
"AND sad.alarmDefinition.id = :id"
),
@NamedQuery(
name = SubAlarmDefinitionDb.Queries.DELETE_BY_IDS,
query = "delete SubAlarmDefinitionDb where id in :ids"
)
})
public class SubAlarmDefinitionDb
extends AbstractAuditablePersistable<String> {
private static final long serialVersionUID = 8898225134690206198L;
@JoinColumn(name = "alarm_definition_id", nullable = false)
@ManyToOne(cascade = {
CascadeType.REMOVE,
CascadeType.PERSIST,
CascadeType.REFRESH
}, fetch = FetchType.LAZY, optional = false)
@OnDelete(action = OnDeleteAction.CASCADE)
private AlarmDefinitionDb alarmDefinition;
@Column(name = "function", length = 10, nullable = false)
private String function;
@Column(name = "metric_name", length = 100)
private String metricName;
@Column(name = "operator", length = 5, nullable = false)
private String operator;
@Column(name = "threshold", nullable = false)
private Double threshold;
@Column(name = "period", length = 11, nullable = false)
private Integer period;
@Column(name = "periods", length = 11, nullable = false)
private Integer periods;
public SubAlarmDefinitionDb() {
super();
}
public SubAlarmDefinitionDb(String id,
AlarmDefinitionDb alarmDefinition,
String function,
String metricName,
String operator,
Double threshold,
Integer period,
Integer periods,
DateTime created_at,
DateTime updated_at) {
super(id, created_at, updated_at);
this.alarmDefinition = alarmDefinition;
this.function = function;
this.metricName = metricName;
this.operator = operator;
this.threshold = threshold;
this.period = period;
this.periods = periods;
}
public SubAlarmDefinitionDb setPeriods(final Integer periods) {
this.periods = periods;
return this;
}
public SubAlarmDefinitionDb setPeriod(final Integer period) {
this.period = period;
return this;
}
public SubAlarmDefinitionDb setThreshold(final Double threshold) {
this.threshold = threshold;
return this;
}
public SubAlarmDefinitionDb setOperator(final String operator) {
this.operator = operator;
return this;
}
public SubAlarmDefinitionDb setOperator(final AlarmOperator operator) {
return this.setOperator(operator.name().toUpperCase());
}
public SubAlarmDefinitionDb setMetricName(final String metricName) {
this.metricName = metricName;
return this;
}
public SubAlarmDefinitionDb setFunction(final String function) {
this.function = function;
return this;
}
public SubAlarmDefinitionDb setAlarmDefinition(final AlarmDefinitionDb alarmDefinition) {
this.alarmDefinition = alarmDefinition;
return this;
}
public AlarmDefinitionDb getAlarmDefinition() {
return this.alarmDefinition;
}
public String getFunction() {
return this.function;
}
public String getMetricName() {
return this.metricName;
}
public String getOperator() {
return this.operator;
}
public Double getThreshold() {
return this.threshold;
}
public Integer getPeriod() {
return this.period;
}
public Integer getPeriods() {
return this.periods;
}
public interface Queries {
String BY_ALARMDEFINITION_ID = "SubAlarmDefinition.byAlarmDefinitionId";
String BY_ALARMDEFINITIONDIMENSION_SUBEXPRESSION_ID = "SubAlarmDefinition.byAlarmDefinitionDimension.subExpressionId";
String DELETE_BY_IDS = "SubAlarmDefinition.deleteByIds";
}
}

View File

@ -0,0 +1,124 @@
/*
* Copyright 2015 FUJITSU LIMITED
*
* 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 monasca.common.hibernate.db;
import java.io.Serializable;
import javax.persistence.Column;
import javax.persistence.EmbeddedId;
import javax.persistence.Entity;
import javax.persistence.Table;
import com.google.common.base.Objects;
@Entity
@Table(name = "sub_alarm_definition_dimension")
public class SubAlarmDefinitionDimensionDb
implements Serializable {
private static final long serialVersionUID = -692669756593028956L;
@EmbeddedId
private SubAlarmDefinitionDimensionId subAlarmDefinitionDimensionId;
@Column(name = "value", length = 255, nullable = true)
private String value;
public SubAlarmDefinitionDimensionDb() {
this(null, "", null);
}
public SubAlarmDefinitionDimensionDb(SubAlarmDefinitionDimensionId subAlarmDefinitionDimensionId) {
super();
this.subAlarmDefinitionDimensionId = subAlarmDefinitionDimensionId;
}
public SubAlarmDefinitionDimensionDb(SubAlarmDefinitionDb subExpression,
String dimension_name,
String value) {
super();
this.subAlarmDefinitionDimensionId = new SubAlarmDefinitionDimensionId(subExpression, dimension_name);
this.value = value;
}
public SubAlarmDefinitionDimensionDb(SubAlarmDefinitionDimensionId subAlarmDefinitionDimensionId, String value) {
super();
this.subAlarmDefinitionDimensionId = subAlarmDefinitionDimensionId;
this.value = value;
}
public SubAlarmDefinitionDimensionId getSubAlarmDefinitionDimensionId() {
return subAlarmDefinitionDimensionId;
}
public SubAlarmDefinitionDimensionDb setSubAlarmDefinitionDimensionId(SubAlarmDefinitionDimensionId subAlarmDefinitionDimensionId) {
this.subAlarmDefinitionDimensionId = subAlarmDefinitionDimensionId;
return this;
}
public String getDimensionName() {
return this.requireId().getDimensionName();
}
public SubAlarmDefinitionDb getSubExpression() {
return this.requireId().getSubExpression();
}
public SubAlarmDefinitionDimensionDb setDimensionName(final String dimensionName) {
this.requireId().setDimensionName(dimensionName);
return this;
}
public SubAlarmDefinitionDimensionDb setSubExpression(final SubAlarmDefinitionDb subExpression) {
this.requireId().setSubExpression(subExpression);
return this;
}
public String getValue() {
return value;
}
public SubAlarmDefinitionDimensionDb setValue(String value) {
this.value = value;
return this;
}
private SubAlarmDefinitionDimensionId requireId() {
if (this.subAlarmDefinitionDimensionId == null) {
this.subAlarmDefinitionDimensionId = new SubAlarmDefinitionDimensionId();
}
return this.subAlarmDefinitionDimensionId;
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
SubAlarmDefinitionDimensionDb that = (SubAlarmDefinitionDimensionDb) o;
return Objects.equal(this.subAlarmDefinitionDimensionId, that.subAlarmDefinitionDimensionId);
}
@Override
public int hashCode() {
return Objects.hashCode(subAlarmDefinitionDimensionId);
}
@Override
public String toString() {
return Objects.toStringHelper(this)
.add("subAlarmDefinitionDimensionId", subAlarmDefinitionDimensionId)
.add("value", value)
.toString();
}
}

View File

@ -0,0 +1,87 @@
/*
* Copyright 2015 FUJITSU LIMITED
*
* 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 monasca.common.hibernate.db;
import java.io.Serializable;
import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Embeddable;
import javax.persistence.FetchType;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import com.google.common.base.Objects;
import org.hibernate.annotations.OnDelete;
import org.hibernate.annotations.OnDeleteAction;
@Embeddable
public class SubAlarmDefinitionDimensionId
implements Serializable {
private static final long serialVersionUID = -233531731474459939L;
@JoinColumn(name = "sub_alarm_definition_id", nullable = false)
@ManyToOne(cascade = CascadeType.REMOVE, fetch = FetchType.LAZY, optional = false)
@OnDelete(action = OnDeleteAction.CASCADE)
private SubAlarmDefinitionDb subExpression;
@Column(name = "dimension_name", length = 255, nullable = false)
private String dimensionName;
public SubAlarmDefinitionDimensionId() {
this(null, "");
}
public SubAlarmDefinitionDimensionId(SubAlarmDefinitionDb subExpression,
String dimensionName) {
super();
this.subExpression = subExpression;
this.dimensionName = dimensionName;
}
public SubAlarmDefinitionDimensionId setSubExpression(final SubAlarmDefinitionDb subExpression) {
this.subExpression = subExpression;
return this;
}
public SubAlarmDefinitionDimensionId setDimensionName(final String dimensionName) {
this.dimensionName = dimensionName;
return this;
}
public SubAlarmDefinitionDb getSubExpression() {
return this.subExpression;
}
public String getDimensionName() {
return this.dimensionName;
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
SubAlarmDefinitionDimensionId that = (SubAlarmDefinitionDimensionId) o;
return Objects.equal(this.subExpression, that.subExpression) &&
Objects.equal(this.dimensionName, that.dimensionName);
}
@Override
public int hashCode() {
return Objects.hashCode(subExpression, dimensionName);
}
}

View File

@ -0,0 +1,89 @@
/*
* Copyright 2015 FUJITSU LIMITED
*
* 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 monasca.common.hibernate.type;
import java.io.Serializable;
import java.util.Arrays;
import javax.annotation.Nonnull;
import com.google.common.collect.ComparisonChain;
public class BinaryId
implements Serializable, Comparable<BinaryId> {
private static final long serialVersionUID = -4185721060467793903L;
private static final char[] HEX_ARRAY = "0123456789ABCDEF".toCharArray();
private final byte[] bytes;
private transient String hexBytes = null;
public BinaryId(final byte[] bytes) {
this.bytes = bytes;
}
private static String bytesToHex(byte[] bytes) {
final char[] hexChars = new char[bytes.length * 2];
int j, v;
for (j = 0; j < bytes.length; j++) {
v = bytes[j] & 0xFF;
hexChars[j * 2] = HEX_ARRAY[v >>> 4];
hexChars[j * 2 + 1] = HEX_ARRAY[v & 0x0F];
}
return new String(hexChars);
}
public byte[] getBytes() {
return this.bytes;
}
public String toHexString() {
return this.convertToHex();
}
@Override
public boolean equals(final Object o) {
if (this == o) return true;
if (!(o instanceof BinaryId)) return false;
final BinaryId binaryId = (BinaryId) o;
return Arrays.equals(bytes, binaryId.bytes);
}
@Override
public int hashCode() {
return Arrays.hashCode(bytes);
}
@Override
public String toString() {
return this.convertToHex();
}
private String convertToHex() {
if (this.hexBytes == null) {
this.hexBytes = bytesToHex(this.bytes);
}
return this.hexBytes;
}
@Override
public int compareTo(@Nonnull final BinaryId binaryId) {
return ComparisonChain
.start()
.compare(this.toString(), binaryId.toString())
.result();
}
}

View File

@ -0,0 +1,104 @@
/*
* Copyright 2015 FUJITSU LIMITED
*
* 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 monasca.common.hibernate.type;
import java.io.Serializable;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Types;
import java.util.Arrays;
import java.util.Objects;
import org.hibernate.HibernateException;
import org.hibernate.engine.spi.SessionImplementor;
import org.hibernate.usertype.UserType;
public class BinaryIdType
implements UserType {
private static final int[] SQL_TYPES = new int[]{Types.BINARY};
@Override
public int[] sqlTypes() {
return SQL_TYPES;
}
@Override
public Class returnedClass() {
return BinaryId.class;
}
@Override
public boolean isMutable() {
return true;
}
@Override
public Object deepCopy(final Object value) throws HibernateException {
final BinaryId binaryId = (BinaryId) value;
final byte[] bytes = binaryId.getBytes();
if (bytes != null) {
return new BinaryId(
Arrays.copyOf(bytes, bytes.length)
);
}
return value;
}
@Override
public Object nullSafeGet(final ResultSet rs, final String[] names, final SessionImplementor session, final Object owner) throws HibernateException, SQLException {
byte[] bytes = rs.getBytes(names[0]);
if (rs.wasNull()) {
return null;
}
return new BinaryId(bytes);
}
@Override
public void nullSafeSet(final PreparedStatement st, final Object value, final int index, final SessionImplementor session) throws HibernateException, SQLException {
if (value == null) {
st.setNull(index, Types.VARBINARY);
} else {
st.setBytes(index, ((BinaryId) value).getBytes());
}
}
@Override
public Serializable disassemble(final Object value) throws HibernateException {
return (Serializable) this.deepCopy(value);
}
@Override
public Object assemble(final Serializable cached, final Object owner) throws HibernateException {
return this.deepCopy(cached);
}
@Override
public Object replace(final Object original, final Object target, final Object owner) throws HibernateException {
return this.deepCopy(original);
}
@Override
public boolean equals(final Object x, final Object y) throws HibernateException {
return Objects.deepEquals(x, y);
}
@Override
public int hashCode(final Object x) throws HibernateException {
return Objects.hashCode(x);
}
}

View File

@ -0,0 +1,21 @@
/*
* Copyright (c) 2014 Hewlett-Packard Development Company, L.P.
*
* 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 monasca.common.model.alarm;
public enum AlarmNotificationMethodType {
EMAIL, WEBHOOK, PAGERDUTY;
}

View File

@ -0,0 +1,21 @@
/*
* Copyright (c) 2014 Hewlett-Packard Development Company, L.P.
*
* 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 monasca.common.model.alarm;
public enum AlarmSeverity {
LOW, MEDIUM, HIGH, CRITICAL
}

View File

@ -37,6 +37,7 @@
<module>monasca-common-util</module>
<module>monasca-common-middleware</module>
<module>monasca-common-influxdb</module>
<module>monasca-common-hibernate</module>
</modules>
<profiles>