Java tutorial
/* * 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; import java.net.URI; import java.text.ParseException; import java.text.SimpleDateFormat; import java.util.Date; import javax.ws.rs.core.EntityTag; import javax.ws.rs.core.MultivaluedMap; import javax.xml.bind.annotation.XmlAccessorType; import javax.xml.bind.annotation.XmlAttribute; import javax.xml.bind.annotation.XmlRootElement; import javax.xml.bind.annotation.XmlTransient; import javax.xml.bind.annotation.XmlType; import javax.xml.bind.annotation.XmlAccessType; import org.apache.commons.lang3.builder.HashCodeBuilder; import com.github.carlomicieli.model.Player; import com.github.carlomicieli.model.PlayerPositions; import com.github.carlomicieli.model.Team; import com.sun.jersey.server.linking.Ref; import com.sun.jersey.server.linking.Ref.Style; /** * The player representation is used to provide a wrapper for * the entity class. * @author Carlo P. Micieli * */ @XmlRootElement(name = "player", namespace = "http://schemas.fantasyfootball.com/2012/04") @XmlAccessorType(XmlAccessType.FIELD) @XmlType(propOrder = { "playerId", "self", "firstName", "lastName", "teamName", "team", "position", "college", "birthday" }) public class PlayerRepresentation { @XmlAttribute private long playerId; private String firstName; private String lastName; private String teamName; private String position; private String college; private Date birthday; @XmlTransient private Team _team; //JAXB will ignore this field @Ref(style = Style.ABSOLUTE, value = "/players/${instance.playerId}") URI self; @Ref(style = Style.ABSOLUTE, value = "/teams/${instance.team.id}") URI team; protected PlayerRepresentation() { } /** * Initialize a player representation. * @param formParams the form values. */ public PlayerRepresentation(MultivaluedMap<String, String> formParams) { try { this.playerId = Long.parseLong(formParams.getFirst("playerId")); } catch (NumberFormatException nfe) { this.playerId = 0L; } this.firstName = formParams.getFirst("firstName"); this.lastName = formParams.getFirst("lastName"); this.position = formParams.getFirst("position"); this.teamName = formParams.getFirst("teamName"); this.college = formParams.getFirst("college"); try { this.birthday = parseDate(formParams.getFirst("birthday")); } catch (ParseException ex) { } } /** * Initialize a player representation. * @param p the inner player instance for this representation. */ public PlayerRepresentation(Player p) { setPlayerId(p.getId()); setFirstName(p.getFirstName()); setLastName(p.getLastName()); setPosition(p.getPosition()); setCollege(p.getCollege()); setTeam(p.getTeam()); setBirthday(p.getBirthday()); } /** * Return the inner player instance for this representation. * @return a player. */ public Player toPlayer() { return new Player(getPlayerId(), getFirstName(), getLastName(), PlayerPositions.valueOf(getPosition()), getTeam(), getCollege(), getBirthday()); } public long getPlayerId() { return playerId; } public void setPlayerId(long playerId) { this.playerId = playerId; } public Date getBirthday() { return birthday; } public void setBirthday(Date birthday) { this.birthday = birthday; } public String getFirstName() { return firstName; } public void setFirstName(String firstName) { this.firstName = firstName; } public String getLastName() { return lastName; } public void setLastName(String lastName) { this.lastName = lastName; } public Team getTeam() { return _team; } public void setTeam(Team team) { this._team = team; if (this._team != null) { setTeamName(this._team.getName()); } } public String getTeamName() { return teamName; } public void setTeamName(String teamName) { this.teamName = teamName; } public String getPosition() { return position; } public void setPosition(String position) { this.position = position; } public String getCollege() { return college; } public void setCollege(String college) { this.college = college; } public URI getSelf() { return self; } public void setSelf(URI self) { this.self = self; } /** * Compute the ETag for the current player representation. * @return the ETag value. */ public EntityTag tag() { return new EntityTag(Long.toHexString(this.hashCode())); } @Override public int hashCode() { return new HashCodeBuilder(15, 37).append(getFirstName()).append(getLastName()).append(getPosition()) .append(getCollege()).append(getBirthday()).append(getTeamName()).toHashCode(); } private Date parseDate(String xmlDate) throws ParseException { SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd"); return sdf.parse(xmlDate); } }