Wiki source code of IdeaVoteService
Last modified by Andrey Che on 2018/12/10 14:09
Hide last authors
| |
1.1 | 1 | {{groovy}} |
| 2 | import org.xwiki.velocity.tools.JSONTool; | ||
| 3 | import org.apache.commons.lang3.StringUtils; | ||
| 4 | |||
| 5 | def lock = "lock" | ||
| 6 | def result = new HashMap(); | ||
| 7 | try { | ||
| 8 | if(request.page && request.action) { | ||
| 9 | synchronized(lock) { | ||
| 10 | def mydoc = xwiki.getDocument(request.page); | ||
| 11 | if(!mydoc.isNew() && null != mydoc.getObject("Ideas.IdeasClass") && xcontext.user != "XWiki.XWikiGuest") { | ||
| 12 | def doRemove = false; | ||
| 13 | def doRemoveagainst = false; | ||
| 14 | // Action : Add a supporter (vote) | ||
| 15 | if(request.action == "addVote") { | ||
| 16 | mydoc.use("Ideas.IdeasClass"); | ||
| 17 | def userList = mydoc.getValue("supporters"); | ||
| 18 | def userListSup = mydoc.getValue("against"); | ||
| 19 | def nbvotes = mydoc.getValue("nbvotes"); | ||
| 20 | def nbopp = mydoc.getValue("nbagainst"); | ||
| 21 | if(StringUtils.contains(userList, xwiki.checkAuth().toString())) { | ||
| 22 | doRemove = true; | ||
| 23 | } | ||
| 24 | else { | ||
| 25 | // Add user in userList | ||
| 26 | if (StringUtils.contains(userListSup, xwiki.checkAuth().toString())){ | ||
| 27 | doRemoveagainst = true; | ||
| 28 | result.put("remove", true); | ||
| 29 | result.put("nbopp", nbopp - 1); | ||
| 30 | } | ||
| 31 | userList = userList + "," + xwiki.checkAuth(); | ||
| 32 | mydoc.set("supporters", userList); | ||
| 33 | // Increase vote by one | ||
| 34 | if(!nbvotes) { | ||
| 35 | nbvotes = 1; | ||
| 36 | } else { | ||
| 37 | nbvotes = nbvotes + 1; | ||
| 38 | } | ||
| 39 | mydoc.set("nbvotes", nbvotes); | ||
| 40 | // Save document | ||
| 41 | mydoc.saveWithProgrammingRights(); | ||
| 42 | result.put("result", true); | ||
| 43 | result.put("nbvotes", nbvotes); | ||
| 44 | } | ||
| 45 | } | ||
| 46 | else if(request.action == "addVoteOpp") { | ||
| 47 | mydoc.use("Ideas.IdeasClass"); | ||
| 48 | def userList = mydoc.getValue("against"); | ||
| 49 | def userListSup = mydoc.getValue("supporters"); | ||
| 50 | def nbvotes = mydoc.getValue("nbagainst"); | ||
| 51 | def nbopp = mydoc.getValue("nbvotes"); | ||
| 52 | if(StringUtils.contains(userList, xwiki.checkAuth().toString())) { | ||
| 53 | doRemoveagainst = true; | ||
| 54 | } | ||
| 55 | else { | ||
| 56 | // Add user in userList | ||
| 57 | if (StringUtils.contains(userListSup, xwiki.checkAuth().toString())){ | ||
| 58 | doRemove = true; | ||
| 59 | result.put("remove", true); | ||
| 60 | result.put("nbopp", nbopp - 1); | ||
| 61 | } | ||
| 62 | if (userList == null){ | ||
| 63 | userList = "," + xwiki.checkAuth(); | ||
| 64 | } | ||
| 65 | else{ | ||
| 66 | userList = userList + "," + xwiki.checkAuth(); | ||
| 67 | } | ||
| 68 | mydoc.set("against", userList); | ||
| 69 | // Increase vote by one | ||
| 70 | if(!nbvotes) { | ||
| 71 | nbvotes = 1; | ||
| 72 | } else { | ||
| 73 | nbvotes = nbvotes + 1; | ||
| 74 | } | ||
| 75 | mydoc.set("nbagainst", nbvotes); | ||
| 76 | // Save document | ||
| 77 | mydoc.saveWithProgrammingRights(); | ||
| 78 | result.put("result", true); | ||
| 79 | result.put("nbagainst", nbvotes); | ||
| 80 | } | ||
| 81 | } | ||
| 82 | // Action : Remove a supporter (vote) | ||
| 83 | if(request.action == "removeVote" || doRemove){ | ||
| 84 | // Remove user from user list | ||
| 85 | def userList = mydoc.getValue("supporters"); | ||
| 86 | def nbvotes = mydoc.getValue("nbvotes"); | ||
| 87 | def theUser = xwiki.checkAuth(); | ||
| 88 | def newUserList = StringUtils.replaceOnce(userList, ","+theUser.toString(), ""); | ||
| 89 | newUserList = StringUtils.replaceOnce(newUserList , theUser.toString()+",", ""); | ||
| 90 | newUserList = StringUtils.replaceOnce(newUserList , theUser.toString(), ""); | ||
| 91 | mydoc.set("supporters", newUserList); | ||
| 92 | // Decrease vote by one | ||
| 93 | nbvotes = nbvotes - 1; | ||
| 94 | mydoc.set("nbvotes", nbvotes); | ||
| 95 | // Save document | ||
| 96 | mydoc.saveWithProgrammingRights(); | ||
| 97 | result.put("result", true); | ||
| 98 | result.put("nbvotes", nbvotes); | ||
| 99 | } | ||
| 100 | else if(doRemoveagainst){ | ||
| 101 | // Remove user from user list | ||
| 102 | def userList = mydoc.getValue("against"); | ||
| 103 | def nbvotes = mydoc.getValue("nbagainst"); | ||
| 104 | def theUser = xwiki.checkAuth(); | ||
| 105 | def newUserList = StringUtils.replaceOnce(userList, ","+theUser.toString(), ""); | ||
| 106 | newUserList = StringUtils.replaceOnce(newUserList , theUser.toString()+",", ""); | ||
| 107 | newUserList = StringUtils.replaceOnce(newUserList , theUser.toString(), ""); | ||
| 108 | mydoc.set("against", newUserList); | ||
| 109 | // Decrease vote by one | ||
| 110 | nbvotes = nbvotes - 1; | ||
| 111 | mydoc.set("nbagainst", nbvotes); | ||
| 112 | // Save document | ||
| 113 | mydoc.saveWithProgrammingRights(); | ||
| 114 | result.put("result", true); | ||
| 115 | result.put("nbagainst", nbvotes); | ||
| 116 | } | ||
| 117 | } else if (xcontext.user == "XWiki.XWikiGuest") { | ||
| 118 | result.put("result", false); | ||
| 119 | result.put("msg", services.localization.render('ideas.message.guess')); | ||
| 120 | } else { | ||
| 121 | result.put("result", false); | ||
| 122 | result.put("msg", services.localization.render('ideas.message.error')); | ||
| 123 | } | ||
| 124 | } | ||
| 125 | } else { | ||
| 126 | result.put("result", false); | ||
| 127 | result.put("msg", "Dev error : Missing parameters") | ||
| 128 | } | ||
| 129 | |||
| 130 | // Format JSON response | ||
| 131 | response.setContentType("application/json"); | ||
| 132 | def json = new JSONTool(); | ||
| 133 | println json.serialize(result); | ||
| 134 | } | ||
| 135 | catch (Exception exception) { | ||
| 136 | println "Message : " + exception.getMessage(); | ||
| 137 | println "StackTrace : " + exception.printStackTrace(); | ||
| 138 | } | ||
| 139 | {{/groovy}} |