OpenConcerto

Dépôt officiel du code source de l'ERP OpenConcerto
sonarqube

svn://code.openconcerto.org/openconcerto

Rev

Rev 17 | Rev 65 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
17 ilm 1
/*
2
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
3
 *
4
 * Copyright 2011 OpenConcerto, by ILM Informatique. All rights reserved.
5
 *
6
 * The contents of this file are subject to the terms of the GNU General Public License Version 3
7
 * only ("GPL"). You may not use this file except in compliance with the License. You can obtain a
8
 * copy of the License at http://www.gnu.org/licenses/gpl-3.0.html See the License for the specific
9
 * language governing permissions and limitations under the License.
10
 *
11
 * When distributing the software, include this License Header Notice in each file.
12
 */
13
 
14
 package org.openconcerto.sql.changer.correct;
15
 
16
import org.openconcerto.sql.Configuration;
17
import org.openconcerto.sql.changer.Changer;
18
import org.openconcerto.sql.element.SQLElement;
19
import org.openconcerto.sql.model.DBSystemRoot;
25 ilm 20
import org.openconcerto.sql.model.SQLField;
21
import org.openconcerto.sql.model.SQLRow;
22
import org.openconcerto.sql.model.SQLRowListRSH;
17 ilm 23
import org.openconcerto.sql.model.SQLRowValues;
24
import org.openconcerto.sql.model.SQLSelect;
25 ilm 25
import org.openconcerto.sql.model.SQLSelect.ArchiveMode;
17 ilm 26
import org.openconcerto.sql.model.SQLTable;
27
import org.openconcerto.sql.model.Where;
28
import org.openconcerto.sql.utils.SQLUtils;
29
import org.openconcerto.sql.utils.SQLUtils.SQLFactory;
30
 
31
import java.sql.SQLException;
32
import java.util.List;
33
 
34
/**
35
 * Find the private foreign fields for the passed tables and copy the shared private rows. Eg if
36
 * CPI1 -> OBS1{DES='pb'} and CPI2 -> OBS1{DES='pb'} this clones OBS1 : CPI1 -> OBS1{DES='pb'} and
37
 * CPI2 -> OBS2{DES='pb'}.
38
 *
39
 * @author Sylvain
40
 */
41
public class FixSharedPrivate extends Changer<SQLTable> {
42
 
43
    public FixSharedPrivate(DBSystemRoot b) {
44
        super(b);
45
    }
46
 
25 ilm 47
    @Override
17 ilm 48
    protected void changeImpl(final SQLTable t) throws SQLException {
49
        getStream().println(t + "... ");
50
        if (Configuration.getInstance() == null || Configuration.getInstance().getDirectory() == null)
51
            throw new IllegalStateException("no directory");
52
        final SQLElement elem = Configuration.getInstance().getDirectory().getElement(t);
53
        if (elem == null)
54
            throw new IllegalArgumentException("no element for " + t);
55
 
56
        for (final String pff : elem.getPrivateForeignFields()) {
57
            // eg Q18
58
            final SQLElement privateElement = elem.getPrivateElement(pff);
59
            final SQLTable privateTable = privateElement.getTable();
60
            // SELECT q.ID FROM Ideation_2007.Q18 q
61
            // JOIN Ideation_2007.MISSION m on m.ID_Q18 = q.ID
62
            // where q.ID != 1
63
            // GROUP BY q.ID
64
            // HAVING count(q.ID) > 1;
65
            final SQLSelect sel = new SQLSelect(t.getBase());
66
            sel.setArchivedPolicy(ArchiveMode.BOTH);
67
            sel.addSelect(privateTable.getKey());
68
            sel.addBackwardJoin("INNER", "m", t.getField(pff), null);
69
            final String req = sel.asString() + " GROUP BY " + privateTable.getKey().getFieldRef() + " HAVING count(" + privateTable.getKey().getFieldRef() + ")>1";
70
 
25 ilm 71
            @SuppressWarnings("unchecked")
17 ilm 72
            final List<Number> privateIDs = t.getDBSystemRoot().getDataSource().executeCol(req);
73
            if (privateIDs.size() > 0) {
74
                getStream().println("\t" + pff + " fixing " + privateIDs.size() + " ... ");
25 ilm 75
                final SQLField archF = t.getArchiveField();
76
                final SQLField privateArchF = privateTable.getArchiveField();
77
                if ((archF == null) != (privateArchF == null))
78
                    throw new IllegalStateException("Incoherent archive field : " + archF + " / " + privateArchF);
17 ilm 79
                SQLUtils.executeAtomic(t.getDBSystemRoot().getDataSource(), new SQLFactory<Object>() {
80
                    @Override
81
                    public Object create() throws SQLException {
82
                        // for each private pointed by more than one parent
83
                        for (final Number privateID : privateIDs) {
84
                            final SQLSelect fixSel = new SQLSelect(t.getBase());
85
                            fixSel.setArchivedPolicy(ArchiveMode.BOTH);
86
                            fixSel.addSelect(t.getKey());
25 ilm 87
                            if (archF != null)
88
                                fixSel.addSelect(archF);
17 ilm 89
                            fixSel.setWhere(new Where(t.getField(pff), "=", privateID));
25 ilm 90
                            final List<SQLRow> tIDs = SQLRowListRSH.execute(fixSel);
91
                            for (final SQLRow tID : tIDs) {
92
                                // the first one can keep its private
93
                                final SQLRowValues reallyPrivate;
94
                                if (tID == tIDs.get(0))
95
                                    reallyPrivate = new SQLRowValues(privateElement.getTable()).setID(privateID);
96
                                else
97
                                    reallyPrivate = privateElement.createCopy(privateID.intValue());
98
                                // keep archive coherence
99
                                if (archF != null)
100
                                    reallyPrivate.put(privateArchF.getName(), tID.getObject(archF.getName()));
101
                                new SQLRowValues(t).setID(tID.getIDNumber()).put(pff, reallyPrivate).update();
17 ilm 102
                            }
103
                        }
104
                        return null;
105
                    }
106
                });
107
            }
108
        }
109
        getStream().println(t + " done");
110
    }
111
}