/*
* Copyright 2012 the original author or authors.
*
* 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.github.carlomicieli.rest.representations;
import javax.ws.rs.WebApplicationException;
import javax.ws.rs.core.EntityTag;
import javax.ws.rs.core.MultivaluedMap;
import javax.ws.rs.core.Response.Status;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlRootElement;
import javax.xml.bind.annotation.XmlAccessType;
import org.apache.commons.lang3.builder.HashCodeBuilder;
import com.github.carlomicieli.entities.Brand;
/**
* The representation for a model railway company.
* @author Carlo Micieli
* @see com.github.carlomicieli.entities.Brand
*
*/
@XmlRootElement(name = "brand",
namespace = "http://schemas.trenako.com/2012/05")
@XmlAccessorType(XmlAccessType.FIELD)
public class BrandRepresentation {
private final Brand brand;
BrandRepresentation() {
this.brand = new Brand();
}
/**
* Creates a new brand representation.
* @param brand the inner brand entity.
*/
public BrandRepresentation(Brand brand) {
if( brand==null )
throw new WebApplicationException(Status.NOT_FOUND);
this.brand = brand;
}
/**
* Creates a new brand representation.
* @param formParams the form values.
*/
public BrandRepresentation(MultivaluedMap<String, String> formParams) {
this();
long id = 0;
try {
id = Long.parseLong(formParams.getFirst("id"));
}
catch( NumberFormatException ex ) {}
setId(id);
setName(formParams.getFirst("name"));
setWebsite(formParams.getFirst("website"));
setDescription(formParams.getFirst("description"));
}
public Brand getInner() {
return this.brand;
}
public long getId() {
return this.brand.getId();
}
public void setId(long id) {
this.brand.setId(id);
}
public String getName() {
return this.brand.getName();
}
public void setName(String name) {
this.brand.setName(name);
}
public String getWebsite() {
return this.brand.getWebsite();
}
public void setWebsite(String website) {
this.brand.setWebsite(website);
}
public String getDescription() {
return this.brand.getDescription();
}
public void setDescription(String desc) {
this.brand.setDescription(desc);
}
/**
* Compute the ETag for the current player representation.
* @return the ETag value.
*/
public EntityTag tag() {
return new EntityTag(Long.toHexString(this.hashCode()));
}
/**
* Returns a string representation of this <em>BrandRepresentation</em>.
* @return a string representation of the object.
*/
@Override
public int hashCode() {
return new HashCodeBuilder(15, 37)
.append(getName())
.append(getDescription())
.append(getWebsite())
.toHashCode();
}
}