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