| 1 | /** |
| 2 | * |
| 3 | * Copyright (C) 2011 Cloud Conscious, LLC. <info@cloudconscious.com> |
| 4 | * |
| 5 | * ==================================================================== |
| 6 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 7 | * you may not use this file except in compliance with the License. |
| 8 | * You may obtain a copy of the License at |
| 9 | * |
| 10 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | * |
| 12 | * Unless required by applicable law or agreed to in writing, software |
| 13 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 14 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 15 | * See the License for the specific language governing permissions and |
| 16 | * limitations under the License. |
| 17 | * ==================================================================== |
| 18 | */ |
| 19 | package org.jclouds.gogrid.options; |
| 20 | |
| 21 | import org.jclouds.gogrid.domain.JobState; |
| 22 | import org.jclouds.gogrid.domain.ObjectType; |
| 23 | import org.jclouds.http.options.BaseHttpRequestOptions; |
| 24 | |
| 25 | import java.util.Date; |
| 26 | |
| 27 | import static com.google.common.base.Preconditions.checkState; |
| 28 | import static org.jclouds.gogrid.reference.GoGridQueryParams.*; |
| 29 | |
| 30 | /** |
| 31 | * @author Oleksiy Yarmula |
| 32 | */ |
| 33 | public class GetJobListOptions extends BaseHttpRequestOptions { |
| 34 | |
| 35 | public static final GetJobListOptions NONE = new GetJobListOptions(); |
| 36 | |
| 37 | public GetJobListOptions maxItemsNumber(Integer maxNumber) { |
| 38 | checkState(!queryParameters.containsKey(MAX_NUMBER_KEY), "Can't have duplicate parameter of max returned items"); |
| 39 | queryParameters.put(MAX_NUMBER_KEY, maxNumber.toString()); |
| 40 | return this; |
| 41 | } |
| 42 | |
| 43 | public GetJobListOptions withStartDate(Date startDate) { |
| 44 | checkState(!queryParameters.containsKey(START_DATE_KEY), "Can't have duplicate start date for filtering"); |
| 45 | queryParameters.put(START_DATE_KEY, String.valueOf(startDate.getTime())); |
| 46 | return this; |
| 47 | } |
| 48 | |
| 49 | public GetJobListOptions withEndDate(Date endDate) { |
| 50 | checkState(!queryParameters.containsKey(END_DATE_KEY), "Can't have duplicate end date for filtering"); |
| 51 | queryParameters.put(END_DATE_KEY, String.valueOf(endDate.getTime())); |
| 52 | return this; |
| 53 | } |
| 54 | |
| 55 | public GetJobListOptions withOwner(String owner) { |
| 56 | checkState(!queryParameters.containsKey(OWNER_KEY), "Can't have duplicate owner name for filtering"); |
| 57 | queryParameters.put(OWNER_KEY, owner); |
| 58 | return this; |
| 59 | } |
| 60 | |
| 61 | public GetJobListOptions onlyForState(JobState jobState) { |
| 62 | checkState(!queryParameters.containsKey(JOB_STATE_KEY), "Can't have duplicate job state for filtering"); |
| 63 | queryParameters.put(JOB_STATE_KEY, jobState.toString()); |
| 64 | return this; |
| 65 | } |
| 66 | |
| 67 | public GetJobListOptions onlyForObjectType(ObjectType objectType) { |
| 68 | checkState(!queryParameters.containsKey(JOB_OBJECT_TYPE_KEY), "Can't have duplicate object type for filtering"); |
| 69 | queryParameters.put(JOB_OBJECT_TYPE_KEY, objectType.toString()); |
| 70 | return this; |
| 71 | } |
| 72 | |
| 73 | public GetJobListOptions onlyForObjectName(String objectName) { |
| 74 | checkState(!queryParameters.containsKey(OBJECT_KEY), "Can't have duplicate object name for filtering"); |
| 75 | queryParameters.put(OBJECT_KEY, objectName); |
| 76 | return this; |
| 77 | } |
| 78 | |
| 79 | /* |
| 80 | * This method is intended for testing |
| 81 | */ |
| 82 | @Override |
| 83 | public boolean equals(Object o) { |
| 84 | if (this == o) return true; |
| 85 | if (o == null || getClass() != o.getClass()) return false; |
| 86 | |
| 87 | GetJobListOptions options = (GetJobListOptions) o; |
| 88 | |
| 89 | return buildQueryParameters().equals(options.buildQueryParameters()); |
| 90 | } |
| 91 | |
| 92 | public static class Builder { |
| 93 | public GetJobListOptions create() { |
| 94 | return new GetJobListOptions(); |
| 95 | } |
| 96 | |
| 97 | public GetJobListOptions latestJobForObjectByName(String serverName) { |
| 98 | return new GetJobListOptions(). |
| 99 | maxItemsNumber(1). |
| 100 | onlyForObjectName(serverName); |
| 101 | } |
| 102 | } |
| 103 | |
| 104 | } |