BrightSide Workbench Full Report + Source Code
PublicationMigration.java
Go to the documentation of this file.
1 /*
2  * TurrĂ³ i Cutiller Foundation. License notice.
3  * Copyright (C) 2023 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.publication.command;
20 
21 import java.util.concurrent.TimeUnit;
22 import java.util.concurrent.locks.ReentrantLock;
23 import java.util.stream.Stream;
24 import org.turro.jpa.Dao;
25 import org.turro.jpa.DaoUtil;
26 import org.turro.log.WebLoggers;
27 import org.turro.publication.db.PublicationPU;
28 import org.turro.publication.entity.PublicationCategory;
29 import org.turro.publication.entity.PublicationConstraint;
30 import org.turro.string.Strings;
31 
36 public class PublicationMigration implements Runnable {
37 
38  public static void migrate() {
40  if(migration.isNecessary()) new Thread(migration, "ElephantPublication migration").start();
41  }
42 
43  private boolean isNecessary() {
44  return DaoUtil.isEmpty(dao, "PublicationConstraint");
45  }
46 
47  private void start() {
48  try(Stream<PublicationCategory> categories = new PublicationPU().stream(PublicationCategory.class, "select p from PublicationCategory p")) {
49  categories.forEach((category) -> {
50  if(!Strings.isBlank(category.getConditionedBy())) {
51  PublicationConstraint constraint = new PublicationConstraint();
52  constraint.setPublicationCategory(category);
53  constraint.setKeyId(category.getConditionedBy());
54  constraint.setOptional(true);
55  category.getConstraints().add(constraint);
56  dao.saveObject(category);
57  }
58  });
59  }
60  }
61 
62  private final Dao dao;
63 
64  private PublicationMigration() {
65  this.dao = new PublicationPU();
66  }
67 
68  private static final ReentrantLock LOCK = new ReentrantLock();
69 
70  @Override
71  public void run() {
72  if(!LOCK.isLocked()) {
73  try {
74  if(LOCK.tryLock(0, TimeUnit.SECONDS)) {
75  try {
76  if(isNecessary()) {
77  start();
78  }
79  } finally {
80  LOCK.unlock();
81  }
82  }
83  } catch (InterruptedException ex) {
84  WebLoggers.severe(this).exception(ex).log();
85  }
86  }
87  }
88 
89 }
static boolean isEmpty(Dao dao, String table)
Definition: DaoUtil.java:35
static WebLoggers severe(Object entity)
Definition: WebLoggers.java:51
WebLoggers exception(Throwable throwable)
Definition: WebLoggers.java:29