BrightSide Workbench Full Report + Source Code
SystemLogMigration.java
Go to the documentation of this file.
1 /*
2  * TurrĂ³ i Cutiller Foundation. License notice.
3  * Copyright (C) 2021 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.log;
20 
21 import java.util.concurrent.TimeUnit;
22 import java.util.concurrent.locks.ReentrantLock;
23 import java.util.logging.Level;
24 import java.util.logging.Logger;
25 import java.util.stream.Stream;
26 import org.turro.elephant.context.ElephantContext;
27 import org.turro.elephant.db.ElephantPU;
28 import org.turro.jpa.Dao;
29 import org.turro.jpa.DaoUtil;
30 
35 public class SystemLogMigration implements Runnable {
36 
37  private final Dao oldLogDao, newLogDao;
38 
39  public static void migrate() {
40  SystemLogMigration migration = new SystemLogMigration(Dao.getDaoByPU("contactsPU"));
41  if(migration.isNecessary()) new Thread(migration, "ElephantLog migration").start();
42  }
43 
44  private boolean isNecessary() {
45  return DaoUtil.isEmpty(newLogDao, "SystemLog") && !DaoUtil.isEmpty(oldLogDao, "LogEntry");
46  }
47 
48  private void start() {
49  try(Stream<OldLogEntry> logs = oldLogDao.stream(OldLogEntry.class, getSelect())) {
50  logs.forEach((log) -> {
51  log.migrate(newLogDao);
52  });
53  }
54  }
55 
56  private String getSelect() {
57  return "select new org.turro.log.OldLogEntry("
58  + "case when l.logType = 'LOG_INFO' then 0 "
59  + "when l.logType = 'LOG_WARNING' then 1 "
60  + "when l.logType = 'LOG_SEVERE' then 2 "
61  + "end, "
62  + "l.path, l.idUser, l.name, l.dateLog, l.comment, l.data) from LogEntry l";
63  }
64 
65  private SystemLogMigration(Dao oldLogDao) {
66  this.oldLogDao = oldLogDao;
67  this.newLogDao = new ElephantPU();
68  }
69 
70  private static final ReentrantLock LOCK = new ReentrantLock();
71 
72  @Override
73  public void run() {
74  if(!LOCK.isLocked()) {
75  try {
76  if(LOCK.tryLock(0, TimeUnit.SECONDS)) {
77  try {
78  if(isNecessary()) {
79  start();
80  }
81  } finally {
82  LOCK.unlock();
83  }
84  }
85  } catch (InterruptedException ex) {
86  Logger.getLogger(SystemLogMigration.class.getName()).log(Level.SEVERE, ElephantContext.logMsg(null), ex);
87  }
88  }
89  }
90 
91 }
static boolean isEmpty(Dao dao, String table)
Definition: DaoUtil.java:35
static Dao getDaoByPU(String pu)
Definition: Dao.java:654