User.java 12.4 KB
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514
/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package fi.insomnia.bortal.model;

import static javax.persistence.CascadeType.ALL;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;

import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.EnumType;
import javax.persistence.Enumerated;
import javax.persistence.FetchType;
import javax.persistence.JoinColumn;
import javax.persistence.JoinTable;
import javax.persistence.ManyToMany;
import javax.persistence.OneToMany;
import javax.persistence.OneToOne;
import javax.persistence.OrderBy;
import javax.persistence.Table;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;
import javax.persistence.Transient;
import javax.validation.constraints.NotNull;

import org.eclipse.persistence.annotations.PrivateOwned;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import fi.insomnia.bortal.enums.Gender;
import fi.insomnia.bortal.utilities.PasswordFunctions;

/**
 * 
 */
@Entity
@Table(name = "users")
public class User extends GenericEntity {
    public static final String ANONYMOUS_LOGINNAME = "anonymous";
    private static final long serialVersionUID = -1632200627103418206L;

    @Column(name = "created", nullable = false)
    // , columnDefinition = "timestamptz default now()")
    @Temporal(TemporalType.TIMESTAMP)
    private Calendar created = Calendar.getInstance();

    @NotNull
    @Column(name = "login", unique = true, nullable = false)
    private String login = "";

    @Column(name = "active", nullable = false, columnDefinition = "boolean default true")
    private boolean active = true;

    @Column(name = "password")
    private String password;

    @Column(name = "lastname")
    private String lastname = "";

    @Column(name = "firstnames")
    private String firstnames = "";

    @Column(name = "birthday")
    @Temporal(TemporalType.TIMESTAMP)
    private Calendar birthday;

    @Column(name = "nick")
    private String nick = "";

    @Column(name = "email")
    private String email = "";

    @Column(name = "address")
    private String address = "";

    @Column(name = "zip")
    private String zip = "";

    @Column(name = "postal_town")
    private String postalTown = "";

    @Column(name = "town")
    private String town = "";

    @Column(name = "phone")
    private String phone = "";

    @OneToOne
    @JoinColumn(name = "current_image_id", referencedColumnName = "id")
    private UserImage currentImage;

    @Enumerated(EnumType.STRING)
    @Column(name = "gender", nullable = false)
    private Gender gender = Gender.UNDEFINED;

    @Column(name = "confirm_hash")
    private String confirmHash;

    @Column(name = "confirm_time")
    @Temporal(TemporalType.TIMESTAMP)
    private Calendar confirmTime;

    @Column(name = "superadmin")
    private boolean superadmin = false;

    @OneToMany(mappedBy = "voter", cascade = CascadeType.ALL)
    private List<Vote> votes;

    @ManyToMany()
    @JoinTable(name = "role_memberships", inverseJoinColumns = {
            @JoinColumn(name = "role_id", referencedColumnName = Role.ID_COLUMN) },
            joinColumns = { @JoinColumn(name = "user_id", referencedColumnName = "id") })
    private List<Role> roles = new ArrayList<Role>();

    @OneToMany(mappedBy = "user")
    private List<LogEntry> logEntryList;

    @OneToMany(mappedBy = "user", fetch = FetchType.LAZY, cascade = CascadeType.ALL)
    @OrderBy
    @PrivateOwned
    private List<UserImage> userImageList;

    @OneToMany(mappedBy = "user")
    private List<CompoEntryParticipant> compoEntryParticipants;

    @OneToMany(mappedBy = "creator")
    @OrderBy("id")
    private List<CompoEntry> compoEntries;

    @OneToMany(mappedBy = "creator", cascade = ALL)
    private List<PlaceGroup> placeGroups = new ArrayList<PlaceGroup>();

    @OneToMany(mappedBy = "user")
    private List<GroupMembership> groupMemberships;

    /**
     * The places this user has registered into.
     */
    @OneToMany(mappedBy = "currentUser", fetch = FetchType.LAZY)
    @OrderBy("id")
    private List<Place> currentPlaces;

    @OneToMany(cascade = CascadeType.ALL, mappedBy = "user")
    private List<PrintedCard> printedCards;

    @OneToMany(cascade = CascadeType.ALL, mappedBy = "user")
    @OrderBy("id.id")
    private List<AccountEvent> accountEvents;

    @OneToMany(cascade = CascadeType.ALL, mappedBy = "user")
    @OrderBy("id.id")
    private List<Bill> bills;

    @OneToMany(cascade = CascadeType.ALL, mappedBy = "seller")
    @OrderBy("id.id")
    private List<AccountEvent> soldItems;

    @OneToMany(mappedBy = "admin")
    private List<EventOrganiser> eventOrganiser;

    @OneToMany(mappedBy = "user")
    private List<PollAnswer> pollAnswers;

    @Transient
    private static final Logger logger = LoggerFactory.getLogger(User.class);

    public BigDecimal getAccountBalance() {
        BigDecimal ret = BigDecimal.ZERO;
        for (AccountEvent ac : getAccountEvents()) {
            ret = ret.add(ac.getTotal()).setScale(2, RoundingMode.HALF_UP);
        }
        return ret;
    }

    public List<Vote> getVotes() {
        return votes;
    }

    public void setVotes(List<Vote> votes) {
        this.votes = votes;
    }

    public Calendar getCreated() {
        return created;
    }

    public void setCreated(Calendar created) {
        this.created = created;
    }

    public boolean getActive() {
        return active;
    }

    public void setActive(boolean active) {
        this.active = active;
    }

    public String getPassword() {
        logger.warn("Directly reading raw User password");
        return password;
    }

    public void setPassword(String password) {
        logger.warn("Directly settings raw User password");
        this.password = password;
    }

    public String getWholeName() {
        String ret = new StringBuilder().append(firstnames).append(" ").append(lastname).toString().trim();
        if (ret.isEmpty()) {
            ret = login;
        }
        return ret;
    }

    public String getLastname() {
        return lastname;
    }

    public void setLastname(String lastname) {
        this.lastname = lastname;
    }

    public String getFirstnames() {
        return firstnames;
    }

    public void setFirstnames(String firstnames) {
        this.firstnames = firstnames;
    }

    public Calendar getBirthday() {
        return birthday;
    }

    public void setBirthday(Calendar birthday) {
        this.birthday = birthday;
    }

    public String getNick() {
        return nick;
    }

    public void setNick(String nick) {
        this.nick = nick;
    }

    public String getEmail() {
        return email;
    }

    public void setEmail(String email) {
        this.email = email;
    }

    public String getAddress() {
        return address;
    }

    public void setAddress(String address) {
        this.address = address;
    }

    public String getZip() {
        return zip;
    }

    public void setZip(String zip) {
        this.zip = zip;
    }

    public String getTown() {
        return town;
    }

    public void setTown(String town) {
        this.town = town;
    }

    public String getPhone() {
        return phone;
    }

    public void setPhone(String phone) {
        this.phone = phone;
    }

    public String getLogin() {
        return login;
    }

    public void setLogin(String login) {
        // Do not allow anonymous userchange
        if (!isAnonymous()) {
            if (login != null) {
                this.login = login.trim().toLowerCase();
            } else {
                login = null;
            }
        }

    }

    public List<Vote> getVoteList() {
        return votes;
    }

    public void setVoteList(List<Vote> voteList) {
        this.votes = voteList;
    }

    public List<LogEntry> getLogEntryList() {
        return logEntryList;
    }

    public void setLogEntryList(List<LogEntry> logEntryList) {
        this.logEntryList = logEntryList;
    }

    public List<UserImage> getUserImageList() {
        return userImageList;
    }

    public void setUserImageList(List<UserImage> userImageList) {
        this.userImageList = userImageList;
    }

    public List<CompoEntryParticipant> getCompoEntryParticipants() {
        return compoEntryParticipants;
    }

    public void setCompoEntryParticipants(
            List<CompoEntryParticipant> compoEntryParticipantList) {
        this.compoEntryParticipants = compoEntryParticipantList;
    }

    public List<CompoEntry> getCompoEntries() {
        return compoEntries;
    }

    public void setCompoEntries(List<CompoEntry> compoEntryList) {
        this.compoEntries = compoEntryList;
    }

    public List<PlaceGroup> getPlaceGroups() {
        return placeGroups;
    }

    public void setPlaceGroups(List<PlaceGroup> placeGroupList) {
        this.placeGroups = placeGroupList;
    }

    public List<GroupMembership> getGroupMemberships() {
        return groupMemberships;
    }

    public void setGroupMemberships(List<GroupMembership> groupMembershipList) {
        this.groupMemberships = groupMembershipList;
    }

    public List<Place> getCurrentPlaces() {
        return currentPlaces;
    }

    public void setCurrentPlaces(List<Place> placeList) {
        this.currentPlaces = placeList;
    }

    public List<PrintedCard> getPrintedCards() {
        return printedCards;
    }

    public void setPrintedCards(List<PrintedCard> printedCardList) {
        this.printedCards = printedCardList;
    }

    public List<AccountEvent> getAccountEvents() {
        return accountEvents;
    }

    public void setAccountEvents(List<AccountEvent> accountEventList) {
        this.accountEvents = accountEventList;
    }

    public List<Bill> getBills() {
        return bills;
    }

    public void setBills(List<Bill> billList) {
        this.bills = billList;
    }

    public void setRoles(List<Role> roles) {
        this.roles = roles;
    }

    public List<Role> getRoles() {
        return roles;
    }

    public void setSoldItems(List<AccountEvent> accountEvents) {
        this.soldItems = accountEvents;
    }

    public List<AccountEvent> getSoldItems() {
        return soldItems;
    }

    /**
     * @return the confirmHash
     */
    public String getConfirmHash() {
        return confirmHash;
    }

    /**
     * @param confirmHash
     *            the confirmHash to set
     */
    public void setConfirmHash(String confirmHash) {
        this.confirmHash = confirmHash;
    }

    /**
     * @return the confirmTime
     */
    public Calendar getConfirmTime() {
        return confirmTime;
    }

    /**
     * @param confirmTime
     *            the confirmTime to set
     */
    public void setConfirmTime(Calendar confirmTime) {
        this.confirmTime = confirmTime;
    }

    public void resetPassword(String password) {
        String newEncryptedPassword = PasswordFunctions.getEncryptedPassword(password);
        this.password = newEncryptedPassword; // Bypass setter
    }

    public boolean checkPassword(String plainPassword) {
        boolean matches = PasswordFunctions.checkPlainPassword(plainPassword, this.password);
        return matches;
    }

    public void setEventOrganiser(List<EventOrganiser> eventOrganiser) {
        this.eventOrganiser = eventOrganiser;
    }

    public List<EventOrganiser> getEventOrganiser() {
        return eventOrganiser;
    }

    public void setSuperadmin(boolean superadmin) {
        this.superadmin = superadmin;
    }

    public boolean isSuperadmin() {
        return superadmin;
    }

    public void setPostalTown(String postalTown) {
        this.postalTown = postalTown;
    }

    public String getPostalTown() {
        return postalTown;
    }

    public void setGender(Gender gender) {
        this.gender = gender;
    }

    public Gender getGender() {
        return gender;
    }

    public void setCurrentImage(UserImage currentImage) {
        this.currentImage = currentImage;
    }

    public UserImage getCurrentImage() {
        return currentImage;
    }

    public void setPollAnswers(List<PollAnswer> pollAnswers) {
        this.pollAnswers = pollAnswers;
    }

    public List<PollAnswer> getPollAnswers() {
        return pollAnswers;
    }

    @Transient
    private Boolean isAnon;

    public boolean isAnonymous() {
        if (isAnon == null) {
            isAnon = ANONYMOUS_LOGINNAME.equals(login);
        }
        return isAnon;
    }
}