Add Registration functionality and tidy up
[WeStealzYourDataz.git] / src / uk / ac / ntu / n0521366 / wsyd / libs / WSYD_Member.java
1 /*
2  * The MIT License
3  *
4  * Copyright 2015 Eddie Berrisford-Lynch <dev@fun2be.me>.
5  *
6  * Permission is hereby granted, free of charge, to any person obtaining a copy
7  * of this software and associated documentation files (the "Software"), to deal
8  * in the Software without restriction, including without limitation the rights
9  * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10  * copies of the Software, and to permit persons to whom the Software is
11  * furnished to do so, subject to the following conditions:
12  *
13  * The above copyright notice and this permission notice shall be included in
14  * all copies or substantial portions of the Software.
15  *
16  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22  * THE SOFTWARE.
23  */
24 package uk.ac.ntu.n0521366.wsyd.libs;
25
26 import java.text.ParseException;
27 import java.text.SimpleDateFormat;
28 import java.util.ArrayList;
29 import java.util.Arrays;
30 import java.util.Date;
31 import java.util.TreeSet;
32 import java.util.logging.Level;
33
34 /**
35  * Contains personal data about a member.
36  * 
37  * _userID contains a key that uniquely identifies this member. This should be used by other
38  * classes and friend references to ensure data is not duplicated unnecessarily.
39  * 
40  * The object can be serialized over a stream to a File or Network connection and added to a sorted collection.
41  * It can be added to ordered Collections and provides its own compareTo() using userName.
42  *
43  * It is supported by 2 Comparators:
44  * 
45  * WSYD_Member_Comparator_UserID supports sorted Maps with userID as the sort key
46  * WSYD_Member_Comparator_UserName supports sorted Sets and Collections with userName as the sort key
47  *
48  * @author Eddie Berrisford-Lynch <dev@fun2be.me>
49  */
50 public class WSYD_Member implements java.io.Serializable, java.lang.Comparable<WSYD_Member> {
51     
52     /**
53      * Choices for _interests
54      */
55     public static String[] Interests = {"Computers","Driving","DIY","Gardening","Games","Gym","Music","Reading" };
56     
57     public long _userID;
58     public String _userName;
59     public String _password;
60     public String _currentLocation;
61     public String _bio;
62     public Date _birthDate;
63     public TreeSet<String> _interests;
64     public TreeSet<Long> _friends;
65     public TreeSet<Long> _friendsRequestsSent;
66     public TreeSet<Long> _friendsRequestsReceived;
67     
68     public static enum Fields {
69         USER_ID, USER_NAME, PASSWORD, CURRENT_LOCATION, BIO, BIRTH_DATE, INTERESTS, FRIENDS, FRIENDS_REQUESTS_SENT, FRIENDS_REQUESTS_RECEIVED
70     }
71     
72     /**
73      * Default constructor
74      */
75     public WSYD_Member() {}
76     
77     /**
78      * Construct a complete member
79      * 
80      * @param userID unique key
81      * @param userName user's preferred name
82      * @param password password for authentication
83      * @param currentLocation Geographic area
84      * @param bio Personal biography
85      * @param birthDate date of birth
86      * @param interests titles of personal interests
87      * @param friends IDs of all accepted friends
88      * @param friendsRequestsSent IDs of pending friends requests 
89      * @param friendsRequestsReceived IDs of pending friends requests sent
90      */
91     public WSYD_Member(long userID, String userName, String password, String currentLocation, String bio,
92                        Date birthDate,
93                        TreeSet<String> interests,
94                        TreeSet<Long> friends,
95                        TreeSet<Long> friendsRequestsSent,
96                        TreeSet<Long> friendsRequestsReceived
97     )
98     {
99         _userID = userID;
100         _userName = userName;
101         _password = password;
102         _currentLocation = currentLocation;
103         _bio = bio;
104         _birthDate = birthDate;
105         _interests = interests;
106         _friends = friends;
107         _friendsRequestsSent = friendsRequestsSent;
108         _friendsRequestsReceived = friendsRequestsReceived;
109     }
110
111     public static WSYD_Member createWSYD_Member(String line) throws IllegalArgumentException {
112         WSYD_Member result = null;
113         
114         if (line.startsWith("#")) {
115             return result; // ignore comment lines
116         }
117         result = new WSYD_Member();
118         // split line into array of fields
119         String[] fields = line.split(",");
120         if (fields.length != 10)
121             throw new IllegalArgumentException();
122
123         // convert each field from a String to the correct type using the Fields enum
124         result._userID = new Long(fields[WSYD_Member.Fields.USER_ID.ordinal()]);
125         result._userName = fields[WSYD_Member.Fields.USER_NAME.ordinal()];
126         result._password = fields[WSYD_Member.Fields.PASSWORD.ordinal()];
127         result._currentLocation = fields[WSYD_Member.Fields.CURRENT_LOCATION.ordinal()];
128         result._bio = fields[WSYD_Member.Fields.BIO.ordinal()];
129         SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");
130         try {
131             result._birthDate = df.parse(fields[WSYD_Member.Fields.BIRTH_DATE.ordinal()]);
132         } catch (ParseException e) {
133             result._birthDate = new Date();
134         }
135         // read the multi-element entries. "~" is used as the element separator
136         result._interests = new TreeSet<>();
137         String[] items = fields[WSYD_Member.Fields.INTERESTS.ordinal()].split("~");
138         result._interests.addAll(Arrays.asList(items));
139         result._friends = new TreeSet<>();
140         items = fields[WSYD_Member.Fields.FRIENDS.ordinal()].split("~");
141         for (String item : items) {
142             result._friends.add(new java.lang.Long(item));
143         }
144         result._friendsRequestsSent = new TreeSet<>();
145         items = fields[WSYD_Member.Fields.FRIENDS_REQUESTS_SENT.ordinal()].split("~");
146         for (String item : items) {
147             result._friendsRequestsSent.add(new Long(item));
148         }
149         result._friendsRequestsReceived = new TreeSet<>();
150         items = fields[WSYD_Member.Fields.FRIENDS_REQUESTS_RECEIVED.ordinal()].split("~");
151         for (String item : items) {
152             result._friendsRequestsReceived.add(new Long(item));
153         }
154
155         return result;
156     }
157
158     /**
159      * implement the java.lang.Comparable interface
160      * 
161      * @param m the member to compare with
162      * @return Returns a negative integer, zero, or a positive integer as this member is less than, equal to, or greater than the provided member
163      */
164     @Override
165     public int compareTo(WSYD_Member m) throws IllegalArgumentException {
166         if (m == null)
167             throw new IllegalArgumentException("Reference cannot be null");
168         return this._userName.compareTo(m._userName);
169     }
170     
171     /**
172      * Convert to a String suitable for display and export to CSV
173      * 
174      * Fields containing collections write collection members separated by "~"
175      * 
176      * @return String
177      */
178     @Override
179     public String toString() {
180         String interests = new String();
181         for (String i: _interests) {
182             interests = interests.concat((interests.length() > 0 ? "~" : "") + i);
183         }
184         String friends = new String();
185         for (Long l: _friends) {
186             friends = friends.concat((friends.length() > 0 ? "~" : "") + l.toString());
187         }
188         String friendsRequestsSent = new String();
189         for (Long l: _friendsRequestsSent) {
190             friendsRequestsSent = friendsRequestsSent.concat((friendsRequestsSent.length() > 0 ? "~" : "") + l.toString());
191         }
192         String friendsRequestsReceived = new String();
193         for (Long l: _friendsRequestsReceived) {
194             friendsRequestsReceived = friendsRequestsReceived.concat((friendsRequestsReceived.length() > 0 ? "~" : "") + l.toString());
195         }
196         SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");
197
198         String record = Long.toString(_userID) + ","
199                     + _userName + ","
200                     + _password + ","
201                     + _currentLocation + ","
202                     + _bio + ","
203                     + df.format(_birthDate) + ","
204                     + interests + ","
205                     + friends + ","
206                     + friendsRequestsSent + ","
207                     + friendsRequestsReceived;
208
209         return record;
210     }
211 }