621e1472267eec0855520c6e02f88471b373273e
[WeStealzYourDataz.git] / src / uk / ac / ntu / n0521366 / wsyd / libs / WSYD_Member.java
1 /*
2  * The MIT License
3  *
4  * Copyright 2015 TJ <hacker@iam.tj>.
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.Arrays;
29 import java.util.Date;
30 import java.util.TreeSet;
31 import java.util.logging.Level;
32
33 /**
34  * Contains personal data about a member.
35  * 
36  * _userID contains a key that uniquely identifies this member. This should be used by other
37  * classes and friend references to ensure data is not duplicated unnecessarily.
38  * 
39  * The object can be serialized over a stream to a File or Network connection and added to a sorted collection.
40  * It can be added to ordered Collections and provides its own compareTo() using userName.
41  *
42  * It is supported by 2 Comparators:
43  * 
44  * WSYD_Member_Comparator_UserID supports sorted Maps with userID as the sort key
45  * WSYD_Member_Comparator_UserName supports sorted Sets and Collections with userName as the sort key
46  *
47  * @author TJ <hacker@iam.tj>
48  */
49 public class WSYD_Member implements java.io.Serializable, java.lang.Comparable<WSYD_Member> {
50     public long _userID;
51     public String _userName;
52     public String _password;
53     public String _currentLocation;
54     public String _bio;
55     public Date _birthDate;
56     public TreeSet<String> _interests;
57     public TreeSet<Long> _friends;
58     public TreeSet<Long> _friendsRequestsSent;
59     public TreeSet<Long> _friendsRequestsReceived;
60     
61     public static enum Fields {
62         USER_ID, USER_NAME, PASSWORD, CURRENT_LOCATION, BIO, BIRTH_DATE, INTERESTS, FRIENDS, FRIENDS_REQUESTS_SENT, FRIENDS_REQUESTS_RECEIVED
63     }
64     
65     /**
66      * Default constructor
67      */
68     public WSYD_Member() {}
69     
70     /**
71      * Construct a complete member
72      * 
73      * @param userID unique key
74      * @param userName user's preferred name
75      * @param password password for authentication
76      * @param currentLocation Geographic area
77      * @param bio Personal biography
78      * @param birthDate date of birth
79      * @param interests titles of personal interests
80      * @param friends IDs of all accepted friends
81      * @param friendsRequestsSent IDs of pending friends requests 
82      * @param friendsRequestsReceived IDs of pending friends requests sent
83      */
84     public WSYD_Member(long userID, String userName, String password, String currentLocation, String bio,
85                        Date birthDate,
86                        TreeSet<String> interests,
87                        TreeSet<Long> friends,
88                        TreeSet<Long> friendsRequestsSent,
89                        TreeSet<Long> friendsRequestsReceived
90     )
91     {
92         _userID = userID;
93         _userName = userName;
94         _password = password;
95         _currentLocation = currentLocation;
96         _bio = bio;
97         _birthDate = birthDate;
98         _interests = interests;
99         _friends = friends;
100         _friendsRequestsSent = friendsRequestsSent;
101         _friendsRequestsReceived = friendsRequestsReceived;
102     }
103
104     public static WSYD_Member createWSYD_Member(String line) throws IllegalArgumentException {
105         WSYD_Member result = null;
106         
107         if (line.startsWith("#")) {
108             return result; // ignore comment lines
109         }
110         result = new WSYD_Member();
111         // split line into array of fields
112         String[] fields = line.split(",");
113         if (fields.length != 10)
114             throw new IllegalArgumentException();
115
116         // convert each field from a String to the correct type using the Fields enum
117         result._userID = new Long(fields[WSYD_Member.Fields.USER_ID.ordinal()]);
118         result._userName = fields[WSYD_Member.Fields.USER_NAME.ordinal()];
119         result._password = fields[WSYD_Member.Fields.PASSWORD.ordinal()];
120         result._currentLocation = fields[WSYD_Member.Fields.CURRENT_LOCATION.ordinal()];
121         result._bio = fields[WSYD_Member.Fields.BIO.ordinal()];
122         SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");
123         try {
124             result._birthDate = df.parse(fields[WSYD_Member.Fields.BIRTH_DATE.ordinal()]);
125         } catch (ParseException e) {
126             result._birthDate = new Date();
127         }
128         // read the multi-element entries. "~" is used as the element separator
129         result._interests = new TreeSet<>();
130         String[] items = fields[WSYD_Member.Fields.INTERESTS.ordinal()].split("~");
131         result._interests.addAll(Arrays.asList(items));
132         result._friends = new TreeSet<>();
133         items = fields[WSYD_Member.Fields.FRIENDS.ordinal()].split("~");
134         for (String item : items) {
135             result._friends.add(new java.lang.Long(item));
136         }
137         result._friendsRequestsSent = new TreeSet<>();
138         items = fields[WSYD_Member.Fields.FRIENDS_REQUESTS_SENT.ordinal()].split("~");
139         for (String item : items) {
140             result._friendsRequestsSent.add(new Long(item));
141         }
142         result._friendsRequestsReceived = new TreeSet<>();
143         items = fields[WSYD_Member.Fields.FRIENDS_REQUESTS_RECEIVED.ordinal()].split("~");
144         for (String item : items) {
145             result._friendsRequestsReceived.add(new Long(item));
146         }
147
148         return result;
149     }
150
151     /**
152      * implement the java.lang.Comparable interface
153      * 
154      * @param m the member to compare with
155      * @return Returns a negative integer, zero, or a positive integer as this member is less than, equal to, or greater than the provided member
156      */
157     @Override
158     public int compareTo(WSYD_Member m) throws IllegalArgumentException {
159         if (m == null)
160             throw new IllegalArgumentException("Reference cannot be null");
161         return this._userName.compareTo(m._userName);
162     }
163     
164     /**
165      * Convert to a String suitable for display and export to CSV
166      * 
167      * Fields containing collections write collection members separated by "~"
168      * 
169      * @return String
170      */
171     @Override
172     public String toString() {
173         String interests = new String();
174         for (String i: _interests) {
175             interests = interests.concat((interests.length() > 0 ? "~" : "") + i);
176         }
177         String friends = new String();
178         for (Long l: _friends) {
179             friends = friends.concat((friends.length() > 0 ? "~" : "") + l.toString());
180         }
181         String friendsRequestsSent = new String();
182         for (Long l: _friendsRequestsSent) {
183             friendsRequestsSent = friendsRequestsSent.concat((friendsRequestsSent.length() > 0 ? "~" : "") + l.toString());
184         }
185         String friendsRequestsReceived = new String();
186         for (Long l: _friendsRequestsReceived) {
187             friendsRequestsReceived = friendsRequestsReceived.concat((friendsRequestsReceived.length() > 0 ? "~" : "") + l.toString());
188         }
189         SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");
190
191         String record = Long.toString(_userID) + ","
192                     + _userName + ","
193                     + _password + ","
194                     + _currentLocation + ","
195                     + _bio + ","
196                     + df.format(_birthDate) + ","
197                     + interests + ","
198                     + friends + ","
199                     + friendsRequestsSent + ","
200                     + friendsRequestsReceived;
201
202         return record;
203     }
204 }