|
| 1 | +/* |
| 2 | + * Copyright 2020 Google LLC |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +package com.google.firebase.remoteconfig; |
| 18 | + |
| 19 | +import static com.google.common.base.Preconditions.checkNotNull; |
| 20 | + |
| 21 | +import com.google.common.base.Strings; |
| 22 | +import com.google.firebase.internal.NonNull; |
| 23 | +import com.google.firebase.internal.Nullable; |
| 24 | +import com.google.firebase.remoteconfig.internal.TemplateResponse; |
| 25 | +import com.google.firebase.remoteconfig.internal.TemplateResponse.VersionResponse; |
| 26 | + |
| 27 | +import java.text.ParseException; |
| 28 | +import java.text.SimpleDateFormat; |
| 29 | +import java.util.TimeZone; |
| 30 | + |
| 31 | +/** |
| 32 | + * Represents a Remote Config template version. |
| 33 | + * Output only, except for the version description. Contains metadata about a particular |
| 34 | + * version of the Remote Config template. All fields are set at the time the specified Remote |
| 35 | + * Config template is published. A version's description field may be specified when |
| 36 | + * publishing a template. |
| 37 | + */ |
| 38 | +public final class Version { |
| 39 | + |
| 40 | + private String versionNumber; |
| 41 | + private long updateTime; |
| 42 | + private String updateOrigin; |
| 43 | + private String updateType; |
| 44 | + private User updateUser; |
| 45 | + private String description; |
| 46 | + private String rollbackSource; |
| 47 | + private boolean legacy; |
| 48 | + |
| 49 | + /** |
| 50 | + * Creates a new {@link Version} with a description. |
| 51 | + */ |
| 52 | + public static Version withDescription(String description) { |
| 53 | + return new Version().setDescription(description); |
| 54 | + } |
| 55 | + |
| 56 | + Version() { |
| 57 | + } |
| 58 | + |
| 59 | + Version(@NonNull VersionResponse versionResponse) { |
| 60 | + checkNotNull(versionResponse); |
| 61 | + this.versionNumber = versionResponse.getVersionNumber(); |
| 62 | + if (!Strings.isNullOrEmpty(versionResponse.getUpdateTime())) { |
| 63 | + SimpleDateFormat dateFormat = new SimpleDateFormat("EEE, dd MMM yyyy HH:mm:ss z"); |
| 64 | + dateFormat.setTimeZone(TimeZone.getTimeZone("UTC")); |
| 65 | + try { |
| 66 | + this.updateTime = dateFormat.parse(versionResponse.getUpdateTime()).getTime(); |
| 67 | + } catch (ParseException e) { |
| 68 | + this.updateTime = 0; |
| 69 | + } |
| 70 | + } |
| 71 | + this.updateOrigin = versionResponse.getUpdateOrigin(); |
| 72 | + this.updateType = versionResponse.getUpdateType(); |
| 73 | + TemplateResponse.UserResponse userResponse = versionResponse.getUpdateUser(); |
| 74 | + this.updateUser = (userResponse != null) ? new User(userResponse) : null; |
| 75 | + this.description = versionResponse.getDescription(); |
| 76 | + this.rollbackSource = versionResponse.getRollbackSource(); |
| 77 | + this.legacy = versionResponse.isLegacy(); |
| 78 | + } |
| 79 | + |
| 80 | + /** |
| 81 | + * Gets the version number of the template. |
| 82 | + * |
| 83 | + * @return The version number or null. |
| 84 | + */ |
| 85 | + @Nullable |
| 86 | + public String getVersionNumber() { |
| 87 | + return versionNumber; |
| 88 | + } |
| 89 | + |
| 90 | + /** |
| 91 | + * Gets the update time of the version. The timestamp of when this version of the Remote Config |
| 92 | + * template was written to the Remote Config backend. |
| 93 | + * |
| 94 | + * @return The update time of the version or null. |
| 95 | + */ |
| 96 | + @Nullable |
| 97 | + public long getUpdateTime() { |
| 98 | + return updateTime; |
| 99 | + } |
| 100 | + |
| 101 | + /** |
| 102 | + * Gets the origin of the template update action. |
| 103 | + * |
| 104 | + * @return The origin of the template update action or null. |
| 105 | + */ |
| 106 | + @Nullable |
| 107 | + public String getUpdateOrigin() { |
| 108 | + return updateOrigin; |
| 109 | + } |
| 110 | + |
| 111 | + /** |
| 112 | + * Gets the type of the template update action. |
| 113 | + * |
| 114 | + * @return The type of the template update action or null. |
| 115 | + */ |
| 116 | + @Nullable |
| 117 | + public String getUpdateType() { |
| 118 | + return updateType; |
| 119 | + } |
| 120 | + |
| 121 | + /** |
| 122 | + * Gets the update user of the template. |
| 123 | + * An aggregation of all metadata fields about the account that performed the update. |
| 124 | + * |
| 125 | + * @return The update user of the template or null. |
| 126 | + */ |
| 127 | + @Nullable |
| 128 | + public User getUpdateUser() { |
| 129 | + return updateUser; |
| 130 | + } |
| 131 | + |
| 132 | + /** |
| 133 | + * Gets the user-provided description of the corresponding Remote Config template. |
| 134 | + * |
| 135 | + * @return The description of the template or null. |
| 136 | + */ |
| 137 | + @Nullable |
| 138 | + public String getDescription() { |
| 139 | + return description; |
| 140 | + } |
| 141 | + |
| 142 | + /** |
| 143 | + * Gets the rollback source of the template. |
| 144 | + * |
| 145 | + * <p>The version number of the Remote Config template that has become the current version |
| 146 | + * due to a rollback. Only present if this version is the result of a rollback. |
| 147 | + * |
| 148 | + * @return The rollback source of the template or null. |
| 149 | + */ |
| 150 | + @Nullable |
| 151 | + public String getRollbackSource() { |
| 152 | + return rollbackSource; |
| 153 | + } |
| 154 | + |
| 155 | + /** |
| 156 | + * Indicates whether this Remote Config template was published before version history was |
| 157 | + * supported. |
| 158 | + * |
| 159 | + * @return true if the template was published before version history was supported, |
| 160 | + * and false otherwise. |
| 161 | + */ |
| 162 | + public boolean isLegacy() { |
| 163 | + return legacy; |
| 164 | + } |
| 165 | + |
| 166 | + /** |
| 167 | + * Sets the user-provided description of the template. |
| 168 | + * |
| 169 | + * @param description The description of the template. |
| 170 | + * @return This {@link Version}. |
| 171 | + */ |
| 172 | + public Version setDescription(String description) { |
| 173 | + this.description = description; |
| 174 | + return this; |
| 175 | + } |
| 176 | + |
| 177 | + VersionResponse toVersionResponse() { |
| 178 | + return new VersionResponse() |
| 179 | + .setDescription(this.description); |
| 180 | + } |
| 181 | +} |
0 commit comments