001package votorola.s.gwt.scene.feed.ss; // Copyright 2011-2012, Michael Allan. Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Votorola Software"), to deal in the Votorola Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicence, and/or sell copies of the Votorola Software, and to permit persons to whom the Votorola Software is furnished to do so, subject to the following conditions: The preceding copyright notice and this permission notice shall be included in all copies or substantial portions of the Votorola Software. THE VOTOROLA SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE VOTOROLA SOFTWARE OR THE USE OR OTHER DEALINGS IN THE VOTOROLA SOFTWARE. 002 003import com.google.gson.stream.JsonWriter; 004import java.io.IOException; 005import java.sql.SQLException; 006import javax.script.ScriptException; 007import votorola.a.count.*; 008import votorola.s.gwt.scene.feed.*; 009 010 011/** A mutable and serializeable poll. 012 */ 013public final class PollJig implements Poll, SerialJig 014{ 015 016 // adding fields? ensure they are cleared in serialize() 017 018 019 020 /** Fetches the currently reported count and caches it for reuse. 021 * 022 * @throws NoSuchItem if no count is currently reported. 023 * @throws NullPointerException if setName() was not called. 024 */ 025 public Count count( final PollService.VoteServerScope.Run runPoll ) 026 throws IOException, NoSuchItem, ScriptException, SQLException 027 { 028 if( count == null || !count.pollName().equals( name )) 029 { 030 count = poll(runPoll).countToReportT(); 031 } 032 if( count == null ) throw new NoSuchItem( "count" ); 033 034 return count; 035 } 036 037 038 private Count count; 039 040 041 042 /** Fetches the poll service and caches it for reuse. 043 * 044 * @throws NullPointerException if setName() was not called. 045 */ 046 public PollService poll( final PollService.VoteServerScope.Run runPoll ) 047 throws IOException, ScriptException, SQLException 048 { 049 if( poll == null || !poll.name().equals( name )) 050 { 051 if( name == null ) throw new NullPointerException(); // fail fast 052 053 poll = runPoll.ensurePoll( name ); 054 } 055 return poll; 056 } 057 058 059 private PollService poll; 060 061 062 063 // - P o l l -------------------------------------------------------------------------- 064 065 066 public String displayTitle() { return displayTitle; } 067 068 069 private String displayTitle; 070 071 072 private void displayTitle( final JsonWriter out ) throws IOException 073 { 074 if( displayTitle == null ) return; 075 076 out.name( "displayTitle" ).value( displayTitle ); 077 displayTitle = null; 078 } 079 080 081 /** Sets the display title. 082 */ 083 public void setDisplayTitle( String _displayTitle ) { displayTitle = _displayTitle; } 084 085 086 087 public String issueType() { return issueType; } 088 089 090 private String issueType; 091 092 093 private void issueType( final JsonWriter out ) throws IOException 094 { 095 if( issueType == null ) throw new NullPointerException(); // meet contract of Poll 096 097 out.name( "issueType" ).value( issueType ); 098 issueType = null; 099 } 100 101 102 /** Sets the issue type. 103 */ 104 public void setIssueType( String _issueType ) { issueType = _issueType; } 105 106 107 108 public String name() { return name; } 109 110 111 private String name; 112 113 114 private void name( final JsonWriter out ) throws IOException 115 { 116 if( name == null ) throw new NullPointerException(); // meet contract of Poll 117 118 out.name( "name" ).value( name ); 119 name = null; 120 } 121 122 123 /** Sets the name. 124 * 125 * @throws IllegalStateException if the name was already set. 126 */ 127 public void setName( String _name ) 128 { 129 if( name != null ) throw new IllegalStateException( "name was already set" ); 130 131 name = _name; 132 } 133 134 135 136 // - S e r i a l - J i g -------------------------------------------------------------- 137 138 139 public void serialize( final JsonWriter out ) throws IOException 140 { 141 out.beginObject(); 142 name( out ); 143 displayTitle( out ); 144 issueType( out ); 145 out.endObject(); 146 } 147 148 149}