BrightSide Workbench Full Report + Source Code
DossierPU.java
Go to the documentation of this file.
1 /*
2  * TurrĂ³ i Cutiller Foundation. License notice.
3  * Copyright (C) 2011 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 package org.turro.dossier.db;
19 
20 import java.util.List;
21 import javax.persistence.EntityManager;
22 import javax.persistence.EntityManagerFactory;
23 import org.turro.string.Strings;
24 import org.turro.dossier.entity.Category;
25 import org.turro.dossier.entity.Dossier;
26 import org.turro.dossier.entity.Issue;
27 import org.turro.dossier.entity.IssueComment;
28 import org.turro.dossier.entity.IssueParticipant;
29 import org.turro.dossier.entity.Participant;
30 import org.turro.dossier.entity.ProjectGrant;
31 import org.turro.elephant.db.WhereClause;
32 import org.turro.jpa.Dao;
33 import org.turro.jpa.DaoFactory;
34 import org.turro.jpa.entity.EntityWebUrls;
35 import org.turro.version.Module;
36 import org.turro.version.Version;
37 
42 public class DossierPU extends Dao {
43 
44  private static final DaoFactory factory = new DaoFactory();
45 
46  @Override
47  protected EntityManagerFactory getFactory() {
48  return factory.getEmf();
49  }
50 
51  @Override
52  protected EntityManager createEntityManager() {
53  return factory.createEntityManager("dossierPU", "Dossier");
54  }
55 
56  @Override
57  protected EntityManager createCachedEntityManager() {
58  return factory.createCachedEntityManager("dossierPU", "Dossier");
59  }
60 
61  @Override
62  public String getPath(Object object) {
63  return getObjectPath(object);
64  }
65 
66  public static String getObjectPath(Object object) {
67  if(object instanceof Dossier) {
68  return "/dossier/" + ((Dossier) object).getId();
69  } else if(object instanceof Issue) {
70  return "/issue/" + ((Issue) object).getId();
71  } else if(object instanceof IssueComment) {
72  return "/issue-comment/" + ((IssueComment) object).getId();
73  } else if(object instanceof Participant) {
74  return "/dossier-participant/" + ((Participant) object).getId();
75  } else if(object instanceof IssueParticipant) {
76  return "/issue-participant/" + ((IssueParticipant) object).getId();
77  } else if(object instanceof Category) {
78  return "/dossier-category/" + ((Category) object).getId();
79  } else if(object instanceof ProjectGrant) {
80  return "/project-grant/" + ((ProjectGrant) object).getId();
81  }
82  return null;
83  }
84 
85  public static String getObjectExtendedPath(Object object) {
86  if(object instanceof Dossier) {
87  return createCategoryPath(((Dossier) object).getCategory());
88  } else if(object instanceof Issue) {
89  return getObjectPath(((Issue) object).getDossier()) + createCategoryPath(((Issue) object).getDossier().getCategory());
90  } else if(object instanceof Category) {
91  return createCategoryPath(((Category) object).getParent());
92  }
93  return "";
94  }
95 
96  public static Module getModule() {
97  return new Module(new Version("1.9.5"), "BrightSide Dossier");
98  }
99 
100  public static void convertUrls() {
101  if(!EntityWebUrls.existsAnyOf("/dossier")) {
102  Dao dao = new DossierPU();
103  WhereClause wc = new WhereClause();
104  wc.addClause("select d.id from Dossier as d");
105  List<Long> list = (List<Long>) dao.getResultList(wc);
106  if(!list.isEmpty()) {
107  for(Long id : list) {
108  if(!EntityWebUrls.existsEntity("/dossier/" + id)) {
109  Dossier d = dao.find(Dossier.class, id);
110  if(d != null) {
111  String url = Strings.unpunctuateKeepPath(d.getCategory().getPath()).toLowerCase() +
112  "/" + d.getId() +
113  "/" + Strings.unpunctuate(d.getDescription()).toLowerCase();
114  EntityWebUrls.addWebUrl("/dossier/" + id, url);
115  }
116  }
117  }
118  }
119  }
120  }
121 
122  private static String createCategoryPath(Category category) {
123  if(category != null) {
124  String path = "/dossier-category/" + category.getId();
125  while(category.getParent() != null) {
126  category = category.getParent();
127  path += "/dossier-category/" + category.getId();
128  }
129  return path;
130  }
131  return "";
132  }
133 
134 }
static String getObjectPath(Object object)
Definition: DossierPU.java:66
EntityManager createCachedEntityManager()
Definition: DossierPU.java:57
EntityManager createEntityManager()
Definition: DossierPU.java:52
EntityManagerFactory getFactory()
Definition: DossierPU.java:47
String getPath(Object object)
Definition: DossierPU.java:62
static String getObjectExtendedPath(Object object)
Definition: DossierPU.java:85
EntityManagerFactory getEmf()
Definition: DaoFactory.java:89
EntityManager createEntityManager(String pu, String conf)
Definition: DaoFactory.java:54
synchronized EntityManager createCachedEntityManager(String pu, String conf)
Definition: DaoFactory.java:64
static boolean existsAnyOf(String entityPath)
static EntityWebUrl addWebUrl(String entityPath, String url)
static boolean existsEntity(String entityPath)