package 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. import com.google.gson.stream.JsonWriter; import java.io.IOException; import java.sql.SQLException; import javax.script.ScriptException; import votorola.a.count.*; import votorola.s.gwt.scene.feed.*; /** A mutable and serializeable poll. */ public final class PollJig implements Poll, SerialJig { // adding fields? ensure they are cleared in serialize() /** Fetches the currently reported count and caches it for reuse. * * @throws NoSuchItem if no count is currently reported. * @throws NullPointerException if setName() was not called. */ public Count count( final PollService.VoteServerScope.Run runPoll ) throws IOException, NoSuchItem, ScriptException, SQLException { if( count == null || !count.pollName().equals( name )) { count = poll(runPoll).countToReportT(); } if( count == null ) throw new NoSuchItem( "count" ); return count; } private Count count; /** Fetches the poll service and caches it for reuse. * * @throws NullPointerException if setName() was not called. */ public PollService poll( final PollService.VoteServerScope.Run runPoll ) throws IOException, ScriptException, SQLException { if( poll == null || !poll.name().equals( name )) { if( name == null ) throw new NullPointerException(); // fail fast poll = runPoll.ensurePoll( name ); } return poll; } private PollService poll; // - P o l l -------------------------------------------------------------------------- public String displayTitle() { return displayTitle; } private String displayTitle; private void displayTitle( final JsonWriter out ) throws IOException { if( displayTitle == null ) return; out.name( "displayTitle" ).value( displayTitle ); displayTitle = null; } /** Sets the display title. */ public void setDisplayTitle( String _displayTitle ) { displayTitle = _displayTitle; } public String issueType() { return issueType; } private String issueType; private void issueType( final JsonWriter out ) throws IOException { if( issueType == null ) throw new NullPointerException(); // meet contract of Poll out.name( "issueType" ).value( issueType ); issueType = null; } /** Sets the issue type. */ public void setIssueType( String _issueType ) { issueType = _issueType; } public String name() { return name; } private String name; private void name( final JsonWriter out ) throws IOException { if( name == null ) throw new NullPointerException(); // meet contract of Poll out.name( "name" ).value( name ); name = null; } /** Sets the name. * * @throws IllegalStateException if the name was already set. */ public void setName( String _name ) { if( name != null ) throw new IllegalStateException( "name was already set" ); name = _name; } // - S e r i a l - J i g -------------------------------------------------------------- public void serialize( final JsonWriter out ) throws IOException { out.beginObject(); name( out ); displayTitle( out ); issueType( out ); out.endObject(); } }