BrightSide Workbench Full Report + Source Code
TagMigration.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.tags;
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.db.WhereClause;
29 import org.turro.jpa.Dao;
30 import org.turro.jpa.DaoUtil;
31 
36 public class TagMigration implements Runnable {
37 
38  private final Dao oldTagDao, newTagDao;
39 
40  public static void clean() {
41  WhereClause wc = new WhereClause();
42  wc.addClause("delete from Tag");
43  wc.addClause("where entityPath like '%/null'");
44  new ElephantPU().executeUpdate(wc);
45  }
46 
47  public static void migrate() {
48  TagMigration migration = new TagMigration(Dao.getDaoByPU("contactsPU"));
49  if(migration.isNecessary()) new Thread(migration, "ElephantTag migration").start();
50  }
51 
52  private boolean isNecessary() {
53  return DaoUtil.isEmpty(newTagDao, "Tag") && !DaoUtil.isEmpty(oldTagDao, "TagIt");
54  }
55 
56  private void start() {
57  try(Stream<OldTagEntry> tags = oldTagDao.stream(OldTagEntry.class, getSelect())) {
58  tags.forEach((tag) -> {
59  tag.migrate(newTagDao);
60  });
61  }
62  }
63 
64  private String getSelect() {
65  return "select new org.turro.tags.OldTagEntry(t.name, t.path) from TagIt t";
66  }
67 
68  private TagMigration(Dao oldTagDao) {
69  this.oldTagDao = oldTagDao;
70  this.newTagDao = new ElephantPU();
71  }
72 
73  private static final ReentrantLock LOCK = new ReentrantLock();
74 
75  @Override
76  public void run() {
77  if(!LOCK.isLocked()) {
78  try {
79  if(LOCK.tryLock(0, TimeUnit.SECONDS)) {
80  try {
81  if(isNecessary()) {
82  start();
83  }
84  } finally {
85  LOCK.unlock();
86  }
87  }
88  } catch (InterruptedException ex) {
89  Logger.getLogger(TagMigration.class.getName()).log(Level.SEVERE, ElephantContext.logMsg(null), ex);
90  }
91  }
92  }
93 
94 }
static boolean isEmpty(Dao dao, String table)
Definition: DaoUtil.java:35
int executeUpdate(String query)
Definition: Dao.java:463
static Dao getDaoByPU(String pu)
Definition: Dao.java:654