Changed the simpleRegistry file
Edited the GetPersonIDDetails method , and refactored the basic methods of upload data to BulletinBoardServervote-registry
parent
7734ba8c91
commit
109135ae1b
|
@ -18,9 +18,8 @@ import java.text.ParseException;
|
|||
import java.util.*;
|
||||
|
||||
/**
|
||||
* Created by Vladimir Eliezer Tokarev on 1/8/2016.
|
||||
* Gives the ability to manage voters information
|
||||
* This object is synchronous (which meaning its method blocking)
|
||||
* Created by Vladimir Eliezer Tokarev on 1/8/2016.
|
||||
* Gives the ability to manage voters information
|
||||
*/
|
||||
public class SimpleRegistry {
|
||||
|
||||
|
@ -28,123 +27,27 @@ public class SimpleRegistry {
|
|||
|
||||
protected SimpleBulletinBoardClient communicator;
|
||||
|
||||
/**
|
||||
* @param signatory implements the DigitalSignature interface
|
||||
* @param communicator implements the BulletinBoardClient interface
|
||||
*/
|
||||
public SimpleRegistry(Encryption signatory, SimpleBulletinBoardClient communicator){
|
||||
public SimpleRegistry(Encryption signatory, SimpleBulletinBoardClient communicator) {
|
||||
this.signatory = signatory;
|
||||
this.communicator = communicator;
|
||||
}
|
||||
|
||||
/**
|
||||
* Adds new voter to the bulletin-board
|
||||
* @param voterID
|
||||
* @param personalData for example residence location
|
||||
* @throws throws CommunicationException
|
||||
* @return void
|
||||
*/
|
||||
public void AddVoter(String voterID, String personalData) throws CommunicationException, IOException {
|
||||
Tag.Builder idTag = Tag.newBuilder();
|
||||
idTag.setContent(Tags.ID_TAG + " " + voterID);
|
||||
private Tag.Builder[] GetTagsArrayInLength(int length) {
|
||||
Tag.Builder[] tags = new Tag.Builder[length];
|
||||
|
||||
Tag.Builder voterEntryTag = Tag.newBuilder();
|
||||
voterEntryTag.setContent(Tags.VOTER_ENTRY_TAG.toString());
|
||||
|
||||
Tag.Builder timestampTag = Tag.newBuilder();
|
||||
timestampTag.setContent(Tags.ACTION_TIMESTAMP_TAG + " "
|
||||
+ AccurateTimestamp.GetCurrentTimestampString());
|
||||
|
||||
BasicMessage.Builder basicMessage =
|
||||
BasicMessage.newBuilder().addTag(idTag).addTag(voterEntryTag).addTag(timestampTag);
|
||||
basicMessage.setData(ByteString.copyFrom(personalData.getBytes()));
|
||||
|
||||
communicator.postMessage(CreateBulletinBoardMessage(basicMessage.build()));
|
||||
for (int i = 0; i < length; i++) {
|
||||
tags[i] = Tag.newBuilder();
|
||||
}
|
||||
return tags;
|
||||
}
|
||||
|
||||
/**
|
||||
* Adding given voter to given group
|
||||
* @param voterID
|
||||
* @param groupID
|
||||
* @throws CommunicationException
|
||||
* @return true if the adding action succeeded else return false
|
||||
*/
|
||||
public void AddToGroup(String voterID, String groupID) throws CommunicationException, IOException {
|
||||
Tag.Builder idTag = Tag.newBuilder();
|
||||
idTag.setContent(Tags.ID_TAG + " " + voterID);
|
||||
|
||||
Tag.Builder groupIDTag = Tag.newBuilder();
|
||||
groupIDTag.setContent(Tags.GROUP_ID_TAG + " " + groupID);
|
||||
|
||||
Tag.Builder actionTag = Tag.newBuilder();
|
||||
actionTag.setContent(Tags.GROUP_ACTION_TAG + " " + Tags.ADD_TO_GROUP_TAG);
|
||||
|
||||
Tag.Builder timestampTag = Tag.newBuilder();
|
||||
timestampTag.setContent(Tags.ACTION_TIMESTAMP_TAG + " "
|
||||
+AccurateTimestamp.GetCurrentTimestampString());
|
||||
|
||||
BasicMessage.Builder basicMessage =
|
||||
BasicMessage.newBuilder().addTag(idTag).addTag(groupIDTag).addTag(actionTag).addTag(timestampTag);
|
||||
|
||||
communicator.postMessage(CreateBulletinBoardMessage(basicMessage.build()));
|
||||
}
|
||||
|
||||
/**
|
||||
* Removes given voter from given group
|
||||
* @param voterID
|
||||
* @param groupID
|
||||
* @return true if the removing action succeeded else return false
|
||||
* @throws CommunicationException
|
||||
*/
|
||||
public void RemoveFromGroup(String voterID, String groupID) throws CommunicationException, IOException {
|
||||
Tag.Builder idTag = Tag.newBuilder();
|
||||
idTag.setContent(Tags.ID_TAG + " " + voterID);
|
||||
|
||||
Tag.Builder groupIDTag = Tag.newBuilder();
|
||||
groupIDTag.setContent(Tags.GROUP_ID_TAG + " " + groupID);
|
||||
|
||||
Tag.Builder actionTag = Tag.newBuilder();
|
||||
actionTag.setContent(Tags.GROUP_ACTION_TAG + " " + Tags.REMOVE_FROM_GROUP_TAG);
|
||||
|
||||
Tag.Builder timestampTag = Tag.newBuilder();
|
||||
timestampTag.setContent(Tags.ACTION_TIMESTAMP_TAG + " "
|
||||
+AccurateTimestamp.GetCurrentTimestampString());
|
||||
|
||||
BasicMessage.Builder basicMessage =
|
||||
BasicMessage.newBuilder().addTag(idTag).addTag(groupIDTag).addTag(actionTag).addTag(timestampTag);
|
||||
|
||||
communicator.postMessage(CreateBulletinBoardMessage(basicMessage.build()));
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets that the voter have voted
|
||||
* @param id
|
||||
* @return true if the set voted succeded else false
|
||||
* @throws CommunicationException
|
||||
*/
|
||||
public void SetVoted(String id) throws CommunicationException, IOException {
|
||||
Tag.Builder idTag = Tag.newBuilder();
|
||||
idTag.setContent(Tags.ID_TAG + " " + id);
|
||||
|
||||
Tag.Builder voteAction = Tag.newBuilder();
|
||||
voteAction.setContent(Tags.VOTE_ACTION_TAG.toString());
|
||||
|
||||
Tag.Builder timestampTag = Tag.newBuilder();
|
||||
timestampTag.setContent(Tags.ACTION_TIMESTAMP_TAG + " "
|
||||
+AccurateTimestamp.GetCurrentTimestampString());
|
||||
|
||||
BasicMessage.Builder basicMessage = BasicMessage.newBuilder().addTag(idTag).addTag(voteAction).addTag(timestampTag);
|
||||
|
||||
communicator.postMessage(CreateBulletinBoardMessage(basicMessage.build()));
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates bulletin board message from basic message (with the signatures parts)
|
||||
* @param basicMessage BasicMessage
|
||||
* Creates BulletinBoardMessage with signed basicMessage and UnsignedBulletinBoardMessage that contains the basic message
|
||||
*
|
||||
* @param basicMessage BasicMessage
|
||||
* @return BulletinBoardAPI.BulletinBoardMessage
|
||||
*/
|
||||
private BulletinBoardAPI.BulletinBoardMessage CreateBulletinBoardMessage(BasicMessage basicMessage) throws IOException {
|
||||
|
||||
private BulletinBoardAPI.BulletinBoardMessage CreateBulletinBoardMessage(BasicMessage basicMessage) throws IOException {
|
||||
BulletinBoardAPI.BulletinBoardMessage.Builder bulletinBoardMessage =
|
||||
BulletinBoardAPI.BulletinBoardMessage.newBuilder();
|
||||
|
||||
|
@ -167,62 +70,162 @@ public class SimpleRegistry {
|
|||
|
||||
/**
|
||||
* Gets messages that have the wanted id tag and have or the ADD_TO_GROUP_TAG or REMOVE_FROM_GROUP_TAG
|
||||
* @param id
|
||||
* @return List<BulletinBoardAPI.BulletinBoardMessage>
|
||||
*
|
||||
* @param tags the tags based on which the messages will be filtered
|
||||
* @return List<BulletinBoardAPI.BulletinBoardMessage>
|
||||
*/
|
||||
private List<BulletinBoardAPI.BulletinBoardMessage> GetRelevantMessages(String id) {
|
||||
private List<BulletinBoardAPI.BulletinBoardMessage> GetRelevantMessages(List<Tag> tags) {
|
||||
BulletinBoardAPI.MessageFilterList.Builder filters =
|
||||
BulletinBoardAPI.MessageFilterList.newBuilder();
|
||||
|
||||
BulletinBoardAPI.MessageFilter.Builder idFilter =
|
||||
BulletinBoardAPI.MessageFilter.newBuilder().setTag(Tags.ID_TAG + " " + id)
|
||||
.setTag(Tags.GROUP_ACTION_TAG + " " + Tags.ADD_TO_GROUP_TAG)
|
||||
.setTag(Tags.GROUP_ACTION_TAG + " " + Tags.REMOVE_FROM_GROUP_TAG);
|
||||
|
||||
filters.addFilter(idFilter);
|
||||
for (Tag tag : tags) {
|
||||
BulletinBoardAPI.MessageFilter.Builder idFilter =
|
||||
BulletinBoardAPI.MessageFilter.newBuilder().setTag(tag.getContent());
|
||||
|
||||
filters.addFilter(idFilter);
|
||||
}
|
||||
return communicator.readMessages(filters.build());
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets Relevant bulletinBoard messages from communicator than converts them to VoterRegistryMessages
|
||||
*
|
||||
* @param tags list of tags that will be used as filters
|
||||
* @return List<VoterRegistryMessage>
|
||||
*/
|
||||
private List<VoterRegistryMessage> GetRelevantVoterRegistryMessages(List<Tag> tags) throws InvalidProtocolBufferException {
|
||||
List<BulletinBoardAPI.BulletinBoardMessage> relevantMessages = GetRelevantMessages(tags);
|
||||
List<BasicMessage> messages = CollectionMessagesUtils.GetBasicMessagesFromBulletinBoardMessages(relevantMessages);
|
||||
List<VoterRegistryMessage> voterRegistryMessages = CollectionMessagesUtils.ConvertToVoterRegistryMessages(messages);
|
||||
|
||||
return voterRegistryMessages;
|
||||
}
|
||||
|
||||
/**
|
||||
* Adds new voter to the bulletin-board
|
||||
*
|
||||
* @param voterID
|
||||
* @param personalData for example residence location
|
||||
* @return void
|
||||
* @throws throws CommunicationException
|
||||
*/
|
||||
public void SetVoted(String voterID, String personalData) throws CommunicationException, IOException {
|
||||
Tag.Builder[] tags = GetTagsArrayInLength(3);
|
||||
tags[0].setContent(Tags.ID_TAG + " " + voterID);
|
||||
tags[1].setContent(Tags.VOTER_ENTRY_TAG.toString());
|
||||
tags[2].setContent(Tags.ACTION_TIMESTAMP_TAG + " " + AccurateTimestamp.GetCurrentTimestampString());
|
||||
|
||||
BasicMessage.Builder basicMessage = BasicMessage.newBuilder().addTag(tags[0]).addTag(tags[1]).addTag(tags[1]);
|
||||
basicMessage.setData(ByteString.copyFrom(personalData.getBytes()));
|
||||
|
||||
communicator.postMessage(CreateBulletinBoardMessage(basicMessage.build()));
|
||||
}
|
||||
|
||||
/**
|
||||
* Adding given voter to given group
|
||||
*
|
||||
* @param voterID
|
||||
* @param groupID
|
||||
* @return true if the adding action succeeded else return false
|
||||
* @throws CommunicationException
|
||||
*/
|
||||
public void AddToGroup(String voterID, String groupID) throws CommunicationException, IOException {
|
||||
Tag.Builder[] tags = GetTagsArrayInLength(4);
|
||||
tags[0].setContent(Tags.ID_TAG + " " + voterID);
|
||||
tags[1].setContent(Tags.GROUP_ID_TAG + " " + groupID);
|
||||
tags[2].setContent(Tags.GROUP_ACTION_TAG + " " + Tags.ADD_TO_GROUP_TAG);
|
||||
tags[3].setContent(Tags.ACTION_TIMESTAMP_TAG + " " + AccurateTimestamp.GetCurrentTimestampString());
|
||||
|
||||
BasicMessage.Builder basicMessage =
|
||||
BasicMessage.newBuilder().addTag(tags[0]).addTag(tags[1]).addTag(tags[2]).addTag(tags[3]);
|
||||
|
||||
communicator.postMessage(CreateBulletinBoardMessage(basicMessage.build()));
|
||||
}
|
||||
|
||||
/**
|
||||
* Removes given voter from given group
|
||||
*
|
||||
* @param voterID
|
||||
* @param groupID
|
||||
* @return true if the removing action succeeded else return false
|
||||
* @throws CommunicationException
|
||||
*/
|
||||
public void RemoveFromGroup(String voterID, String groupID) throws CommunicationException, IOException {
|
||||
Tag.Builder[] tags = GetTagsArrayInLength(4);
|
||||
tags[0].setContent(Tags.ID_TAG + " " + voterID);
|
||||
tags[1].setContent(Tags.GROUP_ID_TAG + " " + groupID);
|
||||
tags[2].setContent(Tags.GROUP_ACTION_TAG + " " + Tags.REMOVE_FROM_GROUP_TAG);
|
||||
tags[3].setContent(Tags.ACTION_TIMESTAMP_TAG + " " + AccurateTimestamp.GetCurrentTimestampString());
|
||||
|
||||
BasicMessage.Builder basicMessage =
|
||||
BasicMessage.newBuilder().addTag(tags[0]).addTag(tags[1]).addTag(tags[2]).addTag(tags[3]);
|
||||
|
||||
communicator.postMessage(CreateBulletinBoardMessage(basicMessage.build()));
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets that the voter have voted
|
||||
*
|
||||
* @param id id tag string
|
||||
* @return true if the set voted succeded else false
|
||||
* @throws CommunicationException
|
||||
*/
|
||||
public void SetVoted(String id) throws CommunicationException, IOException {
|
||||
Tag.Builder[] tags = GetTagsArrayInLength(3);
|
||||
tags[0].setContent(Tags.ID_TAG + " " + id);
|
||||
tags[1].setContent(Tags.VOTE_ACTION_TAG.toString());
|
||||
tags[2].setContent(Tags.ACTION_TIMESTAMP_TAG + " " + AccurateTimestamp.GetCurrentTimestampString());
|
||||
|
||||
BasicMessage.Builder basicMessage = BasicMessage.newBuilder().addTag(tags[0]).addTag(tags[1]).addTag(tags[2]);
|
||||
|
||||
communicator.postMessage(CreateBulletinBoardMessage(basicMessage.build()));
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Requests all the groups that the given id voter is in
|
||||
* @param id
|
||||
*
|
||||
* @param id id tag string
|
||||
* @return list of groups ids (or names), if the method fails its empty
|
||||
* @throws CommunicationException
|
||||
* @throws CommunicationException, InvalidProtocolBufferException
|
||||
*/
|
||||
public List<String> GetGroups(String id) throws CommunicationException, InvalidProtocolBufferException {
|
||||
List<BulletinBoardAPI.BulletinBoardMessage> relevantMessages = GetRelevantMessages(id);
|
||||
List<Tag> GroupsActionsTags = new ArrayList<Tag>();
|
||||
GroupsActionsTags.add(Tag.newBuilder().setContent(Tags.ID_TAG + " " + id).build());
|
||||
GroupsActionsTags.add(Tag.newBuilder().setContent(Tags.GROUP_ACTION_TAG + " " + Tags.REMOVE_FROM_GROUP_TAG).build());
|
||||
GroupsActionsTags.add(Tag.newBuilder().setContent(Tags.GROUP_ACTION_TAG + " " + Tags.ADD_TO_GROUP_TAG).build());
|
||||
|
||||
List<BasicMessage> messages =
|
||||
CollectionMessagesUtils.GetBasicMessagesFromBulletinBoardMessages(relevantMessages);
|
||||
List<VoterRegistryMessage> voterRegistryMessages = GetRelevantVoterRegistryMessages(GroupsActionsTags);
|
||||
|
||||
List<VoterRegistryMessage> voterRegistryMessages =
|
||||
CollectionMessagesUtils.ConvertToVoterRegistryMessages(messages);
|
||||
try{
|
||||
Map<String, VoterRegistryMessage> groupIdToMessage =
|
||||
CollectionMessagesUtils.GetLatestGroupsActions(voterRegistryMessages);
|
||||
try {
|
||||
Map<String, VoterRegistryMessage> groupIdToMessage = CollectionMessagesUtils.GetLatestGroupsActions(voterRegistryMessages);
|
||||
return CollectionMessagesUtils.GetListOfGroupIds(groupIdToMessage);
|
||||
}
|
||||
catch (ParseException e)
|
||||
{
|
||||
// write log
|
||||
} catch (ParseException e) {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Retrieves list of strings that represents voter
|
||||
* @param id
|
||||
*
|
||||
* @param id id tag string
|
||||
* @return list of strings (empty list if the lookup failed)
|
||||
* @throws CommunicationException
|
||||
*/
|
||||
public List<String> GetPersonIDDetails(String id) throws CommunicationException {
|
||||
/**
|
||||
* Retrieve all List<BulletinBoardMessage> that contains this id
|
||||
* search for message with the wanted personal data
|
||||
* Create list with personal data
|
||||
* return the list
|
||||
*/
|
||||
public List<String> GetPersonIDDetails(String id) throws CommunicationException, InvalidProtocolBufferException, ParseException {
|
||||
List<Tag> GroupsActionsTags = new ArrayList<Tag>();
|
||||
GroupsActionsTags.add(Tag.newBuilder().setContent(Tags.ID_TAG + " " + id).build());
|
||||
GroupsActionsTags.add(Tag.newBuilder().setContent(Tags.VOTER_ENTRY_TAG.toString()).build());
|
||||
|
||||
List<VoterRegistryMessage> voterRegistryMessages = GetRelevantVoterRegistryMessages(GroupsActionsTags);
|
||||
VoterRegistryMessage LatestMessage = voterRegistryMessages.get(0);
|
||||
|
||||
for (VoterRegistryMessage message : voterRegistryMessages) {
|
||||
if (LatestMessage.GetBasicMessageActionTimestamp().before(message.GetBasicMessageActionTimestamp()) {
|
||||
LatestMessage = message;
|
||||
}
|
||||
}
|
||||
|
||||
return Arrays.asList(LatestMessage.GetWantedTagFromBasicMessage(Tags.ID_TAG), LatestMessage.base.getData().toString());
|
||||
}
|
||||
}
|
||||
|
|
|
@ -12,7 +12,8 @@ import java.util.List;
|
|||
import java.util.Map;
|
||||
|
||||
/**
|
||||
* Created by Dasha on 1/15/2016.
|
||||
* Created by Vladimir Eliezer Tokarev on 1/15/2016.
|
||||
* adds extra functionality to Messages collections
|
||||
*/
|
||||
public abstract class CollectionMessagesUtils {
|
||||
|
||||
|
@ -47,7 +48,7 @@ public abstract class CollectionMessagesUtils {
|
|||
|
||||
if (temp != null) {
|
||||
if (temp != messages.get(i)) {
|
||||
if (temp.GetBasicMessageActionTimestamp().compareTo(messages.get(i).GetBasicMessageActionTimestamp()) < 0) {
|
||||
if (temp.GetBasicMessageActionTimestamp().before(messages.get(i).GetBasicMessageActionTimestamp())) {
|
||||
groupIdToMessage.put(groupId, messages.get(i));
|
||||
}
|
||||
}
|
||||
|
|
|
@ -27,3 +27,5 @@ public enum Tags {
|
|||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
|
Loading…
Reference in New Issue