Code Search for Developers
 
 
  

PersisterToXML.java from MASE: Agile Software Engineering at Krugle


Show PersisterToXML.java syntax highlighted

package persister.local;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.FilenameFilter;
import java.io.IOException;
import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.Hashtable;
import java.util.Iterator;
import java.util.List;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerConfigurationException;
import javax.xml.transform.TransformerException;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.TransformerFactoryConfigurationError;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;

import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.NamedNodeMap;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;

import persister.AbstractRoot;
import persister.Backlog;
import persister.ConnectionFailedException;
import persister.CouldNotLoadProjectException;
import persister.IndexCard;
import persister.SynchronousPersister;
import persister.ForbiddenOperationException;
import persister.IndexCardNotFoundException;
import persister.IndexCardWithChild;
import persister.Iteration;
import persister.Project;
import persister.StoryCard;
import persister.impl.data.BacklogDataObject;
import persister.impl.data.IterationDataObject;
import persister.impl.data.ProjectDataObject;
import persister.impl.data.StoryCardDataObject;
import persister.util.FileSystemIDGenerator;

public class PersisterToXML implements SynchronousPersister {

	private long maxid = 1;

	private File file;

	private Project rootproject;

	private FileSystemIDGenerator generator;

	private Hashtable<Long, AbstractRoot> lookup;

	private Timestamp start;

	private Timestamp end;

	private File projectDirectory;

	 private class ProjectXMLFileFilter implements FilenameFilter  {  
		    public boolean accept ( java.io.File f )   {  
		       return f.getName (  ) .endsWith ( ".xml" ) ; 
		     }

			public boolean accept(File dir, String name) {
				return (name.endsWith(".xml"));
			}
		  }  


	/**
	 * 
	 * @param localProjectDirPath path to the directory where the project files are stored
	 * @param projectName
	 * @throws CouldNotLoadProjectException 
	 */
	public PersisterToXML(String localProjectDirPath, String projectName) throws ConnectionFailedException, CouldNotLoadProjectException {
		this.generator = FileSystemIDGenerator.getInstance();

		this.lookup = new Hashtable<Long, AbstractRoot>();
		this.start = new Timestamp(0);
		this.end = new Timestamp(Long.MAX_VALUE);
		this.projectDirectory = new File(localProjectDirPath);
		this.projectDirectory.mkdir();
		assert(projectDirectory.isDirectory());
		this.file = new File(localProjectDirPath + File.separatorChar + projectName +".xml");
//		 if (!file.isFile()) 
//			 throw new ConnectionFailedException("Project could not be opened");
		 this.load(projectName);
	}

	/***************************************************************************
	 * LOAD AND SAVE *
	 **************************************************************************/

	public synchronized Project load(String projectName, Timestamp start, Timestamp end) throws CouldNotLoadProjectException{
		
			this.start = start;
			this.end = end;
			return load(projectName);
		
	}

	public synchronized Project load(String projectName) throws CouldNotLoadProjectException{
		this.rootproject = new ProjectDataObject();
		this.rootproject.setName(projectName);
		this.generator.init(1);
		this.rootproject.setId(this.generator.getNextID());
		this.lookup = new Hashtable<Long, AbstractRoot>();
		this.start = new Timestamp(0);
		this.end = new Timestamp(Long.MAX_VALUE);
		String filename = projectName + ".xml";
		file = new File(projectDirectory.getAbsolutePath() + File.separatorChar + filename);
		assert(file.isFile());
	
		DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
	
		DocumentBuilder loader;
		try {
			loader = factory.newDocumentBuilder();
			Document document;
			document = loader.parse(this.file);
	
			
			NodeList iterationElements = document.getElementsByTagName("Iteration");
			for (int i = 0; i < iterationElements.getLength(); i++) {
				this.rootproject.addIteration(loadIteration(iterationElements.item(i)));
			}
			
			NodeList backlogs = document.getElementsByTagName("Backlog");
			if (backlogs.getLength() > 0) {
				Backlog backlog = loadBacklog(document, backlogs.item(0));
				this.rootproject.setBacklog(backlog);
			}
			this.generator.init(maxid + 1);
			
				
	
		} catch (SAXException e) {
			e.printStackTrace();
			throw new CouldNotLoadProjectException("", e);
		} catch (IOException e) {
			e.printStackTrace();
		} catch (ParserConfigurationException e) {
			e.printStackTrace();
		}
	
		return this.rootproject;
	}


	/***************************************************************************
	 * CREATE OBJECTS FROM XML FILE HELPER METHODS *
	 **************************************************************************/
	private Backlog loadBacklog(Document document, Node node) {
		
		Backlog backlog = new BacklogDataObject();
			
			NamedNodeMap attributes = node.getAttributes();
	
			
			long blid = Long.valueOf(attributes.getNamedItem("ID")
					.getNodeValue().toString());
	
			if (this.maxid < blid) {
				this.maxid = blid;
			}
	
			backlog.setId(blid);
			backlog.setParent(Long.valueOf(attributes.getNamedItem("Parent")
					.getNodeValue().toString()));
	
			backlog.setName(attributes.getNamedItem("Name").getNodeValue()
					.toString());
	
			backlog.setLocationX(Integer.valueOf(attributes.getNamedItem(
					"XLocation").getNodeValue().toString()));
	
			backlog.setLocationY(Integer.valueOf(attributes.getNamedItem(
					"YLocation").getNodeValue().toString()));
	
			backlog.setWidth(Integer.valueOf(attributes.getNamedItem("Width")
					.getNodeValue().toString()));
			backlog.setHeight(Integer.valueOf(attributes.getNamedItem("Height")
					.getNodeValue().toString()));
	
			
			backlog.setStoryCardChildren(loadChildren(node));
			
			this.lookup.put(backlog.getId(), backlog);
	
		
		return backlog;
	}

	private IterationDataObject loadIteration(Node iterationElement) {
		
		IterationDataObject iteration = new IterationDataObject();
			
			Node node = iterationElement;
			NamedNodeMap attributes = node.getAttributes();
			
	
			long itid = Long.valueOf(attributes.getNamedItem("ID")
					.getNodeValue().toString());
			iteration.setId(itid);
	
			if (this.maxid < itid) {
				this.maxid = itid;
			}
	
			iteration.setId(itid);
			iteration.setParent(Long.valueOf(attributes.getNamedItem("Parent")
					.getNodeValue().toString()));
	
			iteration.setName(attributes.getNamedItem("Name").getNodeValue()
					.toString());
			iteration.setDescription(attributes.getNamedItem("Description")
					.getNodeValue().toString());
	
			iteration.setLocationX(Integer.valueOf(attributes.getNamedItem(
					"XLocation").getNodeValue().toString()));
			iteration.setLocationY(Integer.valueOf(attributes.getNamedItem(
					"YLocation").getNodeValue().toString()));
	
			iteration.setWidth(Integer.valueOf(attributes.getNamedItem("Width")
					.getNodeValue().toString()));
			iteration.setHeight(Integer.valueOf(attributes.getNamedItem(
					"Height").getNodeValue().toString()));
	
			iteration.setEndDate(Timestamp.valueOf(attributes.getNamedItem(
					"EndDate").getNodeValue().toString()));
			iteration.setStartDate(Timestamp.valueOf(attributes.getNamedItem(
					"StartDate").getNodeValue().toString()));
	
			iteration.setAvailableEffort(Float.valueOf(attributes.getNamedItem(
					"AvailableEffort").getNodeValue().toString()));
			iteration.setDescription(attributes.getNamedItem("Description")
					.getNodeValue().toString());
	
			iteration.setStatus(attributes.getNamedItem("Status")
					.getNodeValue().toString());
	
			
			iteration.setStoryCardChildren(loadChildren(node));
			this.lookup.put(iteration.getId(), iteration);
	
			return iteration;
			// make sure that the iteration ends or starts within the given
			// timebox
			// if((iteration.getEndDate().after(this.start) &&
			// iteration.getEndDate().before(this.end)) ||
			// (iteration.getStartDate().after(this.start) &&
			// (iteration.getStartDate().before(this.end)))) {
			// this.fireCreatedIterationEvent(iteration);
			//				
			// if(!(iteration.getStoryCardChildren().isEmpty())) {
			// for(StoryCard storycard : iteration.getStoryCardChildren()) {
			// this.fireCreatedStoryCardEvent(storycard);
			// }
			// }
			// }
		//}
		
	}
	

	
	private StoryCardDataObject createStoryCard(NamedNodeMap attributes) {
		StoryCardDataObject storycard = new StoryCardDataObject();
	
		long scid = Long.valueOf(attributes.getNamedItem("ID").getNodeValue()
				.toString());
	
		if (this.maxid < scid) {
			this.maxid = scid;
		}
	
		storycard.setId(scid);
		storycard.setParent(Long.valueOf(attributes.getNamedItem("Parent")
				.getNodeValue().toString()));
	
		storycard.setName(attributes.getNamedItem("Name").getNodeValue()
				.toString());
		storycard.setDescription(attributes.getNamedItem("Description")
				.getNodeValue().toString());
	
		storycard.setLocationX(Integer.valueOf(attributes.getNamedItem(
				"XLocation").getNodeValue().toString()));
		storycard.setLocationY(Integer.valueOf(attributes.getNamedItem(
				"YLocation").getNodeValue().toString()));
		storycard.setWidth(Integer.valueOf(attributes.getNamedItem("Width")
				.getNodeValue().toString()));
		storycard.setHeight(Integer.valueOf(attributes.getNamedItem("Height")
				.getNodeValue().toString()));
	
		storycard.setBestCaseEstimate(Float.valueOf(attributes.getNamedItem(
				"BestCase").getNodeValue().toString()));
		storycard.setWorstCaseEstimate(Float.valueOf(attributes.getNamedItem(
				"WorstCase").getNodeValue().toString()));
		storycard.setMostlikelyEstimate(Float.valueOf(attributes.getNamedItem(
				"MostLikely").getNodeValue().toString()));
		storycard.setActualEffort(Float.valueOf(attributes.getNamedItem(
				"Actual").getNodeValue().toString()));
		storycard.setStatus(attributes.getNamedItem("Status").getNodeValue()
				.toString());
	
		this.lookup.put(storycard.getId(), storycard);
		return storycard;
	}



	private List<StoryCard> loadChildren( Node parent) {
		
		NodeList children = parent.getChildNodes();
		List<StoryCard>  childrenIndexCards = new ArrayList<StoryCard>();
		if (children.getLength() > 0) {
			for (int i = 0; i < children.getLength(); i++) {
				Node node = children.item(i);
				NamedNodeMap attribute = node.getAttributes();
				StoryCardDataObject storycard = createStoryCard(attribute);
				childrenIndexCards.add(storycard);
				//this.lookup.put(storycard.getId(), storycard);
			}
		}
		return childrenIndexCards;
	}
	
	public synchronized boolean save() {
		return this.saveAs(this.file.getAbsolutePath());
	}
	
	public synchronized boolean saveAs(String path)
			{

		try {

			this.file = new File(path);
			this.file.createNewFile();

			StreamResult result = new StreamResult(this.file);

			Document doc = buildDOMToFile();
			DOMSource src = new DOMSource(doc);

			Transformer xformer;

			xformer = TransformerFactory.newInstance().newTransformer();
			xformer.transform(src, result);

			return true;
		} catch (TransformerConfigurationException e) {
			e.printStackTrace();
			return false;
		} catch (TransformerException e) {
			e.printStackTrace();
			return false;
		} catch (TransformerFactoryConfigurationError e) {
			e.printStackTrace();
			return false;
		} catch (IOException e) {
			e.printStackTrace();
			return false;
		}

	}

	private Document buildDOMToFile() {
		Document document = null;
		DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
	
		try {
			DocumentBuilder builder = factory.newDocumentBuilder();
			document = builder.newDocument();
	
			Element root = document.createElement("Project");
			root.setAttribute("ID", String.valueOf(this.rootproject.getId()));
			root.setAttribute("Name", this.rootproject.getName());
			document.appendChild(root);
	
			this.createIterationElements(document, root);
			this.createBacklogElements(document, root);
	
		} catch (Exception e) {
			e.printStackTrace();
		}
		return document;
	}

	

	public List<String> getProjectNames() {
		 String[] projectNames = this.getProjectDirectory().list(new ProjectXMLFileFilter());
		 List<String> projectNamesList = new ArrayList();
		 //remove the ".xml" from the end of the file name
		 for (int i = 0; i < projectNames.length; i++) {
			projectNames[i] = projectNames[i].substring(0, projectNames[i].length()-4);
			projectNamesList.add(projectNames[i]);
		}
		 return projectNamesList;
	}


	/***************************************************************************
	 * WRITE TO XML FILE HELPER METHODS *
	 **************************************************************************/
	private void createBacklogElements(Document document, Element parent) {
		BacklogDataObject backlog = (BacklogDataObject) this.rootproject
				.getBacklog();
	
		if (backlog != null) {
	
			Element element = document.createElement("Backlog");
	
			element.setAttribute("ID", String.valueOf(backlog.getId()));
			element.setAttribute("Parent", String.valueOf(backlog.getParent()));
	
			element.setAttribute("Name", backlog.getName());
	
			element.setAttribute("XLocation", String.valueOf(backlog
					.getLocationX()));
			element.setAttribute("YLocation", String.valueOf(backlog
					.getLocationY()));
	
			element.setAttribute("Width", String.valueOf(backlog.getWidth()));
			element.setAttribute("Height", String.valueOf(backlog.getHeight()));
	
			parent.appendChild(element);
	
			if (backlog.getStoryCardChildren().size() != 0) {
				createStoryCardElement(document, element, backlog
						.getStoryCardChildren());
			}
		}
	}

	private void createIterationElements(Document document, Element parent) {
	
		List<Iteration> iterations = this.rootproject.getIterationChildren();
	
		if (iterations != null) {
	
			for (Iteration iteration : iterations) {
				Element element = document.createElement("Iteration");
	
				element.setAttribute("ID", String.valueOf(iteration.getId()));
				element.setAttribute("Parent", String.valueOf(iteration
						.getParent()));
	
				element.setAttribute("Name", iteration.getName());
				element.setAttribute("Description", iteration.getDescription());
	
				element.setAttribute("XLocation", String.valueOf(iteration
						.getLocationX()));
				element.setAttribute("YLocation", String.valueOf(iteration
						.getLocationY()));
	
				element.setAttribute("Width", String.valueOf(iteration
						.getWidth()));
				element.setAttribute("Height", String.valueOf(iteration
						.getHeight()));
	
				element.setAttribute("EndDate", iteration.getEndDate()
						.toString());
				element.setAttribute("StartDate", iteration.getStartDate()
						.toString());
	
				element.setAttribute("AvailableEffort", String
						.valueOf(iteration.getAvailableEffort()));
	
				element.setAttribute("Status", String.valueOf(iteration
						.getStatus()));
				parent.appendChild(element);
	
				if (iteration.getStoryCardChildren().size() != 0) {
					createStoryCardElement(document, element, iteration
							.getStoryCardChildren());
				}
			}
		}
	}

	private void createStoryCardElement(Document document, Element parent, List<StoryCard> children) {
		for (StoryCard card : children) {
			Element element = document.createElement("StoryCard");
	
			element.setAttribute("ID", String.valueOf(card.getId()));
			element.setAttribute("Parent", String.valueOf(card.getParent()));
	
			element.setAttribute("Name", card.getName());
			element.setAttribute("Description", card.getDescription());
	
			element.setAttribute("XLocation", String.valueOf(card
					.getLocationX()));
			element.setAttribute("YLocation", String.valueOf(card
					.getLocationY()));
	
			element.setAttribute("Width", String.valueOf(card.getWidth()));
			element.setAttribute("Height", String.valueOf(card.getHeight()));
	
			element.setAttribute("BestCase", String.valueOf(card
					.getBestCaseEstimate()));
			element.setAttribute("MostLikely", String.valueOf(card
					.getMostlikelyEstimate()));
			element.setAttribute("WorstCase", String.valueOf(card
					.getWorstCaseEstimate()));
			element.setAttribute("Actual", String.valueOf(card
					.getActualEffort()));
	
			element.setAttribute("Status", String.valueOf(card.getStatus()));
	
			parent.appendChild(element);
		}
	}


	
	/***************************************************************************
	 * FIND HELPER METHODS *
	 **************************************************************************/
	public IndexCardWithChild findParentCardById(long id) throws IndexCardNotFoundException {
		if (this.lookup.containsKey(id)) {
			AbstractRoot card = this.lookup.get(id);
			if ((card instanceof IndexCardWithChild)) {
				return (IndexCardWithChild) card;
			}
		}
	
		throw new IndexCardNotFoundException("Parent IndexCard with ID " + id
				+ " not found!", id);
	
	}


	
	public IndexCard findCard(long id) throws IndexCardNotFoundException {
		if (this.lookup.containsKey(id)) 
			return (IndexCard) this.lookup.get(id);
		else 
			throw new IndexCardNotFoundException("IndexCard with ID " + id
					+ " not found!", id);
	}

/*****************************************************************************/	
	
	
	public synchronized Backlog createBacklog(int width, int height,
			int locationX, int locationY) throws ForbiddenOperationException {
		if (this.rootproject.getBacklog() != null) {
			throw new ForbiddenOperationException(
					"Only one Backlog allowed per Project!", rootproject
							.getBacklog().getId());
		} else {
			BacklogDataObject backlog = new BacklogDataObject();
			backlog.setId(generator.getNextID());
			backlog.setHeight(height);
			backlog.setWidth(width);
			backlog.setLocationX(locationX);
			backlog.setLocationY(locationY);

			this.rootproject.setBacklog(backlog);
			this.lookup.put(backlog.getId(), backlog);
			
			this.save();

			return backlog;
		}
	}

	public synchronized Iteration createIteration(String name,
			String description, int width, int height, int locationX,
			int locationY, float availableEffort, Timestamp startDate,
			Timestamp endDate) {
		IterationDataObject iteration = new IterationDataObject();
		iteration.setId(generator.getNextID());
		iteration.setName(name);
		iteration.setDescription(description);
		iteration.setHeight(height);
		iteration.setWidth(width);
		iteration.setLocationX(locationX);
		iteration.setLocationY(locationY);
		iteration.setAvailableEffort(availableEffort);

		if (startDate != null) {
			iteration.setStartDate(startDate);
		}

		if (endDate != null) {
			iteration.setEndDate(endDate);
		}

		this.rootproject.addIteration(iteration);
		this.lookup.put(iteration.getId(), iteration);
		
		this.save();

		return (iteration);
	}

	public synchronized Project createProject(String name)
			throws ForbiddenOperationException {
			ProjectDataObject project = new ProjectDataObject(name);
			this.generator.init(1);
			project.setId(this.generator.getNextID());
			this.rootproject = project;
			this.lookup.clear();
			
			this.save();

			return project;
	}

	public synchronized StoryCard createStoryCard(String name,
			String description, int width, int height, int locationX,
			int locationY, long parentid, float bestCaseEstimate,
			float mostlikelyEstimate, float worstCaseEstimate,
			float actualEffort, String status) throws IndexCardNotFoundException {

		StoryCardDataObject storycard = new StoryCardDataObject();
		storycard.setId(generator.getNextID());
		storycard.setName(name);
		storycard.setHeight(height);
		storycard.setWidth(width);
		storycard.setLocationX(locationX);
		storycard.setLocationY(locationY);
		storycard.setDescription(description);
		storycard.setBestCaseEstimate(bestCaseEstimate);
		storycard.setMostlikelyEstimate(mostlikelyEstimate);
		storycard.setWorstCaseEstimate(worstCaseEstimate);
		storycard.setActualEffort(actualEffort);
		storycard.setStatus(status);

		((IndexCardWithChild) this.findParentCardById(parentid))
				.addStoryCard(storycard);
		this.lookup.put(storycard.getId(), storycard);
		
		this.save();

		return storycard;
	}

	public synchronized IndexCard deleteCard(long id)
	throws IndexCardNotFoundException, ForbiddenOperationException {
		IndexCard indexCard = findCard(id);
		if(indexCard instanceof Backlog)
			return deleteBacklog((Backlog)indexCard);
		if(indexCard instanceof Iteration)
			return deleteIteration((Iteration)indexCard);
		if(indexCard instanceof StoryCard)
			return deleteStoryCard((StoryCard) indexCard);
		else
			throw new IndexCardNotFoundException("id does not refer to either StoryCard, Iteration or Backlog object!");
	}
	
	private IndexCard deleteStoryCard(StoryCard storyCard)
			throws IndexCardNotFoundException {
		IndexCardWithChild parent = this.findParentCardById(storyCard.getParent());
		parent.removeStoryCard(storyCard);
		this.lookup.remove(storyCard.getId());

		this.save();

		return storyCard;
	}
	
	private IndexCard deleteBacklog(Backlog backlog)
			throws ForbiddenOperationException {
		throw new ForbiddenOperationException("You cannot delete the Backlog without deleting the Project!");
	}

	private IndexCard  deleteIteration(Iteration iteration)
			throws IndexCardNotFoundException {
		List<StoryCard> stories =iteration.getStoryCardChildren();
		for (Iterator<StoryCard> iter = stories.iterator(); iter.hasNext();) {
			StoryCard element = iter.next();
			this.lookup.remove(element.getId());
		}
		this.rootproject.removeIteration(iteration);
		this.lookup.remove(iteration.getId());
		
		this.save();

		return iteration;
	}

	public synchronized IndexCard undeleteCard(IndexCard indexCard)
	throws ForbiddenOperationException, IndexCardNotFoundException {
		if(indexCard instanceof Backlog)
			return undeleteBacklog((Backlog)indexCard);
		if(indexCard instanceof Iteration)
			return undeleteIteration((Iteration)indexCard);
		if(indexCard instanceof StoryCard)
			return undeleteStoryCard((StoryCard) indexCard);
		else
			throw new IndexCardNotFoundException("id does not refer to either StoryCard, Iteration or Backlog object!");

	}


	private Backlog undeleteBacklog(Backlog backlog)
			throws ForbiddenOperationException {
		long id = backlog.getId();
		if (!this.lookup.containsKey(id)) {
			if (this.rootproject.getBacklog() == null) {
				this.rootproject.setBacklog(backlog);
				
				this.lookup.put(id, backlog);
				this.save();

				return backlog;
			} else {
				throw new ForbiddenOperationException(
						"Only one Backlog allowed per Project!", rootproject
								.getBacklog().getId());
			}
		} else {
			throw new ForbiddenOperationException("ID " + id
					+ " already in use!", id);
		}
	}

	private Iteration undeleteIteration(Iteration iteration)
			throws ForbiddenOperationException, IndexCardNotFoundException {


		if (!this.lookup.containsKey(iteration.getId())) {
			this.rootproject.addIteration(iteration);
			this.lookup.put(iteration.getId(), iteration);
			List<StoryCard> allStories = iteration.getStoryCardChildren();
			for (Iterator<StoryCard> iter = allStories.iterator(); iter.hasNext();) {
				StoryCard element = iter.next();
				this.lookup.put(element.getId(), element);				
			}
			this.save();

			return iteration;
		} else {
			throw new ForbiddenOperationException("Iteration with ID " + iteration.getId()
					+ " already exists!", iteration.getId());
		}
	}



	private StoryCard undeleteStoryCard(StoryCard storycard) throws IndexCardNotFoundException, ForbiddenOperationException {
		if (!this.lookup.containsKey(storycard.getId())) {
			this.findParentCardById(storycard.getParent()).addStoryCard(
					storycard);
			this.lookup.put(storycard.getId(), storycard);
			
			this.save();

			return storycard;
		} else {
			throw new ForbiddenOperationException("StoryCard with ID " + storycard.getId()
					+ " already exists!", storycard.getId());
		}
	}

	// public synchronized Project undeleteProject(Project project)
	// throws ForbiddenOperationException {
	// long id = project.getId();
	// if (this.lookup.containsKey(id)) {
	// throw new ForbiddenOperationException("ID " + id
	// + " already in use!", id);
	// } else {
	// this.rootproject = project;
	// this.save();
	//
	// return project;
	//}
	//}
	
	public IndexCard updateCard(IndexCard indexCard)
			throws IndexCardNotFoundException {

		if(indexCard instanceof Backlog)
			return updateBacklog((Backlog)indexCard);
		if(indexCard instanceof Iteration)
			return updateIteration((Iteration)indexCard);
		if(indexCard instanceof StoryCard)
			return updateStoryCard((StoryCard) indexCard);
		else
			throw new IndexCardNotFoundException("id does not refer to either StoryCard, Iteration or Backlog object!");
	
		
	}
	
	
	private Backlog updateBacklog(Backlog backlog) throws IndexCardNotFoundException {
		
		// replace old card
		rootproject.setBacklog(backlog);
		this.lookup.put(backlog.getId(), backlog);
		
		this.save();

		return this.rootproject.getBacklog();
	}

	private Iteration updateIteration(Iteration iteration) throws IndexCardNotFoundException {
		
		Iteration oldCard = (Iteration)findCard(iteration.getId());
		
//		 remove the old card
		rootproject.removeIteration(iteration);
		this.lookup.remove(oldCard.getId());
		
		// add new card
		rootproject.addIteration(iteration);
		this.lookup.put(iteration.getId(), iteration);
		
		this.save();

		return iteration;
	}

	
	private StoryCard updateStoryCard(StoryCard sc)
			throws IndexCardNotFoundException {
		
		IndexCardWithChild parent = findParentCardById(sc.getParent());
		
		// remove the old card
		StoryCard oldCard = (StoryCard) findCard(sc.getId());
		parent.removeStoryCard(oldCard);
		this.lookup.remove(oldCard.getId());

		// add new card
		parent.addStoryCard(sc);
		this.lookup.put(sc.getId(), sc);
		this.save();

		return sc;
	}

	public synchronized StoryCard moveStoryCardToNewParent(long id,
			long oldparentid, long newparentid, int locationX, int locationY)
			throws IndexCardNotFoundException  {
		IndexCardWithChild oldparent = this.findParentCardById(oldparentid);
		IndexCardWithChild newparent = this.findParentCardById(newparentid);
		StoryCard storycard = (StoryCard)this.findCard(id);

		storycard.setLocationX(locationX);
		storycard.setLocationY(locationY);

		oldparent.removeStoryCard(storycard);
		newparent.addStoryCard(storycard);

		this.save();

		return storycard;
	}



	public synchronized Project updateProjectName(long id, String name)
			throws  IndexCardNotFoundException {
		return null;
	}

	/***************************************************************************
	 * GETTERS AND SETTERS *
	 **************************************************************************/

	public synchronized File getFile() {
		return file;
	}

	public synchronized void setFile(File file) {
		this.file = file;
	}

//	public synchronized ProjectDataObject getProject() {
//		return this.rootproject;
//	}
//
//	public synchronized void setProject(ProjectDataObject prj) {
//		this.rootproject = prj;
//	}



	public void writeToFile(String fileName, String fileContent) {
		BufferedWriter out;
		
		try {
				out = new BufferedWriter(new FileWriter(projectDirectory.getName()
					+ "\\" + fileName));

			out.write(fileContent);

			out.close();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	public String[] readFromFile(String localPath) {
		String[] fileStr = new String[2];
		fileStr[0] = "";
		fileStr[1] = "";

		
		File file = new File(projectDirectory.getName() + "\\" + localPath);
		try {
			BufferedReader in = new BufferedReader(new FileReader(file));
			String str = "";
			while ((str = in.readLine()) != null) {
				fileStr[1] = fileStr[1] + str;
			}
			in.close();
		} catch (IOException e) {
		}
		System.out.println(fileStr);
		return fileStr;
	}

//	public void upload(String localPath)  {
//		 writeToFile( readFromFile(localPath));
//
//	}

	public File getProjectDirectory() {

		return projectDirectory;
	}

	public String[][] getIterationNames(String projectName)
			{
		List<Iteration> lst = rootproject.getIterationChildren();
		String[][] iterations = new String[lst.size()][2];
		int j = 0;
		for (Iteration i : lst) {
			iterations[j][0] = i.getName();
			iterations[j][1] = Long.toString(i.getId());
			j++;
			
		}
		return iterations;
	}

	public Project getProject() {
		return rootproject;
	}

}// end class




See more files for this project here

MASE: Agile Software Engineering

The MASE project investigates methods to support the coordination and executable acceptance testing of software projects. Keywords: Agile methods, distributed teams, Extreme Programming. See http://ebe.cpsc.ucalgary.ca/ebe for more information.

Project homepage: http://sourceforge.net/projects/mase
Programming language(s): Java,XML
License: other

  AsynchronousLocalPersister.java
  DummyDistributedUI.java
  PersisterToXML.java