|
|
@@ -0,0 +1,216 @@
|
|
|
+/**
|
|
|
+ * Copyright (c) 2015-2016, Michael Yang 杨福海 (fuhai999@gmail.com).
|
|
|
+ *
|
|
|
+ * Licensed under the GNU Lesser General Public License (LGPL) ,Version 3.0 (the "License");
|
|
|
+ * you may not use this file except in compliance with the License.
|
|
|
+ * You may obtain a copy of the License at
|
|
|
+ *
|
|
|
+ * http://www.gnu.org/licenses/lgpl-3.0.txt
|
|
|
+ *
|
|
|
+ * Unless required by applicable law or agreed to in writing, software
|
|
|
+ * distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
+ * See the License for the specific language governing permissions and
|
|
|
+ * limitations under the License.
|
|
|
+ */
|
|
|
+package io.jpress.model.base;
|
|
|
+
|
|
|
+import com.jfinal.plugin.activerecord.IBean;
|
|
|
+import com.jfinal.plugin.ehcache.CacheKit;
|
|
|
+import com.jfinal.plugin.ehcache.IDataLoader;
|
|
|
+import io.jpress.message.MessageKit;
|
|
|
+import io.jpress.model.Metadata;
|
|
|
+import io.jpress.model.core.JModel;
|
|
|
+import io.jpress.model.query.MetaDataQuery;
|
|
|
+
|
|
|
+import java.math.BigInteger;
|
|
|
+
|
|
|
+/**
|
|
|
+ * @author 黄诚天
|
|
|
+ * @param <M>
|
|
|
+ */
|
|
|
+@SuppressWarnings("serial")
|
|
|
+public abstract class BaseVote<M extends BaseVote<M>> extends JModel<M> implements IBean {
|
|
|
+
|
|
|
+ public static final String CACHE_NAME = "vote";
|
|
|
+ public static final String METADATA_TYPE = "vote";
|
|
|
+
|
|
|
+ public static final String ACTION_ADD = "vote:add";
|
|
|
+ public static final String ACTION_DELETE = "vote:delete";
|
|
|
+ public static final String ACTION_UPDATE = "vote:update";
|
|
|
+
|
|
|
+ public void removeCache(Object key){
|
|
|
+ if(key == null) return;
|
|
|
+ CacheKit.remove(CACHE_NAME, key);
|
|
|
+ }
|
|
|
+
|
|
|
+ public void putCache(Object key,Object value){
|
|
|
+ CacheKit.put(CACHE_NAME, key, value);
|
|
|
+ }
|
|
|
+
|
|
|
+ public M getCache(Object key){
|
|
|
+ return CacheKit.get(CACHE_NAME, key);
|
|
|
+ }
|
|
|
+
|
|
|
+ public M getCache(Object key,IDataLoader dataloader){
|
|
|
+ return CacheKit.get(CACHE_NAME, key, dataloader);
|
|
|
+ }
|
|
|
+
|
|
|
+ public Metadata createMetadata(){
|
|
|
+ Metadata md = new Metadata();
|
|
|
+ md.setObjectId(getId());
|
|
|
+ md.setObjectType(METADATA_TYPE);
|
|
|
+ return md;
|
|
|
+ }
|
|
|
+
|
|
|
+ public Metadata createMetadata(String key,String value){
|
|
|
+ Metadata md = new Metadata();
|
|
|
+ md.setObjectId(getId());
|
|
|
+ md.setObjectType(METADATA_TYPE);
|
|
|
+ md.setMetaKey(key);
|
|
|
+ md.setMetaValue(value);
|
|
|
+ return md;
|
|
|
+ }
|
|
|
+
|
|
|
+ public boolean saveOrUpdateMetadta(String key,String value){
|
|
|
+ Metadata metadata = MetaDataQuery.me().findByTypeAndIdAndKey(METADATA_TYPE, getId(), key);
|
|
|
+ if (metadata == null) {
|
|
|
+ metadata = createMetadata(key, value);
|
|
|
+ return metadata.save();
|
|
|
+ }
|
|
|
+ metadata.setMetaValue(value);
|
|
|
+ return metadata.update();
|
|
|
+ }
|
|
|
+
|
|
|
+ public String metadata(String key) {
|
|
|
+ Metadata m = MetaDataQuery.me().findByTypeAndIdAndKey(METADATA_TYPE, getId(), key);
|
|
|
+ if (m != null) {
|
|
|
+ return m.getMetaValue();
|
|
|
+ }
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public boolean equals(Object o) {
|
|
|
+ if(o == null){ return false; }
|
|
|
+ if(!(o instanceof BaseVote<?>)){return false;}
|
|
|
+
|
|
|
+ BaseVote<?> m = (BaseVote<?>) o;
|
|
|
+ if(m.getId() == null){return false;}
|
|
|
+
|
|
|
+ return m.getId().compareTo(this.getId()) == 0;
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public boolean save() {
|
|
|
+ boolean saved = super.save();
|
|
|
+ if (saved) { MessageKit.sendMessage(ACTION_ADD, this); }
|
|
|
+ return saved;
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public boolean delete() {
|
|
|
+ boolean deleted = super.delete();
|
|
|
+ if (deleted) { MessageKit.sendMessage(ACTION_DELETE, this); }
|
|
|
+ return deleted;
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public boolean deleteById(Object idValue) {
|
|
|
+ boolean deleted = super.deleteById(idValue);
|
|
|
+ if (deleted) { MessageKit.sendMessage(ACTION_DELETE, this); }
|
|
|
+ return deleted;
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public boolean update() {
|
|
|
+ boolean update = super.update();
|
|
|
+ if (update) { MessageKit.sendMessage(ACTION_UPDATE, this); }
|
|
|
+ return update;
|
|
|
+ }
|
|
|
+
|
|
|
+ public void setId(BigInteger id) {
|
|
|
+ set("id", id);
|
|
|
+ }
|
|
|
+
|
|
|
+ public BigInteger getId() {
|
|
|
+ Object id = get("id");
|
|
|
+ if (id == null)
|
|
|
+ return null;
|
|
|
+
|
|
|
+ return id instanceof BigInteger ? (BigInteger)id : new BigInteger(id.toString());
|
|
|
+ }
|
|
|
+
|
|
|
+ public void setCommentId(BigInteger commentId) {
|
|
|
+ set("comment_id", commentId);
|
|
|
+ }
|
|
|
+
|
|
|
+ public BigInteger getCommentId() {
|
|
|
+ return get("comment_id");
|
|
|
+ }
|
|
|
+
|
|
|
+ public void setUserId(BigInteger userId) {
|
|
|
+ set("user_id", userId);
|
|
|
+ }
|
|
|
+
|
|
|
+ public BigInteger getUserId() {
|
|
|
+ return get("user_id");
|
|
|
+ }
|
|
|
+
|
|
|
+ public void setUuUserId(BigInteger uuUserId) {
|
|
|
+ set("uu_user_id", uuUserId);
|
|
|
+ }
|
|
|
+
|
|
|
+ public BigInteger getUuUserId() {
|
|
|
+ return get("uu_user_id");
|
|
|
+ }
|
|
|
+
|
|
|
+ public void setUp(java.lang.Boolean up) {
|
|
|
+ set("up", up);
|
|
|
+ }
|
|
|
+
|
|
|
+ public java.lang.Boolean getUp() {
|
|
|
+ return get("up");
|
|
|
+ }
|
|
|
+
|
|
|
+ public void setDown(java.lang.Boolean down) {
|
|
|
+ set("down", down);
|
|
|
+ }
|
|
|
+
|
|
|
+ public java.lang.Boolean getDown() {
|
|
|
+ return get("down");
|
|
|
+ }
|
|
|
+
|
|
|
+ public void setStatus(java.lang.String status) {
|
|
|
+ set("status", status);
|
|
|
+ }
|
|
|
+
|
|
|
+ public java.lang.String getStatus() {
|
|
|
+ return get("status");
|
|
|
+ }
|
|
|
+
|
|
|
+ public void setIp(java.lang.String ip) {
|
|
|
+ set("ip", ip);
|
|
|
+ }
|
|
|
+
|
|
|
+ public java.lang.String getIp() {
|
|
|
+ return get("ip");
|
|
|
+ }
|
|
|
+
|
|
|
+ public void setAgent(java.lang.String agent) {
|
|
|
+ set("agent", agent);
|
|
|
+ }
|
|
|
+
|
|
|
+ public java.lang.String getAgent() {
|
|
|
+ return get("agent");
|
|
|
+ }
|
|
|
+
|
|
|
+ public void setCreated(java.util.Date created) {
|
|
|
+ set("created", created);
|
|
|
+ }
|
|
|
+
|
|
|
+ public java.util.Date getCreated() {
|
|
|
+ return get("created");
|
|
|
+ }
|
|
|
+
|
|
|
+}
|