Merge pull request #122 from bsinno/reduce-duplicate-metadata

Reduce duplicate metadata code
This commit is contained in:
Michael Hirsch
2016-04-07 08:34:49 +02:00
4 changed files with 113 additions and 124 deletions

View File

@@ -8,6 +8,7 @@
*/ */
package org.eclipse.hawkbit.simulator; package org.eclipse.hawkbit.simulator;
import java.net.MalformedURLException;
import java.net.URL; import java.net.URL;
import org.eclipse.hawkbit.simulator.amqp.SpSenderService; import org.eclipse.hawkbit.simulator.amqp.SpSenderService;
@@ -47,7 +48,7 @@ public class SimulatorStartup implements ApplicationListener<ContextRefreshedEve
repository.add(deviceFactory.createSimulatedDevice(deviceId, autostart.getTenant(), repository.add(deviceFactory.createSimulatedDevice(deviceId, autostart.getTenant(),
autostart.getApi(), autostart.getPollDelay(), new URL(autostart.getEndpoint()), autostart.getApi(), autostart.getPollDelay(), new URL(autostart.getEndpoint()),
autostart.getGatewayToken())); autostart.getGatewayToken()));
} catch (final Exception e) { } catch (final MalformedURLException e) {
LOGGER.error("Creation of simulated device at startup failed.", e); LOGGER.error("Creation of simulated device at startup failed.", e);
} }

View File

@@ -8,10 +8,6 @@
*/ */
package org.eclipse.hawkbit.repository.model; package org.eclipse.hawkbit.repository.model;
import java.io.Serializable;
import javax.persistence.Basic;
import javax.persistence.Column;
import javax.persistence.ConstraintMode; import javax.persistence.ConstraintMode;
import javax.persistence.Entity; import javax.persistence.Entity;
import javax.persistence.FetchType; import javax.persistence.FetchType;
@@ -29,63 +25,31 @@ import javax.persistence.Table;
@IdClass(DsMetadataCompositeKey.class) @IdClass(DsMetadataCompositeKey.class)
@Entity @Entity
@Table(name = "sp_ds_metadata") @Table(name = "sp_ds_metadata")
public class DistributionSetMetadata implements Serializable { public class DistributionSetMetadata extends MetaData {
private static final long serialVersionUID = 1L; private static final long serialVersionUID = 1L;
@Id
@Column(name = "meta_key", length = 128)
private String key;
@Column(name = "meta_value", length = 4000)
@Basic
private String value;
@Id @Id
@ManyToOne(fetch = FetchType.LAZY) @ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "ds_id", foreignKey = @ForeignKey(value = ConstraintMode.CONSTRAINT, name = "fk_metadata_ds")) @JoinColumn(name = "ds_id", foreignKey = @ForeignKey(value = ConstraintMode.CONSTRAINT, name = "fk_metadata_ds"))
private DistributionSet distributionSet; private DistributionSet distributionSet;
public DistributionSetMetadata() { public DistributionSetMetadata() {
// Default constructor for JPA. // default public constructor for JPA
} }
/**
* Parameter constructor.
*
* @param key
* @param distributionSet
* @param value
*/
public DistributionSetMetadata(final String key, final DistributionSet distributionSet, final String value) { public DistributionSetMetadata(final String key, final DistributionSet distributionSet, final String value) {
this.key = key; super(key, value);
this.distributionSet = distributionSet; this.distributionSet = distributionSet;
this.value = value;
} }
public DsMetadataCompositeKey getId() { public DsMetadataCompositeKey getId() {
return new DsMetadataCompositeKey(distributionSet, key); return new DsMetadataCompositeKey(distributionSet, getKey());
}
public String getKey() {
return key;
}
public void setKey(final String key) {
this.key = key;
} }
public void setDistributionSet(final DistributionSet distributionSet) { public void setDistributionSet(final DistributionSet distributionSet) {
this.distributionSet = distributionSet; this.distributionSet = distributionSet;
} }
public String getValue() {
return value;
}
public void setValue(final String value) {
this.value = value;
}
public DistributionSet getDistributionSet() { public DistributionSet getDistributionSet() {
return distributionSet; return distributionSet;
} }
@@ -93,21 +57,14 @@ public class DistributionSetMetadata implements Serializable {
@Override @Override
public int hashCode() { public int hashCode() {
final int prime = 31; final int prime = 31;
int result = 1; int result = super.hashCode();
result = prime * result + (distributionSet == null ? 0 : distributionSet.hashCode()); result = prime * result + ((distributionSet == null) ? 0 : distributionSet.hashCode());
result = prime * result + (key == null ? 0 : key.hashCode());
return result; return result;
} }
@Override @Override
public boolean equals(final Object obj) { public boolean equals(final Object obj) {
if (this == obj) { if (!super.equals(obj)) {
return true;
}
if (obj == null) {
return false;
}
if (!(obj instanceof DistributionSetMetadata)) {
return false; return false;
} }
final DistributionSetMetadata other = (DistributionSetMetadata) obj; final DistributionSetMetadata other = (DistributionSetMetadata) obj;
@@ -118,14 +75,6 @@ public class DistributionSetMetadata implements Serializable {
} else if (!distributionSet.equals(other.distributionSet)) { } else if (!distributionSet.equals(other.distributionSet)) {
return false; return false;
} }
if (key == null) {
if (other.key != null) {
return false;
}
} else if (!key.equals(other.key)) {
return false;
}
return true; return true;
} }
} }

View File

@@ -0,0 +1,98 @@
/**
* Copyright (c) 2015 Bosch Software Innovations GmbH and others.
*
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* which accompanies this distribution, and is available at
* http://www.eclipse.org/legal/epl-v10.html
*/
package org.eclipse.hawkbit.repository.model;
import java.io.Serializable;
import javax.persistence.Basic;
import javax.persistence.Column;
import javax.persistence.Id;
import javax.persistence.MappedSuperclass;
/**
* Meta data for entities.
*
*/
@MappedSuperclass
public abstract class MetaData implements Serializable {
private static final long serialVersionUID = 1L;
@Id
@Column(name = "meta_key", length = 128)
private String key;
@Column(name = "meta_value", length = 4000)
@Basic
private String value;
public MetaData(final String key, final String value) {
super();
this.key = key;
this.value = value;
}
public MetaData() {
// Default constructor needed for JPA entities
}
public String getKey() {
return key;
}
public void setKey(final String key) {
this.key = key;
}
public String getValue() {
return value;
}
public void setValue(final String value) {
this.value = value;
}
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + ((key == null) ? 0 : key.hashCode());
result = prime * result + ((value == null) ? 0 : value.hashCode());
return result;
}
@Override
public boolean equals(final Object obj) {
if (this == obj) {
return true;
}
if (obj == null) {
return false;
}
if (!(this.getClass().isInstance(obj))) {
return false;
}
final MetaData other = (MetaData) obj;
if (key == null) {
if (other.key != null) {
return false;
}
} else if (!key.equals(other.key)) {
return false;
}
if (value == null) {
if (other.value != null) {
return false;
}
} else if (!value.equals(other.value)) {
return false;
}
return true;
}
}

View File

@@ -8,9 +8,6 @@
*/ */
package org.eclipse.hawkbit.repository.model; package org.eclipse.hawkbit.repository.model;
import java.io.Serializable;
import javax.persistence.Column;
import javax.persistence.ConstraintMode; import javax.persistence.ConstraintMode;
import javax.persistence.Entity; import javax.persistence.Entity;
import javax.persistence.FetchType; import javax.persistence.FetchType;
@@ -28,53 +25,25 @@ import javax.persistence.Table;
@IdClass(SwMetadataCompositeKey.class) @IdClass(SwMetadataCompositeKey.class)
@Entity @Entity
@Table(name = "sp_sw_metadata") @Table(name = "sp_sw_metadata")
public class SoftwareModuleMetadata implements Serializable { public class SoftwareModuleMetadata extends MetaData {
private static final long serialVersionUID = 1L; private static final long serialVersionUID = 1L;
@Id
@Column(name = "meta_key", length = 128)
private String key;
@Column(name = "meta_value", length = 4000)
private String value;
@Id @Id
@ManyToOne(targetEntity = SoftwareModule.class, fetch = FetchType.LAZY) @ManyToOne(targetEntity = SoftwareModule.class, fetch = FetchType.LAZY)
@JoinColumn(name = "sw_id", foreignKey = @ForeignKey(value = ConstraintMode.CONSTRAINT, name = "fk_metadata_sw")) @JoinColumn(name = "sw_id", foreignKey = @ForeignKey(value = ConstraintMode.CONSTRAINT, name = "fk_metadata_sw"))
private SoftwareModule softwareModule; private SoftwareModule softwareModule;
/**
* Default constructor for JPA.
*/
public SoftwareModuleMetadata() { public SoftwareModuleMetadata() {
// default constructor for JPA. // default public constructor for JPA
} }
/**
* Standard constructor.
*
* @param key
* of the meta data element
* @param softwareModule
* @param value
* of the meta data element
*/
public SoftwareModuleMetadata(final String key, final SoftwareModule softwareModule, final String value) { public SoftwareModuleMetadata(final String key, final SoftwareModule softwareModule, final String value) {
this.key = key; super(key, value);
this.softwareModule = softwareModule; this.softwareModule = softwareModule;
this.value = value;
} }
public SwMetadataCompositeKey getId() { public SwMetadataCompositeKey getId() {
return new SwMetadataCompositeKey(softwareModule, key); return new SwMetadataCompositeKey(softwareModule, getKey());
}
public String getValue() {
return value;
}
public void setValue(final String value) {
this.value = value;
} }
public SoftwareModule getSoftwareModule() { public SoftwareModule getSoftwareModule() {
@@ -85,47 +54,20 @@ public class SoftwareModuleMetadata implements Serializable {
this.softwareModule = softwareModule; this.softwareModule = softwareModule;
} }
public String getKey() {
return key;
}
public void setKey(final String key) {
this.key = key;
}
@Override
public String toString() {
return "SoftwareModuleMetadata [key=" + key + ", value=" + value + ", softwareModule=" + softwareModule + "]";
}
@Override @Override
public int hashCode() { public int hashCode() {
final int prime = 31; final int prime = 31;
int result = 1; int result = super.hashCode();
result = prime * result + ((key == null) ? 0 : key.hashCode());
result = prime * result + ((softwareModule == null) ? 0 : softwareModule.hashCode()); result = prime * result + ((softwareModule == null) ? 0 : softwareModule.hashCode());
return result; return result;
} }
@Override @Override
public boolean equals(final Object obj) { public boolean equals(final Object obj) {
if (this == obj) { if (!super.equals(obj)) {
return true;
}
if (obj == null) {
return false;
}
if (!(obj instanceof SoftwareModuleMetadata)) {
return false; return false;
} }
final SoftwareModuleMetadata other = (SoftwareModuleMetadata) obj; final SoftwareModuleMetadata other = (SoftwareModuleMetadata) obj;
if (key == null) {
if (other.key != null) {
return false;
}
} else if (!key.equals(other.key)) {
return false;
}
if (softwareModule == null) { if (softwareModule == null) {
if (other.softwareModule != null) { if (other.softwareModule != null) {
return false; return false;
@@ -135,5 +77,4 @@ public class SoftwareModuleMetadata implements Serializable {
} }
return true; return true;
} }
} }