001    package org.apache.ojb.broker.metadata.torque;
002    
003    /* Copyright 2002-2005 The Apache Software Foundation
004     *
005     * Licensed under the Apache License, Version 2.0 (the "License");
006     * you may not use this file except in compliance with the License.
007     * You may obtain a copy of the License at
008     *
009     *     http://www.apache.org/licenses/LICENSE-2.0
010     *
011     * Unless required by applicable law or agreed to in writing, software
012     * distributed under the License is distributed on an "AS IS" BASIS,
013     * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014     * See the License for the specific language governing permissions and
015     * limitations under the License.
016     */
017    
018    import org.apache.ojb.broker.metadata.ClassDescriptor;
019    import org.apache.ojb.broker.metadata.DescriptorRepository;
020    import org.apache.ojb.broker.metadata.FieldDescriptor;
021    
022    import java.util.HashMap;
023    import java.util.Iterator;
024    
025    public class TorqueTablePreprocessor {
026    
027        private DescriptorRepository repository;
028        private HashMap standardTables = new HashMap();
029    
030        public TorqueTablePreprocessor(DescriptorRepository repository) {
031            this.repository = repository;
032        }
033    
034        public void buildStandardTables() {
035            Iterator classDescriptorIterators = this.repository.iterator();
036            while (classDescriptorIterators.hasNext()) {
037                ClassDescriptor cd = (ClassDescriptor) classDescriptorIterators.next();
038                if(cd.isAbstract() || cd.isInterface())
039                {
040                    System.out.println("Skip table build for abstract base class / interface called "+cd.getClassNameOfObject());
041                }
042                else
043                {
044                    buildStandardTable(cd);
045                }
046            }
047        }
048    
049        public HashMap getStandardTables() {
050            return this.standardTables;
051        }
052    
053        private void buildStandardTable(ClassDescriptor cd) {
054            String tableName = cd.getFullTableName();
055            TableDescriptor tableDescriptor = (TableDescriptor) this.standardTables.get(tableName);
056            if (tableDescriptor == null) {
057                tableDescriptor = new TableDescriptor();
058                tableDescriptor.setName(tableName);
059                this.standardTables.put(tableName, tableDescriptor);
060            }
061    
062            buildStandardTableFieldDescriptors(cd.getFieldDescriptions(), tableDescriptor);
063            tableDescriptor.getIndices().addAll(cd.getIndexes());
064            tableDescriptor.getReferences().addAll(cd.getObjectReferenceDescriptors());
065        }
066    
067        private void buildStandardTableFieldDescriptors(FieldDescriptor fieldDescriptors[], TableDescriptor tableDescriptor) {
068            if (fieldDescriptors != null) {
069                for (int i = 0; i < fieldDescriptors.length; i++) {
070                    tableDescriptor.addColumn(fieldDescriptors[i]);
071                }
072            }
073        }
074    }