BrightSide Workbench Full Report + Source Code
ForumMigration.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.forum;
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.elephant.entities.db.Topic;
29 import org.turro.jpa.Dao;
30 import org.turro.jpa.DaoUtil;
31 
36 public class ForumMigration implements Runnable {
37 
38  private final Dao oldForumDao, newForumDao;
39  private String currentEntity = null;
40  private Topic currentTopic = null;
41 
42  public static void migrate() {
43  ForumMigration migration = new ForumMigration(Dao.getDaoByPU("contactsPU"));
44  if(migration.isNecessary()) new Thread(migration, "ElephantForum migration").start();
45  }
46 
47  private boolean isNecessary() {
48  return DaoUtil.isEmpty(newForumDao, "Topic");
49  }
50 
51  private void start() {
52  try(Stream<CommentItEntry> comments = oldForumDao.stream(CommentItEntry.class, getSelect())) {
53  comments.forEach((comment) -> {
54  if(comment.getEntityPath().equals(currentEntity)) {
55  comment.migratePost(newForumDao, currentTopic);
56  } else {
57  currentTopic = comment.migrateTopic(newForumDao);
58  currentEntity = comment.getEntityPath();
59  }
60  });
61  }
62  }
63 
64  private String getSelect() {
65  return "select new org.turro.forum.CommentItEntry(t.path, t.creator.id, t.dateCreation, t.body) " +
66  "from CommentIt t where t.creator is not null and t.path like '/dossier/%' " +
67  "order by t.path";
68  }
69 
70  private ForumMigration(Dao oldForumDao) {
71  this.oldForumDao = oldForumDao;
72  this.newForumDao = new ElephantPU();
73  }
74 
75  private static final ReentrantLock LOCK = new ReentrantLock();
76 
77  @Override
78  public void run() {
79  if(!LOCK.isLocked()) {
80  try {
81  if(LOCK.tryLock(0, TimeUnit.SECONDS)) {
82  try {
83  if(isNecessary()) {
84  start();
85  }
86  } finally {
87  LOCK.unlock();
88  }
89  }
90  } catch (InterruptedException ex) {
91  Logger.getLogger(ForumMigration.class.getName()).log(Level.SEVERE, ElephantContext.logMsg(null), ex);
92  }
93  }
94  }
95 
96 }
static boolean isEmpty(Dao dao, String table)
Definition: DaoUtil.java:35
static Dao getDaoByPU(String pu)
Definition: Dao.java:654