All pastes #493280 Raw Edit

Stuff

public text v1 · immutable
·published 2007-05-17 18:03 UTC
rendered paste body
package persistency;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.Vector;

import models.Comment;
import models.Location;

/**
 * LocationMapper.java
 * This class will be used when something with locations must be done, basicly this 
 * class will interact with the ImageMapper
 * 
 * @author 	Bert Schuyten
 * @since	2007
 */

public class LocationMapper extends CommentMapper {
	
	/**
	 * public constructor
	 * 
	 * @return	void
	 */
	public LocationMapper() {
		super("locations", 3);
	}
	
	/**
	 * Get a persistent location object by inserting the location id
	 * 
	 * @param	id location id
	 * @return	persistent location object
	 */
	public PersLocation getLocation(int id) {
		String sql = "SELECT x, y FROM " + getTable() + " WHERE id = '" + id + "'";
		
		try {
			ResultSet rs = select(sql);
			if(! rs.next()) return null;
			double x = rs.getDouble("x");
			double y = rs.getDouble("y");
			rs.close();
			
			Location l = new Location(x, y, getCommentByTid(id));
			
			return new PersLocation(id, l);
		} catch (SQLException e) {
			e.printStackTrace();
		}
		
		return null;
	}
	
	/**
	 * Edit a location object by inserting the new persistent location object
	 * 
	 * @param 	pl	persistent location object
	 * @return	amount of effected rows (normally one)
	 */
	public int editLocation(PersLocation pl) {
		String sql = "UPDATE " + getTable() + " SET x = '" + pl.getX() + "', y = '" + pl.getY() + "'";
		
		int updateCount = edit(sql);
		
		editCommentByTid(pl.getId(), (Comment) pl);
		
		return updateCount;
	}
	
	/**
	 * Insert a location into the database by passing the location object.
	 * 
	 * @param	l	location object
	 * @return	persistent location object
	 */
	public PersLocation addLocation(Location l) {
		String sql = "INSERT INTO " + getTable() + " (x, y) VALUES ('" + l.getX() + "', '" + l.getY() + "')";
		Comment c = (Comment) l;
		
		int id = insert(sql, c);
		
		return new PersLocation(id, l);
	}
	
	/**
	 * Delete a location from the database by id.
	 * 	
	 * @param 	lid	location id
	 * @return	amount of effected rows
	 */
	public int deleteLocation(int lid) {
		String sql = "DELETE FROM " + getTable() + " WHERE id = '" + lid + "'";
		
		deleteTaggedComment(lid);
		int updateCount = delete(sql);
		
		return updateCount;
	}
	
	/**
	 * Get all the locations who are present in the database.
	 * 
	 * @return	ArrayList with persistent location objects
	 */
	public ArrayList<PersLocation> getAllLocations() {
		Vector<Integer> v = getAllRowids();
		Iterator<Integer> it = v.iterator();
		ArrayList<PersLocation> locations = new ArrayList<PersLocation>();
		
		while(it.hasNext()) locations.add(getLocation(it.next()));
		
		return locations;
	}
	
	/**
	 * Get the id of the location name given in the parameter. On success return
	 * the id, on failure return 0
	 * 
	 * @param 	name	the location name
	 * @return	location id
	 */
	public int getLocationIdByName(String name) {
		return getCommentIdByTopic(name);
	}
	
	/**
	 * Checks for location existance.
	 * 
	 * @param	name	the location name
	 * @return	Boolean
	 */
	public Boolean locationExists(String name) {
		if(getLocationIdByName(name) == 0) return false;
		else return true;
	}
}