001: package net.jforum.entities;
002:
003: import java.util.ArrayList;
004: import java.util.Iterator;
005: import java.util.List;
006:
007: import net.jforum.view.forum.common.PostCommon;
008:
009: /**
010: * An helper class that holds changes made to the pool.
011: *
012: * @author Rafael Steil
013: * @version $Id: PollChanges.java,v 1.4 2007/04/24 02:19:46 rafaelsteil Exp $
014: */
015: public class PollChanges {
016: private List deletedOptions = new ArrayList();
017: private List newOptions = new ArrayList();
018: private List changedOptions = new ArrayList();
019:
020: private boolean hasChanges;
021:
022: private Poll first;
023: private Poll second;
024:
025: /**
026: * @param first The "complete", most recent poll version. Usually the one
027: * that's in the database.
028: * @param second The poll to compare with. It usually will be a poll filled
029: * by {@link PostCommon#fillPostFromRequest()}, so matches will be done againts the
030: * existing poll and the data comming from the server.
031: */
032: public PollChanges(Poll first, Poll second) {
033: this .first = first;
034: this .second = second;
035: }
036:
037: public void addChangedOption(PollOption option) {
038: this .changedOptions.add(option);
039: this .hasChanges = true;
040: }
041:
042: public List getChangedOptions() {
043: return this .changedOptions;
044: }
045:
046: public void addDeletedOption(PollOption option) {
047: this .deletedOptions.add(option);
048: this .hasChanges = true;
049: }
050:
051: public List getDeletedOptions() {
052: return this .deletedOptions;
053: }
054:
055: public void addNewOption(PollOption option) {
056: this .newOptions.add(option);
057: this .hasChanges = true;
058: }
059:
060: public List getNewOptions() {
061: return this .newOptions;
062: }
063:
064: public boolean hasChanges() {
065: this .searchForChanges();
066: return this .hasChanges;
067: }
068:
069: private void searchForChanges() {
070: if (first == null || second == null) {
071: return;
072: }
073:
074: boolean isSame = first.getLabel().equals(second.getLabel());
075: isSame &= first.getLength() == second.getLength();
076:
077: this .hasChanges = !isSame;
078:
079: List firstOptions = first.getOptions();
080: List secondOptions = second.getOptions();
081:
082: // Search for changes in existing options
083: for (Iterator iter = firstOptions.iterator(); iter.hasNext();) {
084: PollOption option = (PollOption) iter.next();
085: PollOption changed = this .findOptionById(option.getId(),
086: secondOptions);
087:
088: if (changed != null
089: && !option.getText().equals(changed.getText())) {
090: this .addChangedOption(changed);
091: } else if (changed == null) {
092: this .addDeletedOption(option);
093: }
094: }
095:
096: // Check if the incoming poll added options
097: for (Iterator iter = secondOptions.iterator(); iter.hasNext();) {
098: PollOption option = (PollOption) iter.next();
099:
100: if (this .findOptionById(option.getId(), firstOptions) == null) {
101: this .addNewOption(option);
102: }
103: }
104: }
105:
106: private PollOption findOptionById(int id, List options) {
107: for (Iterator iter = options.iterator(); iter.hasNext();) {
108: PollOption o = (PollOption) iter.next();
109:
110: if (o.getId() == id) {
111: return o;
112: }
113: }
114:
115: return null;
116: }
117: }
|