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