/*
* gvNIX is an open source tool for rapid application development (RAD).
* Copyright (C) 2010 Generalitat Valenciana
*
* This program is free software: you can redistribute it and/or modify it under
* the terms of the GNU General Public License as published by the Free Software
* Foundation, either version 3 of the License, or (at your option) any later
* version.
*
* This program is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
* FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
* details.
*
* You should have received a copy of the GNU General Public License along with
* this program. If not, see <http://www.gnu.org/licenses/>.
*/
package org.gvnix.dynamic.configuration.roo.addon.entity;
import org.apache.commons.lang3.ObjectUtils;
/**
* Dynamic configuration component entity.
*
* @author <a href="http://www.disid.com">DISID Corporation S.L.</a> made for <a
* href="http://www.dgti.gva.es">General Directorate for Information
* Technologies (DGTI)</a>
*/
public class DynComponent {
private String id;
private String name;
private DynPropertyList properties;
public DynComponent() {
super();
}
public DynComponent(String id, String name, DynPropertyList properties) {
super();
this.id = id;
this.name = name;
this.properties = properties;
}
public DynComponent(String name) {
super();
this.id = name;
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getName() {
if (name != null && name.length() > 0) {
return name;
}
return id.substring(id.lastIndexOf('.') + 1, id.length());
}
public void setName(String name) {
this.name = name;
}
public DynPropertyList getProperties() {
return properties;
}
public void addProperty(DynProperty property) {
properties.add(property);
}
/**
* {@inheritDoc} Name and property/value set, each on new line.
*/
@Override
public String toString() {
// Show the component name
StringBuilder buffer = new StringBuilder();
buffer.append(" * ");
buffer.append(getName());
// Show properties
for (DynProperty prop : properties) {
// Show the property and value if exists with format
buffer.append("\n");
buffer.append(" - ");
buffer.append(prop.getKey());
if (prop.getValue() == null) {
buffer.append(" = (UNDEFINED)");
}
else {
buffer.append(" = \"");
buffer.append(prop.getValue());
buffer.append('"');
}
}
return buffer.toString();
}
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
for (DynProperty property : properties) {
result = prime * result
+ ((property == null) ? 0 : property.hashCode());
}
return result;
}
/**
* Two components are equal if their properties are equal.
*
* @param obj Component to compare to
* @return Component equals
*/
@Override
public boolean equals(Object obj) {
if (ObjectUtils.equals(this, obj)) {
return true;
}
if (obj == null) {
return false;
}
if (!(obj instanceof DynComponent)) {
return false;
}
DynComponent other = (DynComponent) obj;
for (DynProperty component : properties) {
boolean exist = false;
for (DynProperty component2 : other.getProperties()) {
if (component.equals(component2)) {
exist = true;
break;
}
}
if (!exist) {
return false;
}
}
return true;
}
}