BrightSide Workbench Full Report + Source Code
AxCategory.java
Go to the documentation of this file.
1 /*
2  * TurrĂ³ i Cutiller Foundation. License notice.
3  * Copyright (C) 2022 Lluis TurrĂ³ Cutiller <http://www.turro.org/>
4  *
5  * This program is free software: you can redistribute it and/or modify
6  * it under the terms of the GNU Affero General Public License as published by
7  * the Free Software Foundation, either version 3 of the License, or
8  * (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  * GNU Affero General Public License for more details.
14  *
15  * You should have received a copy of the GNU Affero General Public License
16  * along with this program. If not, see <http://www.gnu.org/licenses/>.
17  */
18 
19 package org.turro.alliance.db.entities;
20 
21 import java.io.Serializable;
22 import java.util.Map;
23 import javax.json.JsonValue;
24 import javax.persistence.Entity;
25 import javax.persistence.Id;
26 import javax.persistence.ManyToOne;
27 import javax.persistence.Transient;
28 import org.turro.string.Strings;
29 import org.turro.alliance.db.AlliancePU;
30 import org.turro.elephant.db.IdUtils;
31 import org.turro.jpa.entity.IDaoEntity;
32 import org.turro.json.IJSONizable;
33 import org.turro.math.Zero;
34 import org.turro.path.UniquePath;
35 import org.turro.util.Chars;
36 
41 @Entity
42 public class AxCategory implements Serializable, IDaoEntity, IJSONizable {
43 
44  @Id private Long categoryId;
45 
46  private String name;
47 
48  private String uniquePath;
49 
50  @Transient
51  private String pathName;
52 
53  @ManyToOne
54  private AxCategory parent;
55 
56  public Long getCategoryId() {
57  return categoryId;
58  }
59 
60  public void setCategoryId(Long categoryId) {
61  this.categoryId = categoryId;
62  }
63 
64  public String getName() {
65  return name;
66  }
67 
68  public void setName(String name) {
69  this.name = name;
70  }
71 
72  public String getUniquePath() {
73  return uniquePath;
74  }
75 
76  public void setUniquePath(String uniquePath) {
77  this.uniquePath = uniquePath;
78  }
79 
80  public AxCategory getParent() {
81  return parent;
82  }
83 
84  public void setParent(AxCategory parent) {
85  this.parent = parent;
86  }
87 
88  public String getPathName() {
89  if(pathName == null) {
90  AxCategory current = getParent();
91  StringBuilder sb = new StringBuilder(getName());
92  while(current != null) {
93  sb.insert(0, Chars.forward().spaced().toString())
94  .insert(0, current.getName());
95  current = current.getParent();
96  }
97  pathName = sb.toString();
98  }
99  return pathName;
100  }
101 
102  public void setPathName(String pathName) {
103  this.pathName = pathName;
104  }
105 
106  /* IDaoEntity */
107 
108  @Override
109  public Object entityId() {
110  return categoryId;
111  }
112 
113  @Override
114  public boolean isEmpty() {
115  return Strings.isBlank(name);
116  }
117 
118  @Override
119  public void prepareSave() {
120  IDaoEntity.super.prepareSave();
121  if(Zero.orNull(categoryId)) {
122  categoryId = IdUtils.getNewLongIdFromLong(new AlliancePU(), "AxCategory", "categoryId");
123  }
124  uniquePath = UniquePath.getPath(this, c -> Long.toString(c.getCategoryId()), c -> c.getParent());
125  if(Strings.isBlank(uniquePath)) parent = null;
126  }
127 
128  /* JSONizable */
129 
130  @Override
131  public String toJson() {
132  getPathName();
133  return toJson(this);
134  }
135 
136  @Override
137  public String toJson(Map<String, Object> properties) {
138  return toJson(this, properties);
139  }
140 
141  public static AxCategory fromJson(JsonValue value) {
142  return IJSONizable.fromJson(value.toString(), AxCategory.class);
143  }
144 
145 }
static AxCategory fromJson(JsonValue value)
void setUniquePath(String uniquePath)
Definition: AxCategory.java:76
String toJson(Map< String, Object > properties)
static long getNewLongIdFromLong(Dao dao, String table, String field)
Definition: IdUtils.java:33
static boolean orNull(Number value)
Definition: Zero.java:26