| 1 | |
|
| 2 | |
|
| 3 | |
|
| 4 | |
|
| 5 | |
|
| 6 | |
|
| 7 | |
|
| 8 | |
|
| 9 | |
|
| 10 | |
|
| 11 | |
|
| 12 | |
|
| 13 | |
|
| 14 | |
|
| 15 | |
|
| 16 | |
package org.kuali.student.contract.model.validation; |
| 17 | |
|
| 18 | |
import org.kuali.student.contract.model.DictionaryModel; |
| 19 | |
import org.kuali.student.contract.model.SearchModel; |
| 20 | |
import org.kuali.student.contract.model.SearchType; |
| 21 | |
import org.kuali.student.contract.model.Service; |
| 22 | |
import org.kuali.student.contract.model.util.ModelFinder; |
| 23 | |
|
| 24 | |
import java.util.ArrayList; |
| 25 | |
import java.util.Collection; |
| 26 | |
|
| 27 | |
|
| 28 | |
|
| 29 | |
|
| 30 | |
|
| 31 | |
public class SearchTypeValidator implements ModelValidator { |
| 32 | |
|
| 33 | |
private SearchType searchType; |
| 34 | |
private SearchModel model; |
| 35 | |
|
| 36 | 0 | public SearchTypeValidator(SearchType searchType, SearchModel model) { |
| 37 | 0 | this.searchType = searchType; |
| 38 | 0 | this.model = model; |
| 39 | 0 | } |
| 40 | |
private Collection errors; |
| 41 | |
|
| 42 | |
@Override |
| 43 | |
public Collection<String> validate() { |
| 44 | 0 | errors = new ArrayList(); |
| 45 | 0 | basicValidation(); |
| 46 | 0 | if (searchType.getImplementation() == null) { |
| 47 | 0 | addError("JPQL implementation is required"); |
| 48 | |
} |
| 49 | 0 | if (searchType.getSearchCriteria() == null) { |
| 50 | 0 | addError("Criteria is required"); |
| 51 | |
} |
| 52 | 0 | ModelValidator validator = |
| 53 | |
new SearchCriteriaValidator(searchType.getSearchCriteria(), searchType); |
| 54 | 0 | errors.addAll(validator.validate()); |
| 55 | 0 | if (searchType.getSearchResult() == null) { |
| 56 | 0 | addError("Results is required"); |
| 57 | |
} |
| 58 | 0 | validator = |
| 59 | |
new SearchResultValidator(searchType.getSearchResult(), searchType); |
| 60 | 0 | errors.addAll(validator.validate()); |
| 61 | 0 | return errors; |
| 62 | |
} |
| 63 | |
|
| 64 | |
private void basicValidation() { |
| 65 | 0 | if (searchType.getKey().equals("")) { |
| 66 | 0 | addError("search type key is required"); |
| 67 | |
} |
| 68 | 0 | if (!searchType.getType().equals("Search")) { |
| 69 | 0 | addError("'Type' column in the search type must be 'Search'"); |
| 70 | |
} |
| 71 | 0 | if (searchType.getName().equals("")) { |
| 72 | 0 | addError("Name is required"); |
| 73 | |
} |
| 74 | 0 | if (searchType.getDescription().equals("")) { |
| 75 | 0 | addError("Description is required"); |
| 76 | |
} |
| 77 | 0 | if (!searchType.getDataType().equals("")) { |
| 78 | 0 | addError("Data Type should be blank"); |
| 79 | |
} |
| 80 | 0 | if (!searchType.getService().equals("")) { |
| 81 | 0 | if (findService(searchType.getService()) == null) { |
| 82 | 0 | addError("Service, [" + searchType.getService() |
| 83 | |
+ "] could not be found in the list of services"); |
| 84 | |
} |
| 85 | |
} |
| 86 | 0 | } |
| 87 | |
|
| 88 | |
private Service findService(String service) { |
| 89 | |
|
| 90 | 0 | if (!(model instanceof DictionaryModel)) { |
| 91 | 0 | return null; |
| 92 | |
} |
| 93 | 0 | return new ModelFinder((DictionaryModel) model).findService(service); |
| 94 | |
} |
| 95 | |
|
| 96 | |
private void addError(String msg) { |
| 97 | 0 | String error = "Error in searchType entry: " + searchType.getKey() |
| 98 | |
+ ": " + msg; |
| 99 | 0 | if (!errors.contains(error)) { |
| 100 | 0 | errors.add(error); |
| 101 | |
} |
| 102 | 0 | } |
| 103 | |
} |