KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > net > jforum > entities > PollChanges


1 package net.jforum.entities;
2
3 import java.util.ArrayList JavaDoc;
4 import java.util.Iterator JavaDoc;
5 import java.util.List JavaDoc;
6
7 import net.jforum.view.forum.common.PostCommon;
8
9 /**
10  * An helper class that holds changes made to the pool.
11  *
12  * @author Rafael Steil
13  * @version $Id: PollChanges.java,v 1.2 2005/12/27 22:57:32 rafaelsteil Exp $
14  */

15 public class PollChanges {
16     private List JavaDoc deletedOptions = new ArrayList JavaDoc();
17     private List JavaDoc newOptions = new ArrayList JavaDoc();
18     private List JavaDoc changedOptions = new ArrayList JavaDoc();
19     
20     private boolean hasChanges;
21     
22     private Poll first;
23     private Poll second;
24     
25     /**
26      * @param first The "complete", most recent poll version. Usually the one
27      * that's in the database.
28      * @param second The poll to compare with. It usually will be a poll filled
29      * by {@link PostCommon#fillPostFromRequest()}, so matches will be done againts the
30      * existing poll and the data comming from the server.
31      */

32     public PollChanges(Poll first, Poll second) {
33         this.first = first;
34         this.second = second;
35     }
36     
37     public void addChangedOption(PollOption option) {
38         this.changedOptions.add(option);
39         this.hasChanges = true;
40     }
41     
42     public List JavaDoc getChangedOptions() {
43         return this.changedOptions;
44     }
45     
46     public void addDeletedOption(PollOption option) {
47         this.deletedOptions.add(option);
48         this.hasChanges = true;
49     }
50
51     public List JavaDoc getDeletedOptions() {
52         return this.deletedOptions;
53     }
54     
55     public void addNewOption(PollOption option) {
56         this.newOptions.add(option);
57         this.hasChanges = true;
58     }
59
60     public List JavaDoc getNewOptions() {
61         return this.newOptions;
62     }
63     
64     public boolean hasChanges() {
65         this.searchForChanges();
66         return this.hasChanges;
67     }
68     
69     private void searchForChanges() {
70         if (first == null || second == null) {
71             return;
72         }
73         
74         boolean isSame = first.getLabel().equals(second.getLabel());
75         isSame &= first.getLength() == second.getLength();
76         
77         this.hasChanges = !isSame;
78         
79         List JavaDoc firstOptions = first.getOptions();
80         List JavaDoc secondOptions = second.getOptions();
81         
82         int firstSize = firstOptions.size();
83         int secondSize = secondOptions.size();
84         int maxCount = Math.min(firstSize, secondSize);
85         
86         // Search for changes in existing options
87
for (Iterator JavaDoc iter = firstOptions.iterator(); iter.hasNext(); ) {
88             PollOption option = (PollOption)iter.next();
89             PollOption changed = this.findOptionById(option.getId(), secondOptions);
90             
91             if (changed != null && !option.getText().equals(changed.getText())) {
92                 this.addChangedOption(changed);
93             }
94             else if (changed == null) {
95                 this.addDeletedOption(option);
96             }
97         }
98
99         // Check if the incoming poll added options
100
for (Iterator JavaDoc iter = secondOptions.iterator(); iter.hasNext(); ) {
101             PollOption option = (PollOption)iter.next();
102             
103             if (this.findOptionById(option.getId(), firstOptions) == null) {
104                 this.addNewOption(option);
105             }
106         }
107     }
108     
109     private PollOption findOptionById(int id, List JavaDoc options) {
110         for (Iterator JavaDoc iter = options.iterator(); iter.hasNext(); ) {
111             PollOption o = (PollOption)iter.next();
112             
113             if (o.getId() == id) {
114                 return o;
115             }
116         }
117         
118         return null;
119     }
120 }
Popular Tags