001package ch.gbrain.gwtstorage.model; 002 003/* 004 * #%L 005 * GwtStorage 006 * %% 007 * Copyright (C) 2016 gbrain.ch 008 * %% 009 * Licensed under the Apache License, Version 2.0 (the "License"); 010 * you may not use this file except in compliance with the License. 011 * You may obtain a copy of the License at 012 * 013 * http://www.apache.org/licenses/LICENSE-2.0 014 * 015 * Unless required by applicable law or agreed to in writing, software 016 * distributed under the License is distributed on an "AS IS" BASIS, 017 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 018 * See the License for the specific language governing permissions and 019 * limitations under the License. 020 * #L% 021 */ 022 023import com.fasterxml.jackson.annotation.JsonIgnore; 024import com.google.gwt.json.client.JSONParser; 025import com.google.gwt.json.client.JSONValue; 026 027/** 028 * The main parent class for any object that must be stored locally or remote By 029 * defining the abstract converter methods for your particular inherited object 030 * the object is available for simple storage by the StorageManager 031 * 032 */ 033// @JsonSubTypes({@Type(DomainItem.class), @Type(DomainItems.class)}) 034public abstract class StorageItem 035{ 036 037 private String id; 038 039 public void setId(String id) 040 { 041 this.id = id; 042 } 043 044 public String getId() 045 { 046 return this.id; 047 } 048 049 private static String STORAGEITEMKEYPREFIX = "item-"; 050 private static String STORAGEITEMTIMESUFFIX = "-t"; 051 052 /** 053 * Retrieve a unique key to identify StorageItems in a key/value storage 054 * 055 * @return Retrieve a unique key for this storage item instance. Build from a prefix + object-type-name + id 056 */ 057 @JsonIgnore 058 public String getStorageItemIdKey() 059 { 060 // return this.id.toString(); 061 return STORAGEITEMKEYPREFIX + this.getTypeName() + "-" + this.getId().toString(); 062 } 063 064 /** 065 * Retrieve a unique key to identify StorageItems Save Time in a key/value 066 * storage 067 * 068 * @return Retrieve a unique key for this storage item to store the time information. Based on the StorageItemKey + a suffix 069 */ 070 @JsonIgnore 071 public String getStorageItemTimeKey() 072 { 073 return this.getStorageItemIdKey() + STORAGEITEMTIMESUFFIX; 074 } 075 076 @JsonIgnore 077 public static boolean isStorageItemKey(String key) 078 { 079 if (key == null) return false; 080 if (key.startsWith(STORAGEITEMKEYPREFIX)) return true; 081 return false; 082 } 083 084 @JsonIgnore 085 public static boolean isStorageItemIdKey(String key) 086 { 087 if (key == null) return false; 088 if (isStorageItemKey(key) && !key.endsWith(STORAGEITEMTIMESUFFIX)) return true; 089 return false; 090 } 091 092 @JsonIgnore 093 public static boolean isStorageItemTimeKey(String key) 094 { 095 if (key == null) return false; 096 if (isStorageItemKey(key) && key.endsWith(STORAGEITEMTIMESUFFIX)) return true; 097 return false; 098 } 099 100 /** 101 * Retrieve a unique key to identify StorageItems for log entries 102 * 103 * @return 104 */ 105 @JsonIgnore 106 public String getLogId() 107 { 108 return " item=" + getId() + " class=" + this.getTypeName(); 109 } 110 111 private Integer version = 1; 112 113 public void setVersion(Integer version) 114 { 115 this.version = version; 116 } 117 118 public Integer getVersion() 119 { 120 return version; 121 } 122 123 @JsonIgnore 124 public String getTypeName() 125 { 126 return this.getClass().getCanonicalName(); 127 } 128 129 @JsonIgnore 130 public String getJsonFileName() 131 { 132 return this.getTypeName() + "-" + getId() + ".json"; 133 } 134 135 /** 136 * Used to convert the object value to a JsonValue. This will be used to store 137 * the object in the local and remote storage. 138 * 139 * @return 140 */ 141 @JsonIgnore 142 public abstract JSONValue toJson(); 143 144 /** 145 * Used to convert the JSONValue object (as stored in local and remote 146 * storage) back to the object itself. 147 * 148 * @param storedItem 149 * The value which was stored in the LocalStore and should be 150 * transformed back into the object. 151 */ 152 @JsonIgnore 153 public abstract void fromJson(JSONValue json); 154 155 /** 156 * Convenience method to read the attributes of the StorageItem in and assign 157 * them internally from a Json read object which is inherited from 158 * StrorageItem 159 * 160 * @param storageItem 161 * The item from which the values must be read and assign 162 */ 163 @JsonIgnore 164 public final void fromJson(StorageItem storageItem) 165 { 166 this.id = storageItem.id; 167 this.version = storageItem.version; 168 if (this.version == null) this.version = 0; 169 } 170 171 /** 172 * Converts back from a json string to the object itself 173 * 174 * @param jsonString 175 */ 176 @JsonIgnore 177 public void fromJson(String jsonString) 178 { 179 JSONValue val = JSONParser.parseStrict(jsonString); 180 fromJson(val); 181 } 182 183 /** 184 * Retrieves the current object state as stringified JSON value 185 * 186 * @return The current objects state as JSON String 187 */ 188 @JsonIgnore 189 public String toString() 190 { 191 return toJson().toString(); 192 } 193 194}