BrightSide Workbench Full Report + Source Code
EntityWebUrls.java
Go to the documentation of this file.
1 /*
2  * TurrĂ³ i Cutiller Foundation. License notice.
3  * Copyright (C) 2018 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.jpa.entity;
20 
21 import org.turro.elephant.db.ElephantPU;
22 import org.turro.elephant.db.WhereClause;
23 import org.turro.elephant.entities.db.EntityWebUrl;
24 import org.turro.entities.Entities;
25 import org.turro.jpa.Dao;
26 import org.turro.util.CompareUtil;
27 
32 public class EntityWebUrls {
33 
34  public static Object getEntity(String entityRoot, String url) {
35  return Entities.getController(getEntityPath(entityRoot, url)).getEntity();
36  }
37 
38  public static String getEntityPath(String entityRoot, String url) {
39  if(url.contains("?")) {
40  url = url.substring(0, url.indexOf("?"));
41  }
42  Dao dao = new ElephantPU();
43  WhereClause wc = new WhereClause();
44  wc.addClause("select w.entityPath from EntityWebUrl w");
45  wc.addClause("where w.entityUrl = :url");
46  wc.addNamedValue("url", url);
47  wc.addClause("and w.entityPath like :path");
48  wc.addNamedValue("path", entityRoot + "%");
49  return (String) dao.getSingleResultOrNull(wc);
50  }
51 
52  public static String getUrlFromEntity(Object entity) {
53  return getUrl(Entities.getController(entity).getPath());
54  }
55 
56  public static String getUrl(String entityPath) {
57  Dao dao = new ElephantPU();
58  WhereClause wc = new WhereClause();
59  wc.addClause("select w.entityUrl from EntityWebUrl w");
60  wc.addClause("where w.entityPath = :path");
61  wc.addNamedValue("path", entityPath);
62  return (String) dao.getSingleResultOrNull(wc);
63  }
64 
65  public static EntityWebUrl addWebUrl(String entityPath, String url) {
66  removeEntityUrl(entityPath);
67  EntityWebUrl ewu = new EntityWebUrl();
68  ewu.setEntityPath(entityPath);
69  ewu.setEntityUrl(url);
70  return new ElephantPU().saveObject(ewu);
71  }
72 
73  public static void removeEntityUrl(String entityPath) {
74  Dao dao = new ElephantPU();
75  WhereClause wc = new WhereClause();
76  wc.addClause("delete from EntityWebUrl");
77  wc.addClause("where entityPath = :path");
78  wc.addNamedValue("path", entityPath);
79  dao.executeUpdate(wc);
80  }
81 
82  public static boolean existsEntity(String entityPath) {
83  Dao dao = new ElephantPU();
84  WhereClause wc = new WhereClause();
85  wc.addClause("select w from EntityWebUrl w");
86  wc.addClause("where w.entityPath = :path");
87  wc.addNamedValue("path", entityPath);
88  return !dao.getResultList(wc).isEmpty();
89  }
90 
91  public static boolean existsAnyOf(String entityPath) {
92  Dao dao = new ElephantPU();
93  WhereClause wc = new WhereClause();
94  wc.addClause("select count(w) from EntityWebUrl w");
95  wc.addClause("where w.entityPath like :path");
96  wc.addNamedValue("path", entityPath + "%");
97  return ((Long) dao.getSingleResultOrNull(wc)) > 0;
98  }
99 
100  public static boolean sameURL(String url1, String url2) {
101  if(url1.endsWith("/")) url1 = url1.substring(0, url1.length() - 1);
102  if(url2.endsWith("/")) url2 = url2.substring(0, url2.length() - 1);
103  return CompareUtil.compare(url1, url2) == 0;
104  }
105 
106  private EntityWebUrls() {
107  }
108 
109 }
void addNamedValue(String name, Object value)
static IElephantEntity getController(String path)
Definition: Entities.java:78
int executeUpdate(String query)
Definition: Dao.java:463
Object getSingleResultOrNull(SqlClause sc)
Definition: Dao.java:419
static boolean existsAnyOf(String entityPath)
static void removeEntityUrl(String entityPath)
static EntityWebUrl addWebUrl(String entityPath, String url)
static boolean existsEntity(String entityPath)
static String getUrlFromEntity(Object entity)
static Object getEntity(String entityRoot, String url)
static boolean sameURL(String url1, String url2)
static String getUrl(String entityPath)
static String getEntityPath(String entityRoot, String url)