View Javadoc
1   /*
2    * Copyright (C) 2015 eXo Platform SAS.
3    *
4    * This is free software; you can redistribute it and/or modify it
5    * under the terms of the GNU Lesser General Public License as
6    * published by the Free Software Foundation; either version 2.1 of
7    * the License, or (at your option) any later version.
8    *
9    * This software is distributed in the hope that it will be useful,
10   * but WITHOUT ANY WARRANTY; without even the implied warranty of
11   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12   * Lesser General Public License for more details.
13   *
14   * You should have received a copy of the GNU Lesser General Public
15   * License along with this software; if not, write to the Free
16   * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
17   * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
18   */
19  
20  package org.exoplatform.social.core.jpa.updater.listener;
21  
22  import org.exoplatform.commons.persistence.impl.EntityManagerService;
23  import org.exoplatform.commons.utils.CommonsUtils;
24  import org.exoplatform.services.listener.Event;
25  import org.exoplatform.services.listener.Listener;
26  import org.exoplatform.social.core.jpa.storage.entity.MentionEntity;
27  import org.exoplatform.social.core.identity.model.Identity;
28  
29  import javax.persistence.EntityManager;
30  import javax.persistence.Query;
31  import java.util.List;
32  
33  /**
34   * @author <a href="mailto:tuyennt@exoplatform.com">Tuyen Nguyen The</a>.
35   */
36  public class IdentityReferenceUpdaterListener extends Listener<Identity, String> {
37    @Override
38    public void onEvent(Event<Identity, String> event) throws Exception {
39      EntityManagerService emService = CommonsUtils.getService(EntityManagerService.class);
40      EntityManager em = emService.getEntityManager();
41      if (em == null) {
42        return;
43      }
44  
45      boolean startTx = false;
46      try {
47        if (!em.getTransaction().isActive()) {
48          em.getTransaction().begin();
49          startTx = true;
50        }
51  
52        Identity identity = event.getSource();
53        String newId = event.getData();
54        String oldId = identity.getId();
55  
56        Query query;
57  
58        // Update activity poster
59        query = em.createNamedQuery("SocActivity.migratePosterId");
60        query.setParameter("newId", newId);
61        query.setParameter("oldId", oldId);
62        query.executeUpdate();
63  
64        // Activity owner
65        query = em.createNamedQuery("SocActivity.migrateOwnerId");
66        query.setParameter("newId", newId);
67        query.setParameter("oldId", oldId);
68        query.executeUpdate();
69  
70        //activity mention
71        query = em.createNamedQuery("SocMention.migrateMentionId");
72        query.setParameter("newId", newId);
73        query.setParameter("oldId", oldId);
74        query.executeUpdate();
75  
76        query = em.createNamedQuery("SocMention.selectMentionByOldId", MentionEntity.class);
77        query.setParameter("oldId", oldId + "@%");
78        List<MentionEntity> list = query.getResultList();
79        if (list != null && list.size() > 0) {
80          for (MentionEntity m : list) {
81            String mentionId = m.getMentionId();
82            mentionId = mentionId.replace(oldId, newId);
83            m.setMentionId(mentionId);
84            em.merge(m);
85          }
86        }
87  
88  
89        //TODO: Can not use the JPQL for this?
90        // Activity Liker
91        query = em.createNativeQuery("UPDATE SOC_ACTIVITY_LIKERS SET LIKER_ID = ? WHERE LIKER_ID = ?");
92        query.setParameter(1, newId);
93        query.setParameter(2, oldId);
94        query.executeUpdate();
95  
96        // Stream Item
97        /*query = em.createNamedQuery("SocStreamItem.migrateOwner");
98        query.setParameter("newId", newId);
99        query.setParameter("oldId", oldId);
100       query.executeUpdate();*/
101     } finally {
102       if (startTx) {
103         em.getTransaction().commit();
104       }
105     }
106   }
107 }