001/*
002 * Copyright (c) 2006-2011 Nuxeo SA (http://nuxeo.com/) and others.
003 *
004 * All rights reserved. This program and the accompanying materials
005 * are made available under the terms of the Eclipse Public License v1.0
006 * which accompanies this distribution, and is available at
007 * http://www.eclipse.org/legal/epl-v10.html
008 *
009 * Contributors:
010 *     Florent Guillaume
011 */
012package org.nuxeo.ecm.core.storage.sql.jdbc;
013
014import java.io.Serializable;
015import java.sql.PreparedStatement;
016import java.sql.ResultSet;
017import java.sql.SQLException;
018import java.util.ArrayList;
019import java.util.Calendar;
020import java.util.List;
021
022import org.nuxeo.ecm.core.storage.sql.ACLRow;
023import org.nuxeo.ecm.core.storage.sql.Model;
024import org.nuxeo.ecm.core.storage.sql.Row;
025import org.nuxeo.ecm.core.storage.sql.jdbc.db.Column;
026
027/**
028 * Collection IO for arrays of ACLs.
029 */
030public class ACLCollectionIO implements CollectionIO {
031
032    public static final CollectionIO INSTANCE = new ACLCollectionIO();
033
034    @Override
035    public ACLRow getCurrentFromResultSet(ResultSet rs, List<Column> columns, Model model, Serializable[] returnId,
036            int[] returnPos) throws SQLException {
037        Serializable id = null;
038        String name = null;
039        boolean grant = false;
040        String permission = null;
041        String creator = null;
042        Calendar begin = null;
043        Calendar end = null;
044        String user = null;
045        String group = null;
046        Long status = null;
047        int i = 0;
048        for (Column column : columns) {
049            i++;
050            String key = column.getKey();
051            Serializable v = column.getFromResultSet(rs, i);
052            switch (key) {
053            case Model.MAIN_KEY:
054                id = v;
055                break;
056            case Model.ACL_NAME_KEY:
057                name = (String) v;
058                break;
059            case Model.ACL_GRANT_KEY:
060                grant = v == null ? false : (Boolean) v;
061                break;
062            case Model.ACL_PERMISSION_KEY:
063                permission = (String) v;
064                break;
065            case Model.ACL_CREATOR_KEY:
066                creator = (String) v;
067                break;
068            case Model.ACL_BEGIN_KEY:
069                begin = (Calendar) v;
070                break;
071            case Model.ACL_END_KEY:
072                end = (Calendar) v;
073                break;
074            case Model.ACL_USER_KEY:
075                user = (String) v;
076                break;
077            case Model.ACL_GROUP_KEY:
078                group = (String) v;
079                break;
080            case Model.ACL_STATUS_KEY:
081                status = (Long) v;
082                break;
083            case Model.ACL_POS_KEY:
084                // ignore, query already sorts by pos
085                break;
086            default:
087                throw new RuntimeException(key);
088            }
089        }
090        Serializable prevId = returnId[0];
091        returnId[0] = id;
092        int pos = (id != null && !id.equals(prevId)) ? 0 : returnPos[0] + 1;
093        returnPos[0] = pos;
094        return new ACLRow(pos, name, grant, permission, user, group, creator, begin, end, status);
095    }
096
097    @Override
098    public void executeInserts(PreparedStatement ps, List<Row> rows, List<Column> columns, boolean supportsBatchUpdates,
099            String sql, JDBCConnection connection) throws SQLException {
100        List<Serializable> debugValues = connection.logger.isLogEnabled() ? new ArrayList<Serializable>() : null;
101        String loggedSql = supportsBatchUpdates ? sql + " -- BATCHED" : sql;
102        int batch = 0;
103        for (Row row : rows) {
104            batch++;
105            Serializable id = row.id;
106            Serializable[] array = row.values;
107            for (int i = 0; i < array.length; i++) {
108                ACLRow acl = (ACLRow) array[i];
109                int n = 0;
110                for (Column column : columns) {
111                    n++;
112                    String key = column.getKey();
113                    Serializable v;
114                    switch (key) {
115                    case Model.MAIN_KEY:
116                        v = id;
117                        break;
118                    case Model.ACL_POS_KEY:
119                        v = (long) acl.pos;
120                        break;
121                    case Model.ACL_NAME_KEY:
122                        v = acl.name;
123                        break;
124                    case Model.ACL_GRANT_KEY:
125                        v = acl.grant;
126                        break;
127                    case Model.ACL_PERMISSION_KEY:
128                        v = acl.permission;
129                        break;
130                    case Model.ACL_CREATOR_KEY:
131                        v = acl.creator;
132                        break;
133                    case Model.ACL_BEGIN_KEY:
134                        v = acl.begin;
135                        break;
136                    case Model.ACL_END_KEY:
137                        v = acl.end;
138                        break;
139                    case Model.ACL_STATUS_KEY:
140                        v = acl.status;
141                        break;
142                    case Model.ACL_USER_KEY:
143                        v = acl.user;
144                        break;
145                    case Model.ACL_GROUP_KEY:
146                        v = acl.group;
147                        break;
148                    default:
149                        throw new RuntimeException(key);
150                    }
151                    column.setToPreparedStatement(ps, n, v);
152                    if (debugValues != null) {
153                        debugValues.add(v);
154                    }
155                }
156                if (debugValues != null) {
157                    connection.logger.logSQL(loggedSql, debugValues);
158                    debugValues.clear();
159                }
160                if (supportsBatchUpdates) {
161                    ps.addBatch();
162                    if (batch % JDBCRowMapper.UPDATE_BATCH_SIZE == 0) {
163                        ps.executeBatch();
164                        connection.countExecute();
165                    }
166                } else {
167                    ps.execute();
168                    connection.countExecute();
169                }
170            }
171        }
172        if (supportsBatchUpdates) {
173            ps.executeBatch();
174            connection.countExecute();
175        }
176    }
177
178}