001/* 002 * Stallion Core: A Modern Web Framework 003 * 004 * Copyright (C) 2015 - 2016 Stallion Software LLC. 005 * 006 * This program is free software: you can redistribute it and/or modify it under the terms of the 007 * GNU General Public License as published by the Free Software Foundation, either version 2 of 008 * the License, or (at your option) any later version. This program is distributed in the hope that 009 * it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of 010 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public 011 * License for more details. You should have received a copy of the GNU General Public License 012 * along with this program. If not, see <http://www.gnu.org/licenses/gpl-2.0.html>. 013 * 014 * 015 * 016 */ 017 018package io.stallion.dataAccess; 019 020 021 022import com.fasterxml.jackson.annotation.JsonIgnore; 023import com.fasterxml.jackson.annotation.JsonView; 024import io.stallion.utils.GeneralUtils; 025import io.stallion.utils.json.RestrictedViews; 026 027import javax.persistence.Column; 028import javax.persistence.Transient; 029import java.util.HashMap; 030import java.util.Map; 031 032import static io.stallion.utils.Literals.empty; 033 034/** 035 * Base implementation of the Model interface. 036 */ 037public class ModelBase implements Model { 038 039 public static Map<String, Object> meta = new HashMap<>(); 040 041 // Weird name to not conflict with JPA 042 @Transient 043 private Long id; 044 private Boolean deleted = false; 045 private Boolean isNewInsert = false; 046 private String _bucket; 047 private Long lastModifiedMillis = 0L; 048 049 050 public static Map<String, Object> getMeta() { 051 return meta; 052 } 053 054 @JsonView(RestrictedViews.Public.class) 055 public Long getId() { 056 return id; 057 } 058 059 public <Y extends Model> Y setId(Long id) { 060 this.id = id; 061 return (Y)this; 062 } 063 064 @JsonIgnore 065 public ModelController getController() { 066 return DataAccessRegistry.instance().get(getBucket()); 067 } 068 069 070 @Column 071 @JsonView(RestrictedViews.Member.class) 072 public Boolean getDeleted() { 073 return deleted; 074 } 075 076 public void setDeleted(Boolean deleted) { 077 this.deleted = deleted; 078 } 079 080 @JsonIgnore 081 @Transient 082 public Boolean getIsNewInsert() { 083 return isNewInsert; 084 } 085 086 public void setIsNewInsert(Boolean isNewInsert) { 087 this.isNewInsert = isNewInsert; 088 } 089 090 /* And id made from concatenating the bucket and the object id */ 091 @JsonIgnore 092 public String getCompoundId() { 093 return GeneralUtils.slugify(getController().getBucket()) + "---" + GeneralUtils.slugify(getId().toString()); 094 } 095 096 @Override 097 public boolean equals(Object obj) { 098 if (!(obj instanceof Model)) { 099 return false; 100 } 101 Model m = (Model)obj; 102 if (m.getId() == null || getId() == null) { 103 return false; 104 } 105 return getId().equals(m.getId()); 106 } 107 108 @Override 109 public int hashCode() { 110 if (empty(getId())) { 111 return super.hashCode(); 112 } else { 113 return getId().hashCode(); 114 } 115 } 116 117 @JsonIgnore 118 @Transient 119 public String getBucket() { 120 return _bucket; 121 } 122 123 public Model setBucket(String bucket) { 124 this._bucket = bucket; 125 return this; 126 } 127 128 @Override 129 public Long getLastModifiedMillis() { 130 return lastModifiedMillis; 131 } 132 133 public <Y extends Model> Y setLastModifiedMillis(Long lastModifiedMillis) { 134 this.lastModifiedMillis = lastModifiedMillis; 135 return (Y)this; 136 } 137}