BrightSide Workbench Full Report + Source Code
PublicationPU.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.publication.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.action.queue.NotificationCategory;
25 import org.turro.elephant.db.WhereClause;
26 import org.turro.jpa.Dao;
27 import org.turro.jpa.DaoFactory;
28 import org.turro.jpa.entity.EntityWebUrls;
29 import org.turro.publication.entity.Newsletter;
30 import org.turro.publication.entity.Publication;
31 import org.turro.publication.entity.PublicationCategory;
32 import org.turro.version.Module;
33 import org.turro.version.Version;
34 
39 public class PublicationPU extends Dao {
40 
41  private static final DaoFactory factory = new DaoFactory();
42 
43  @Override
44  protected EntityManagerFactory getFactory() {
45  return factory.getEmf();
46  }
47 
48  @Override
49  protected EntityManager createEntityManager() {
50  return factory.createEntityManager("publicationPU", "Publication");
51  }
52 
53  @Override
54  protected EntityManager createCachedEntityManager() {
55  return factory.createCachedEntityManager("publicationPU", "Publication");
56  }
57 
58  @Override
59  public String getPath(Object object) {
60  return getObjectPath(object);
61  }
62 
63  public static String getObjectPath(Object object) {
64  if(object instanceof Publication) {
65  return "/publication/" + ((Publication) object).getId();
66  } else if(object instanceof PublicationCategory) {
67  return "/pub-category/" + ((PublicationCategory) object).getId();
68  } else if(object instanceof Newsletter) {
69  return "/newsletter/" + ((Newsletter) object).getId();
70  } else if(object instanceof NotificationCategory) {
71  return "/mail-category/" + ((NotificationCategory) object).getIdCategory();
72  }
73  return null;
74  }
75 
76  public static String getObjectExtendedPath(Object object) {
77  if(object instanceof Publication) {
78  return getObjectPath(((Publication) object).getPublicationCategory());
79  }
80  return "";
81  }
82 
83  public static Module getModule() {
84  return new Module(new Version("1.5.0"), "BrightSide Publication");
85  }
86 
87  public static void convertUrls() {
88  if(!EntityWebUrls.existsAnyOf("/publication")) {
89  Dao dao = new PublicationPU();
90  WhereClause wc = new WhereClause();
91  wc.addClause("select p.id from Publication as p");
92  List<Long> list = (List<Long>) dao.getResultList(wc);
93  if(!list.isEmpty()) {
94  for(Long id : list) {
95  if(!EntityWebUrls.existsEntity("/publication/" + id)) {
96  Publication p = dao.find(Publication.class, id);
97  if(p != null) {
99  "/publication/" + id,
100  "/" + p.getId() + "/" + Strings.unpunctuate(p.getTitle()).toLowerCase());
101  }
102  }
103  }
104  }
105  }
106  }
107 
108 }
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)
static String getObjectExtendedPath(Object object)
static String getObjectPath(Object object)