⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 abstractjdbcclinic.java

📁 spring的源代码
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
package org.springframework.samples.petclinic.jdbc;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Types;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import javax.sql.DataSource;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import org.springframework.context.ApplicationContextException;
import org.springframework.dao.DataAccessException;
import org.springframework.jdbc.core.SqlParameter;
import org.springframework.jdbc.core.support.JdbcDaoSupport;
import org.springframework.jdbc.object.MappingSqlQuery;
import org.springframework.jdbc.object.SqlUpdate;
import org.springframework.orm.ObjectRetrievalFailureException;
import org.springframework.samples.petclinic.Clinic;
import org.springframework.samples.petclinic.Entity;
import org.springframework.samples.petclinic.Owner;
import org.springframework.samples.petclinic.Pet;
import org.springframework.samples.petclinic.PetType;
import org.springframework.samples.petclinic.Specialty;
import org.springframework.samples.petclinic.Vet;
import org.springframework.samples.petclinic.Visit;
import org.springframework.samples.petclinic.util.EntityUtils;

/**
 * Base class for JDBC implementations of the Clinic interface.
 *
 * @author Ken Krebs
 * @author Juergen Hoeller
 */
abstract public class AbstractJdbcClinic extends JdbcDaoSupport implements Clinic {

	/** Logger for this class and subclasses */
	protected final Log logger = LogFactory.getLog(getClass());

	/** Holds all vets Query Object. */
	private VetsQuery vetsQuery;

	/** Holds specialties Query Object. */
	private SpecialtiesQuery specialtiesQuery;

	/** Holds vet specialties Query Object. */
	private VetSpecialtiesQuery vetSpecialtiesQuery;

	/** Holds owners by name Query Object. */
	private OwnersByNameQuery ownersByNameQuery;

	/** Holds owner by id Query Object. */
	private OwnerQuery ownerQuery;

	/** Holds owner Insert Object. */
	private OwnerInsert ownerInsert;

	/** Holds owner Update Object. */
	private OwnerUpdate ownerUpdate;

	/** Holds pets by owner Query Object. */
	private PetsByOwnerQuery petsByOwnerQuery;

	/** Holds pets by owner Query Object. */
	private PetQuery petQuery;

	/** Holds pet Insert Object. */
	private PetInsert petInsert;

	/** Holds pet Update Object. */
	private PetUpdate petUpdate;

	/** Holds pet types Query Object. */
	private PetTypesQuery petTypesQuery;

	/** Holds visits Query Object. */
	private VisitsQuery visitsQuery;

	/** Holds Visit Insert Object. */
	private VisitInsert visitInsert;


	protected void setVetsQuery(VetsQuery vetsQuery) {
		this.vetsQuery = vetsQuery;
	}

	protected void setSpecialtiesQuery(SpecialtiesQuery specialtiesQuery) {
		this.specialtiesQuery = specialtiesQuery;
	}

	protected void setVetSpecialtiesQuery(VetSpecialtiesQuery vetSpecialtiesQuery) {
		this.vetSpecialtiesQuery = vetSpecialtiesQuery;
	}

	protected void setOwnersByNameQuery(OwnersByNameQuery ownersByNameQuery) {
		this.ownersByNameQuery = ownersByNameQuery;
	}

	protected void setOwnerQuery(OwnerQuery ownerQuery) {
		this.ownerQuery = ownerQuery;
	}

	protected void setOwnerInsert(OwnerInsert ownerInsert) {
		this.ownerInsert = ownerInsert;
	}

	protected void setOwnerUpdate(OwnerUpdate ownerUpdate) {
		this.ownerUpdate = ownerUpdate;
	}

	protected void setPetsByOwnerQuery(PetsByOwnerQuery petsByOwnerQuery) {
		this.petsByOwnerQuery = petsByOwnerQuery;
	}

	protected void setPetQuery(PetQuery petQuery) {
		this.petQuery = petQuery;
	}

	protected void setPetInsert(PetInsert petInsert) {
		this.petInsert = petInsert;
	}

	protected void setPetUpdate(PetUpdate petUpdate) {
		this.petUpdate = petUpdate;
	}

	protected void setPetTypesQuery(PetTypesQuery petTypesQuery) {
		this.petTypesQuery = petTypesQuery;
	}

	protected void setVisitsQuery(VisitsQuery visitsQuery) {
		this.visitsQuery = visitsQuery;
	}

	protected void setVisitInsert(VisitInsert visitInsert) {
		this.visitInsert = visitInsert;
	}


	protected void initDao() throws ApplicationContextException {
		if (vetsQuery == null)
			vetsQuery = new VetsQuery(getDataSource());
		if (specialtiesQuery == null)
			specialtiesQuery = new SpecialtiesQuery(getDataSource());
		if (vetSpecialtiesQuery == null)
			vetSpecialtiesQuery = new VetSpecialtiesQuery(getDataSource());
		if (petTypesQuery == null)
			petTypesQuery = new PetTypesQuery(getDataSource());
		if (ownersByNameQuery == null)
			ownersByNameQuery = new OwnersByNameQuery(getDataSource());
		if (ownerQuery == null)
			ownerQuery = new OwnerQuery(getDataSource());
		if (ownerInsert == null)
			ownerInsert = new OwnerInsert(getDataSource());
		if (ownerUpdate == null)
			ownerUpdate = new OwnerUpdate(getDataSource());
		if (petsByOwnerQuery == null)
			petsByOwnerQuery = new PetsByOwnerQuery(getDataSource());
		if (petQuery == null)
			petQuery = new PetQuery(getDataSource());
		if (petInsert == null)
			petInsert = new PetInsert(getDataSource());
		if (petUpdate == null)
			petUpdate = new PetUpdate(getDataSource());
		if (visitsQuery == null)
			visitsQuery = new VisitsQuery(getDataSource());
		if (visitInsert == null)
			visitInsert = new VisitInsert(getDataSource());
	}


	// START of Clinic implementation section *******************************

	public List getVets() throws DataAccessException {
		// establish the Map of all vets
		List vets = vetsQuery.execute();

		// establish the map of all the possible specialties
		List specialties = specialtiesQuery.execute();

		// establish each vet's List of specialties
		Iterator vi = vets.iterator();
		while (vi.hasNext()) {
			Vet vet = (Vet) vi.next();
			List vetSpecialtiesIds = vetSpecialtiesQuery.execute(vet.getId());
			Iterator vsi = vetSpecialtiesIds.iterator();
			while (vsi.hasNext()) {
				long specialtyId = ((Long) vsi.next()).longValue();
				Specialty specialty = (Specialty) EntityUtils.getById(specialties, Specialty.class, specialtyId);
				vet.addSpecialty(specialty);
			}
		}

		return vets;
	}

	public List getPetTypes() throws DataAccessException {
		return petTypesQuery.execute();
	}

	/** Method loads owners plus pets and visits if not already loaded */
	public List findOwners(String lastName) throws DataAccessException {
		List owners = ownersByNameQuery.execute(lastName + "%");
		loadOwnersPetsAndVisits(owners);
		return owners;
	}

	/** Method loads an owner plus pets and visits if not already loaded */
	public Owner loadOwner(long id)  throws DataAccessException {
		Owner owner = (Owner) ownerQuery.findObject(id);
		if (owner == null) {
			throw new ObjectRetrievalFailureException(Owner.class, new Long(id));
		}
		loadPetsAndVisits(owner);
		return owner;
	}

	public Pet loadPet(long id) throws DataAccessException {
		JdbcPet pet = (JdbcPet) petQuery.findObject(id);
		if (pet == null) {
			throw new ObjectRetrievalFailureException(Pet.class, new Long(id));
		}
		Owner owner = loadOwner(pet.getOwnerId());
		owner.addPet(pet);
		loadVisits(pet);
		return pet;
	}

	public void storeOwner(Owner owner) throws DataAccessException {
		if (owner.isNew()) {
			ownerInsert.insert(owner);
		}
		else {
			ownerUpdate.update(owner);
		}
	}

	public void storePet(Pet pet) throws DataAccessException {
		if (pet.isNew()) {
			petInsert.insert(pet);
		}
		else {
			petUpdate.update(pet);
		}
	}

	public void storeVisit(Visit visit) throws DataAccessException {
		if (visit.isNew()) {
			visitInsert.insert(visit);
		}
		else {
			throw new UnsupportedOperationException("Visit update not supported");
		}
	}

	// END of Clinic implementation section *******************************


	/**
	 * Method maps a List of Entitys keyed to their ids
	 * @param list containing Entitys
	 * @return Map containing Entitys
	 */
	protected final Map mapEntityList(List list) {
		Map map = new HashMap();
		Iterator iterator = list.iterator();
		while (iterator.hasNext()) {
			Entity entity = (Entity) iterator.next();
			map.put(new Long(entity.getId()), entity);
		}
		return map;
	}

	/**
	 * Method to retrieve the <code>Visit</code> data for a <code>Pet</code>.
	 * @param pet
	 */
	protected void loadVisits(JdbcPet pet) {
		pet.setType((PetType) EntityUtils.getById(getPetTypes(), PetType.class, pet.getTypeId()));
		List visits = visitsQuery.execute(pet.getId());
		Iterator vi = visits.iterator();
		while (vi.hasNext()) {
			Visit visit = (Visit) vi.next();
			pet.addVisit(visit);
		}
	}

	/**
	 * Method to retrieve the <code>Pet</code> and <code>Visit</code>
	 * data for an <code>Owner</code>.
	 * @param owner
	 */
	protected void loadPetsAndVisits(Owner owner) {
		List pets = petsByOwnerQuery.execute(owner.getId());
		Iterator pi = pets.iterator();
		while (pi.hasNext()) {
			JdbcPet pet = (JdbcPet) pi.next();
			owner.addPet(pet);
			loadVisits(pet);
		}
	}

	/**
	 * Method to retrieve a <code>List</code> of <code>Owner</code>s
	 * and their <code>Pet</code> and <code>Visit</code> data.
	 * @param owners <code>List</code>.
	 * @see #loadPetsAndVisits(Owner)
	 */
	protected void loadOwnersPetsAndVisits(List owners) {
		Iterator oi = owners.iterator();
		while (oi.hasNext()) {
			Owner owner = (Owner) oi.next();
			loadPetsAndVisits(owner);
		}
	}

	protected void retrieveIdentity(final Entity entity) {
		entity.setId(getJdbcTemplate().queryForLong(getIdentityQuery()));
	}

	/**
	 * Return the identity query for the particular database:
	 * a query that can be used to retrieve the id of a row
	 * that has just been inserted.
	 * @return the identity query
	 */
	protected abstract String getIdentityQuery();


	// ************* RdbmsOperation Objects section ***************

	/**
	 *  Base class for all <code>Vet</code> Query Objects.
	 */
	protected class VetsQuery extends MappingSqlQuery {

		/**
		 *  Creates a new instance of VetsQuery
		 *  @param ds the DataSource to use for the query.
		 *  @param sql Value of the SQL to use for the query.
		 */
		protected VetsQuery(DataSource ds, String sql) {
			super(ds, sql);
		}

		/**
		 *  Creates a new instance of VetsQuery that returns all vets
		 *  @param ds the DataSource to use for the query.
		 */
		protected VetsQuery(DataSource ds) {
			super(ds, "SELECT id,first_name,last_name FROM vets ORDER BY last_name,first_name");
			compile();
		}

		protected Object mapRow(ResultSet rs, int rownum) throws SQLException {
			Vet vet = new Vet();
			vet.setId(rs.getLong("id"));
			vet.setFirstName(rs.getString("first_name"));
			vet.setLastName(rs.getString("last_name"));
			return vet;
		}
	}


	/**
	 *  All <code>Vet</code>s specialties Query Object.
	 */
	protected class SpecialtiesQuery extends MappingSqlQuery {

		/**
		 *  Creates a new instance of SpecialtiesQuery
		 *  @param ds the DataSource to use for the query.
		 */
		protected SpecialtiesQuery(DataSource ds) {
			super(ds, "SELECT id,name FROM specialties");
			compile();

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -