BrightSide Workbench Full Report + Source Code
ElephantPU.java
Go to the documentation of this file.
1 /*
2  * TurrĂ³ i Cutiller Foundation. License notice.
3  * Copyright (C) 2017 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.elephant.db;
20 
21 import javax.persistence.EntityManager;
22 import javax.persistence.EntityManagerFactory;
23 import org.turro.action.IElephantCloseable;
24 import org.turro.annotation.ElephantCloseable;
25 import org.turro.elephant.entities.db.EntityParticipation;
26 import org.turro.elephant.entities.db.Poll;
27 import org.turro.elephant.entities.db.Post;
28 import org.turro.elephant.entities.db.Topic;
29 import org.turro.jpa.Dao;
30 import org.turro.jpa.DaoFactory;
31 import org.turro.version.Module;
32 import org.turro.version.Version;
33 
38 @ElephantCloseable
39 public class ElephantPU extends Dao implements IElephantCloseable {
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("elephantPU", "Elephant");
51  }
52 
53  @Override
54  protected EntityManager createCachedEntityManager() {
55  return factory.createCachedEntityManager("elephantPU", "Elephant");
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 Poll) {
65  return "/poll/" + ((Poll) object).getId();
66  } else if(object instanceof Topic) {
67  return "/topic/" + ((Topic) object).getId();
68  } else if(object instanceof Post) {
69  return "/post/" + ((Post) object).getId();
70  } else if(object instanceof EntityParticipation) {
71  return "/entity-participation/" + ((EntityParticipation) object).complexId().build();
72  }
73  return null;
74  }
75 
76  public static Module getModule() {
77  return new Module(new Version("0"), "Elephant");
78  }
79 
80  @Override
81  public void closeElephant() {
82 // try {
83 // DriverManager.getConnection("jdbc:derby:;shutdown=true");
84 // } catch (SQLException ex) {
85 // if(!"XJ015".equals(ex.getSQLState())) {
86 // Logger.getLogger(ElephantPU.class.getName()).log(Level.SEVERE, ElephantContext.logMsg(null), ex);
87 // }
88 // }
89  }
90 
91 }
92 
EntityManagerFactory getFactory()
Definition: ElephantPU.java:44
EntityManager createEntityManager()
Definition: ElephantPU.java:49
String getPath(Object object)
Definition: ElephantPU.java:59
static String getObjectPath(Object object)
Definition: ElephantPU.java:63
EntityManager createCachedEntityManager()
Definition: ElephantPU.java:54
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