001package ball.persistence.entity; 002/*- 003 * ########################################################################## 004 * Persistence Implementation (Hibernate) 005 * $Id: JSONEntity.java 5285 2020-02-05 04:23:21Z ball $ 006 * $HeadURL: svn+ssh://svn.hcf.dev/var/spool/scm/repository.svn/ball-persistence/trunk/src/main/java/ball/persistence/entity/JSONEntity.java $ 007 * %% 008 * Copyright (C) 2016 - 2020 Allen D. Ball 009 * %% 010 * Licensed under the Apache License, Version 2.0 (the "License"); 011 * you may not use this file except in compliance with the License. 012 * You may obtain a copy of the License at 013 * 014 * http://www.apache.org/licenses/LICENSE-2.0 015 * 016 * Unless required by applicable law or agreed to in writing, software 017 * distributed under the License is distributed on an "AS IS" BASIS, 018 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 019 * See the License for the specific language governing permissions and 020 * limitations under the License. 021 * ########################################################################## 022 */ 023import ball.databind.JSONBean; 024import com.fasterxml.jackson.databind.JsonNode; 025import java.io.IOException; 026import javax.persistence.Column; 027import javax.persistence.Lob; 028import javax.persistence.MappedSuperclass; 029import lombok.EqualsAndHashCode; 030import lombok.Getter; 031import lombok.NoArgsConstructor; 032 033/** 034 * Abstract base class for {@link JSONBean} entities. 035 * 036 * {@bean.info} 037 * 038 * @author {@link.uri mailto:ball@hcf.dev Allen D. Ball} 039 * @version $Revision: 5285 $ 040 */ 041@MappedSuperclass 042@NoArgsConstructor @EqualsAndHashCode(callSuper = false) 043public class JSONEntity extends JSONBean { 044 private static final long serialVersionUID = -8802509358938963189L; 045 046 /** @serial */ 047 @Column @Lob 048 @Getter 049 protected String json = null; 050 051 protected void setJson(String string) { 052 synchronized (this) { 053 boolean modified = (node == null || (! string.equals(json))); 054 055 json = string; 056 057 if (modified) { 058 node = null; 059 060 if (json != null) { 061 try { 062 node = mapper.readTree(json); 063 } catch (IOException exception) { 064 throw new IllegalArgumentException(exception); 065 } 066 } 067 } 068 } 069 } 070 071 /** 072 * Method to get this {@link JSONEntity} as a {@link JsonNode}. 073 * 074 * @return The {@link JSONEntity} as a {@link JsonNode}. 075 */ 076 public JsonNode asJsonNode() { 077 JsonNode node = this.node; 078 079 return (node != null) ? node.deepCopy() : null; 080 } 081 082 @Override 083 public String toString() { 084 String string = getJson(); 085 086 return (string != null) ? string : super.toString(); 087 } 088}