Project

General

Profile

Download (5.56 KB) Statistics
| Branch: | Tag: | Revision:
1
/**
2
* Copyright (C) 2016 EDIT
3
* European Distributed Institute of Taxonomy
4
* http://www.e-taxonomy.eu
5
*
6
* The contents of this file are subject to the Mozilla Public License Version 1.1
7
* See LICENSE.TXT at the top of this package for the full license terms.
8
*/
9
package eu.etaxonomy.cdm.io.operation;
10

    
11
import java.sql.SQLException;
12
import java.util.ArrayList;
13
import java.util.List;
14
import java.util.Map;
15
import java.util.Set;
16

    
17
import org.apache.log4j.Logger;
18
import org.hibernate.SQLQuery;
19
import org.springframework.stereotype.Component;
20
import org.springframework.transaction.TransactionStatus;
21

    
22
import eu.etaxonomy.cdm.common.monitor.DefaultProgressMonitor;
23
import eu.etaxonomy.cdm.common.monitor.IProgressMonitor;
24
import eu.etaxonomy.cdm.database.update.SortIndexUpdater;
25
import eu.etaxonomy.cdm.io.common.CdmImportBase;
26
import eu.etaxonomy.cdm.io.common.DefaultImportState;
27
import eu.etaxonomy.cdm.io.operation.config.SortIndexUpdaterConfigurator;
28

    
29
/**
30
 * @author k.luther
31
 * @since 08.07.2016
32
 *
33
 */
34
@Component
35
public class SortIndexUpdaterWrapper extends CdmImportBase<SortIndexUpdaterConfigurator, DefaultImportState<SortIndexUpdaterConfigurator>> {
36

    
37
    private static final long serialVersionUID = 1152526455024556637L;
38
    private static final Logger logger = Logger.getLogger(SortIndexUpdaterWrapper.class);
39

    
40

    
41
    @Override
42
    protected void doInvoke(DefaultImportState<SortIndexUpdaterConfigurator> state) {
43
        SortIndexUpdaterConfigurator config =  state.getConfig();
44
        SortIndexUpdater updater;
45

    
46
       // CaseType caseType = CaseType.caseTypeOfDatasource(config.getDestination());
47
        IProgressMonitor  monitor = DefaultProgressMonitor.NewInstance();
48

    
49
        if (config.isDoTaxonNode()){
50
            updater = SortIndexUpdater.NewInstance("Update taxonnode sortindex", "TaxonNode", "parent_id", "sortIndex", true);
51

    
52
            update(updater, monitor);
53

    
54

    
55
        }
56
        if (config.isDoFeatureNode()){
57
            updater = SortIndexUpdater.NewInstance("Update Feature node sortindex", "FeatureNode", "parent_id", "sortIndex", true);
58
            update(updater, monitor);
59
        }
60
        if (config.isDoPolytomousKeyNode()){
61
            updater = SortIndexUpdater.NewInstance("Update Polytomouskey node sortindex", "PolytomousKeyNode", "parent_id", "sortindex", true);
62
           update(updater, monitor);
63
        }
64
        return;
65

    
66
    }
67

    
68
    private void update(SortIndexUpdater updater,  IProgressMonitor  monitor){
69
        try {
70
            TransactionStatus tx;
71
            tx = startTransaction();
72
            String query = updater.createIndexMapQuery();
73
            SQLQuery sqlQuery = getAgentService().getSession().createSQLQuery(query);
74

    
75
            List data = sqlQuery.list();
76

    
77
           List<Integer[]> result = new ArrayList<Integer[]>();
78
           int id;
79
           int parentId;
80
           Object oId;
81
           Object oParentId;
82
           Integer[] rowArray = new Integer[2];
83
            for(Object object : data)
84
            {
85
               Object[] row = (Object[])object;
86
               oId = row[0];
87

    
88
                if (oId != null){
89
                    id = Integer.valueOf(oId.toString());
90
                    rowArray = new Integer[2];
91
                    oParentId = row[1];
92
                    if (oParentId != null){
93
                        parentId = Integer.valueOf(oParentId.toString());
94
                        rowArray[1]= parentId;
95

    
96
                    }else{
97
                        rowArray[1]= null;
98
                    }
99
                    rowArray[0]= id;
100
                    result.add(rowArray);
101
                }
102
            }
103
            Map<Integer, Set<Integer>> indexMap =  updater.makeIndexMap(result);
104
            for (Map.Entry<Integer, Set<Integer>> entry: indexMap.entrySet()){
105
                String idSet = updater.makeIdSetString(entry.getValue());
106
                query = updater.createUpdateIndicesQuery(null,entry.getKey(), idSet);
107
                sqlQuery = getAgentService().getSession().createSQLQuery(query);
108
                int resultInt = sqlQuery.executeUpdate();
109
                logger.debug("update all indice with index "+entry.getKey()+ " - " + resultInt);
110
            }
111
            //Update childrenCount
112
            if (updater.getTableName().equals("TaxonNode")){
113
                query = updater.getChildrenCountQuery();
114
                sqlQuery = getTaxonNodeService().getSession().createSQLQuery(query);
115
                data = sqlQuery.list();
116
                int realCount;
117
                int countChildren;
118
                for(Object object : data)
119
                {
120
                   Object[] row = (Object[])object;
121
                   realCount =  ((Number) row[0]).intValue();
122
                   countChildren = ((Number) row[1]).intValue();
123
                   id = ((Number) row[2]).intValue();
124

    
125
                   if (realCount != countChildren){
126
                       query = updater.getUpdateChildrenCount(realCount, id);
127
                       sqlQuery = getTaxonNodeService().getSession().createSQLQuery(query);
128
                       int resultInt = sqlQuery.executeUpdate();
129
                       logger.debug("update all childrenCount "+ resultInt);
130
                   }
131
                 }
132
            }
133
              commitTransaction(tx);
134
        } catch (SQLException e) {
135

    
136
            monitor.warning("Stopped sortIndex updater");
137
        }
138
    }
139

    
140
    @Override
141
    protected boolean isIgnore(DefaultImportState<SortIndexUpdaterConfigurator> state) {
142
        return false;
143
    }
144

    
145
    /**
146
     * {@inheritDoc}
147
     */
148
    @Override
149
    protected boolean doCheck(DefaultImportState<SortIndexUpdaterConfigurator> state) {
150
        return true;
151
    }
152

    
153
}
(5-5/5)