BrightSide Workbench Full Report + Source Code
AttachInitializer.java
Go to the documentation of this file.
1 /*
2  * TurrĂ³ i Cutiller Foundation. License notice.
3  * Copyright (C) 2020 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.entities;
20 
21 import org.turro.action.Contacts;
22 import org.turro.action.IElephantInitializer;
23 import org.turro.annotation.ElephantInitializer;
24 import org.turro.attach.db.AttachPU;
25 import org.turro.attach.entity.Attachment;
26 import org.turro.elephant.context.IConstructor;
27 import org.turro.jpa.Dao;
28 import org.turro.jpa.query.JpaCriteria;
29 import org.turro.jpa.query.JpaCriteriaUpdate;
30 import org.turro.jpa.query.JpaQuery;
31 import org.turro.jpa.query.JpaRoot;
32 import org.turro.log.WebLoggers;
33 import org.turro.plugin.contacts.IContact;
34 
39 @ElephantInitializer
40 public class AttachInitializer implements IElephantInitializer {
41 
42  @Override
43  public void initialize(IConstructor constructor) {
44  WebLoggers.info(this).message("substituteLoginsForIds started").log();
45  substituteLoginsForIds();
46  }
47 
48  private void substituteLoginsForIds() {
49  Dao dao = new AttachPU();
50  Dao execute = new AttachPU();
51  JpaCriteria jc = new JpaCriteria(dao);
52  JpaQuery<Attachment> jqa = jc.query(Attachment.class);
53  JpaRoot<Attachment> attachment = jqa.root();
54  jqa.select().where(jc.or(
55  jc.like(attachment.get("owner"), "%\\.%", '\\'),
56  jc.like(attachment.get("owner"), "%\\_%", '\\'),
57  jc.lt(jc.length(attachment.get("owner")), 30)));
58  dao.getResultList(jqa).forEach((attach) -> {
59  IContact contact = Contacts.getEmpty();
60  try {
61  contact = Contacts.getContact(contact.loadByLogin(attach.getOwner()));
62  if(contact.isValid()) {
63  JpaCriteriaUpdate<Attachment> juc = jc.createCriteriaUpdate(Attachment.class);
64  execute.executeUpdate(juc.set("owner", contact.getId())
65  .where(jc.equal(juc.field("id"), attach.getId())));
66  }
67  } catch(Exception ex) {}
68  });
69  }
70 
71 }
static IContact getContact(Object object)
Definition: Contacts.java:109
static IContact getEmpty()
Definition: Contacts.java:56
void initialize(IConstructor constructor)
Predicate or(Expression< Boolean > x, Expression< Boolean > y)
Predicate lt(Expression<? extends Number > x, Expression<? extends Number > y)
Expression< Integer > length(Expression< String > x)
Predicate like(Expression< String > x, Expression< String > pattern)
JpaQuery< E > select()
Definition: JpaQuery.java:42
WebLoggers message(String text, Object... parameters)
Definition: WebLoggers.java:34
static WebLoggers info(Object entity)
Definition: WebLoggers.java:43
Object loadByLogin(String login)