BrightSide Workbench Full Report + Source Code
ProfileMigration.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.contacts.profile;
20 
21 import java.util.stream.Stream;
22 import org.turro.contacts.BusinessRelation;
23 import org.turro.contacts.Connector;
24 import org.turro.contacts.Contact;
25 import org.turro.contacts.db.ContactsPU;
26 import org.turro.jpa.Dao;
27 import org.turro.jpa.DaoUtil;
28 import org.turro.www.directory.EditDataControl;
29 
34 public class ProfileMigration {
35 
36  public static void migrate() {
37  ProfileMigration migration = new ProfileMigration();
38  if(migration.isNecessary()) {
39  migration.start();
40  }
41  }
42 
43  private boolean isNecessary() {
44  Dao dao = new ContactsPU();
45  return (DaoUtil.isEmpty(dao, "Connector", "where x.description = ?", Profile.PHONE) &&
46  !DaoUtil.isEmpty(dao, "Connector", "where x.description = ?", EditDataControl.PHONE_DIRECTORY)) ||
47  (DaoUtil.isEmpty(dao, "Connector", "where x.description = ?", Profile.NETWORK) &&
48  !DaoUtil.isEmpty(dao, "Connector", "where x.description = ?", EditDataControl.NETWORK_DIRECTORY));
49  }
50 
51  private void start() {
52  Dao dao = new ContactsPU();
53  try(Stream<Connector> connectors = new ContactsPU().stream(Connector.class, "select c from Connector c")) {
54  connectors.forEach((connector) -> {
55  if(EditDataControl.PHONE_DIRECTORY.equals(connector.getDescription())) {
56  Connector con = new Connector();
57  con.setContact(connector.getContact());
58  con.setDescription(Profile.PHONE);
59  con.setValue(connector.getValue());
60  con.setOnlyOwner(false);
61  saveIfDontExists(dao, con);
62  } else if(EditDataControl.NETWORK_DIRECTORY.equals(connector.getDescription())) {
63  Connector con = new Connector();
64  con.setContact(connector.getContact());
65  con.setDescription(Profile.NETWORK);
66  con.setValue(connector.getValue());
67  con.setOnlyOwner(false);
68  saveIfDontExists(dao, con);
69  } else if(EditDataControl.POSITION_DIRECTORY.equals(connector.getDescription())) {
70  Contact c = connector.getContact();
71  if(c.getBusinessRelations().size() == 1) {
72  BusinessRelation br = c.getBusinessRelations().iterator().next();
73  br.setDescription(connector.getValue());
74  dao.saveObject(br);
75  }
76  }
77  });
78  }
79  }
80 
81  private void saveIfDontExists(Dao dao, Connector con) {
82  if(DaoUtil.isEmpty(dao, "Connector", "where x.description = ? and x.contact = ?", con.getDescription(), con.getContact())) {
83  dao.saveObject(con);
84  }
85  }
86 
87 }
static boolean isEmpty(Dao dao, String table)
Definition: DaoUtil.java:35