Add yaml support for election
Instead of a simple txt file, we now use a yaml which is easier to detect if a user already made a vote or not.
This commit is contained in:
parent
b198489c98
commit
c1fa0bdd0c
@ -8,12 +8,18 @@ import org.kohsuke.stapler.interceptor.RequirePOST;
|
||||
|
||||
import javax.naming.NamingException;
|
||||
import java.io.FileWriter;
|
||||
import java.io.FileReader;
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
import java.io.PrintWriter;
|
||||
import java.text.SimpleDateFormat;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Date;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
import com.esotericsoftware.yamlbeans.YamlReader;
|
||||
import com.esotericsoftware.yamlbeans.YamlWriter;
|
||||
|
||||
/**
|
||||
* @author <a href="mailto:nicolas.deloof@gmail.com">Nicolas De Loof</a>
|
||||
@ -32,7 +38,9 @@ public class BoardElection {
|
||||
open = format.parse(params.electionOpen());
|
||||
close = format.parse(params.electionClose());
|
||||
candidates = params.electionCandidates().split(",");
|
||||
log = params.electionLogfile();
|
||||
log = params.electionLogDir() + "/" + params.electionClose().replaceAll("/","") + ".yaml";
|
||||
File f = new File(log);
|
||||
f.createNewFile();
|
||||
}
|
||||
|
||||
@RequirePOST
|
||||
@ -52,11 +60,23 @@ public class BoardElection {
|
||||
for (String id : vote.split(",")) {
|
||||
selected.add(candidates[Integer.parseInt(id)]);
|
||||
}
|
||||
String log = user.userId + ":" + StringUtils.join(selected, ",");
|
||||
try (PrintWriter w = new PrintWriter(new FileWriter(log, true))) {
|
||||
w.println(log);
|
||||
// TODO build a nicer yaml structure document
|
||||
YamlReader reader = new YamlReader(new FileReader(log));
|
||||
Object object = new Object();
|
||||
object = reader.read();
|
||||
if (null == object){
|
||||
String init_content = "election_close: " + close;
|
||||
YamlReader init = new YamlReader(init_content);
|
||||
object = init.read();
|
||||
}
|
||||
|
||||
Map map = (Map)object;
|
||||
map.put(user.userId,StringUtils.join(selected, ","));
|
||||
|
||||
YamlWriter writer = new YamlWriter(new FileWriter(log, false));
|
||||
writer.write(map);
|
||||
writer.close();
|
||||
|
||||
// TODO store
|
||||
return new HttpRedirect("done");
|
||||
}
|
||||
|
@ -37,7 +37,7 @@ public interface Parameters {
|
||||
|
||||
String electionCandidates();
|
||||
|
||||
String electionLogfile();
|
||||
String electionLogDir();
|
||||
|
||||
String electionOpen();
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user