| 1 | // Copyright 2004-2007 Jean-Francois Poilpret |
| 2 | // |
| 3 | // Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | // you may not use this file except in compliance with the License. |
| 5 | // You may obtain a copy of the License at |
| 6 | // |
| 7 | // http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | // |
| 9 | // Unless required by applicable law or agreed to in writing, software |
| 10 | // distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | // See the License for the specific language governing permissions and |
| 13 | // limitations under the License. |
| 14 | |
| 15 | package net.sourceforge.hiveboard.business; |
| 16 | |
| 17 | import java.util.HashSet; |
| 18 | import java.util.Set; |
| 19 | |
| 20 | import net.sourceforge.hiveboard.Account; |
| 21 | import net.sourceforge.hiveboard.Board; |
| 22 | import net.sourceforge.hiveboard.DoesNotExistException; |
| 23 | |
| 24 | final public class ValidityChecks |
| 25 | { |
| 26 | private ValidityChecks() |
| 27 | { |
| 28 | } |
| 29 | |
| 30 | static public void checkAccount(Account account) |
| 31 | { |
| 32 | if (account.getVisa() == null || account.getVisa().length() == 0) |
| 33 | { |
| 34 | throw new IllegalArgumentException("Account Visa must not be null"); |
| 35 | } |
| 36 | if (account.getName() == null || account.getName().length() == 0) |
| 37 | { |
| 38 | throw new IllegalArgumentException("Account Name must not be null"); |
| 39 | } |
| 40 | if (account.getRole() == null) |
| 41 | { |
| 42 | throw new IllegalArgumentException("Account Role must not be null"); |
| 43 | } |
| 44 | String email = account.getEmail(); |
| 45 | if (email != null) |
| 46 | { |
| 47 | email = email.trim(); |
| 48 | if (email.equals("")) |
| 49 | { |
| 50 | email = null; |
| 51 | } |
| 52 | account.setEmail(email); |
| 53 | } |
| 54 | } |
| 55 | |
| 56 | static public void checkBoard(Board board, int maxSize, AccountRepository accounts) |
| 57 | { |
| 58 | // Check size |
| 59 | checkMaxValue( board.getWidth(), maxSize, |
| 60 | "Board width must be less than " + maxSize); |
| 61 | checkMaxValue( board.getHeight(), maxSize, |
| 62 | "Board height must be less than " + maxSize); |
| 63 | String desc = board.getDescription(); |
| 64 | if (desc != null && desc.trim().equals("")) |
| 65 | { |
| 66 | board.setDescription(null); |
| 67 | } |
| 68 | Set<Integer> participants = board.getParticipants(); |
| 69 | if (participants == null) |
| 70 | { |
| 71 | participants = new HashSet<Integer>(); |
| 72 | board.setParticipants(participants); |
| 73 | } |
| 74 | participants.add(board.getInitiator()); |
| 75 | if (participants != null) |
| 76 | { |
| 77 | for (int id: participants) |
| 78 | { |
| 79 | if (accounts.getAccount(id, false) == null) |
| 80 | { |
| 81 | throw new DoesNotExistException("Unknown participant id " + id); |
| 82 | } |
| 83 | } |
| 84 | } |
| 85 | } |
| 86 | |
| 87 | static public void checkPassword(String password) |
| 88 | { |
| 89 | if (password == null || password.length() == 0) |
| 90 | { |
| 91 | throw new IllegalArgumentException("Password must not be null"); |
| 92 | } |
| 93 | } |
| 94 | |
| 95 | static public void checkMaxValue(int value, int max, String error) |
| 96 | { |
| 97 | if (value > max) |
| 98 | { |
| 99 | throw new IllegalArgumentException(error); |
| 100 | } |
| 101 | } |
| 102 | } |