PageRenderTime 10ms CodeModel.GetById 1ms app.highlight 6ms RepoModel.GetById 1ms app.codeStats 0ms

/grails-app/services/icescrum/plugin/planning/poker/PlanningPokerService.groovy

http://github.com/icescrum/iceScrum-plugin-planning-poker
Groovy | 156 lines | 109 code | 20 blank | 27 comment | 11 complexity | a19015ba8eb7f0f95dac89889e89e900 MD5 | raw file
  1/*
  2 * Copyright (c) 2011 BE ISI iSPlugins Université Paul Sabatier.
  3 *
  4 * This file is part of Planning Poker plugin for icescrum.
  5 *
  6 * This icescrum plugin is free software: you can redistribute it and/or modify
  7 * it under the terms of the GNU Affero General Public License as published by
  8 * the Free Software Foundation, either version 3 of the License.
  9 *
 10 * This icescrum plugin is distributed in the hope that it will be useful,
 11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 13 * GNU General Public License for more details.
 14 *
 15 * You should have received a copy of the GNU Affero General Public License
 16 * along with this icescrum plugin.  If not, see <http://www.gnu.org/licenses/>.
 17 *
 18 * Authors:	Marc-Antoine BEAUVAIS (marcantoine.beauvais@gmail.com)
 19 *		Gabriel GIL (contact.gabrielgil@gmail.com)
 20 *		Julien GOUDEAUX (julien.goudeaux@orange.fr)
 21 *		Jihane KHALIL (khaliljihane@gmail.com)
 22 *		Paul LABONNE (paul.labonne@gmail.com)
 23 *		Nicolas NOULLET (nicolas.noullet@gmail.com)
 24 *		Jérémy SIMONKLEIN (jeremy.simonklein@gmail.com)
 25 *
 26 *
 27*/
 28package icescrum.plugin.planning.poker
 29
 30import org.icescrum.core.domain.Product
 31import org.icescrum.core.domain.User
 32import org.icescrum.core.domain.Story
 33
 34class PlanningPokerService {
 35
 36    def VALUE_BEFORE_VOTE = -10;
 37    def VALUE_UNVOTED = -1;
 38
 39    def springSecurityService
 40    def productBacklogService
 41
 42    def createSession(productid) {
 43        PlanningPokerSession session = new PlanningPokerSession(product:Product.get(productid))
 44        return session.save(flush:true)
 45    }
 46
 47    def getSession(productid) {
 48        return PlanningPokerSession.findByProduct(Product.get(productid))
 49    }
 50
 51    def deleteSession(productid) {
 52        getSession(productid)?.delete()
 53    }
 54
 55    def setStory(productid, storyid) {
 56        PlanningPokerSession currentSession = getSession(productid)
 57        currentSession.story = Story.get(storyid)
 58        return currentSession.save(flush:true)
 59    }
 60
 61    def getStory(productid) {
 62        return getSession(productid).story
 63    }
 64
 65    def createVote(productid, userid) {
 66        User currentUser = User.get(userid)
 67        PlanningPokerSession currentSession = getSession(productid)
 68        PlanningPokerVote vote = new PlanningPokerVote(user:currentUser, session:currentSession)
 69        return vote.save(flush:true)
 70    }
 71
 72    def initVotes(productid){
 73        getVotes(productid).each{
 74            it.voteValue = VALUE_BEFORE_VOTE
 75            it.save(flush:true)
 76        }
 77    }
 78
 79    def getVotes(productid) {
 80        return getSession(productid)?.votes
 81    }
 82
 83    def setUnvoted(productid, userid) {
 84        setVote(productid, userid, VALUE_UNVOTED);
 85    }
 86
 87    def setVote(productid, userid, value) {
 88        User currentUser = User.get(userid)
 89        def vote = null
 90        getVotes(productid).each{
 91            if(it.user == currentUser)
 92                vote = it;
 93        }
 94        if(!vote)
 95            return false
 96        vote.voteValue = value
 97        return vote.save(flush:true)
 98    }
 99
100    boolean hasVoted(productid, userid) {
101        def currentUser = User.get(userid)
102        def hasVoted = false;
103        getVotes(productid).each{
104            if(it.user == currentUser)
105                if (it.voteValue != VALUE_BEFORE_VOTE)
106                    hasVoted = true;
107        }
108        return hasVoted
109    }
110
111    def isVoteTerminated (productid) {
112        def terminated = true
113        getVotes(productid).each{
114            if(it.voteValue == VALUE_BEFORE_VOTE)
115                terminated = false
116        }
117        return terminated
118    }
119
120    def printVotes (commentaire) {
121        println "Votes - " + commentaire
122        PlanningPokerVote.list().each{
123            println it.user.username + ": " + it.voteValue
124        }
125    }
126
127    def getResult (productid) {
128        def votes = getVotes(productid)
129        int totalVotes = 0
130        int nbVotes = 0
131        votes.each{
132            if(it.voteValue >= 0) {
133                totalVotes += it.voteValue
134                nbVotes ++
135            }
136        }
137        String result = "?"
138        if(nbVotes > 0){
139            int resulttmp = totalVotes/nbVotes
140            result = String.valueOf(resulttmp)
141        }
142        return result
143    }
144
145    def acceptResult (productid) {
146      def story = getStory(productid)
147      def result = getResult(productid)
148      productBacklogService.estimateStory(story,result);
149    }
150
151    def acceptEstimate (productid, presult) {
152      def story = getStory(productid)
153      def result = presult
154      productBacklogService.estimateStory(story,result)
155    }
156}