<<Back to http://devdoc.net
Mine with nofee-ng to get DevFee back!
View Javadoc
1   /**
2    *    Copyright 2009-2015 the original author or authors.
3    *
4    *    Licensed under the Apache License, Version 2.0 (the "License");
5    *    you may not use this file except in compliance with the License.
6    *    You may obtain a copy of the License at
7    *
8    *       http://www.apache.org/licenses/LICENSE-2.0
9    *
10   *    Unless required by applicable law or agreed to in writing, software
11   *    distributed under the License is distributed on an "AS IS" BASIS,
12   *    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   *    See the License for the specific language governing permissions and
14   *    limitations under the License.
15   */
16  package org.apache.ibatis.mapping;
17  
18  import java.util.ArrayList;
19  import java.util.Collections;
20  import java.util.HashSet;
21  import java.util.List;
22  import java.util.Locale;
23  import java.util.Set;
24  
25  import org.apache.ibatis.session.Configuration;
26  
27  /**
28   * @author Clinton Begin
29   */
30  public class ResultMap {
31    private String id;
32    private Class<?> type;
33    private List<ResultMapping> resultMappings;
34    private List<ResultMapping> idResultMappings;
35    private List<ResultMapping> constructorResultMappings;
36    private List<ResultMapping> propertyResultMappings;
37    private Set<String> mappedColumns;
38    private Discriminator discriminator;
39    private boolean hasNestedResultMaps;
40    private boolean hasNestedQueries;
41    private Boolean autoMapping;
42  
43    private ResultMap() {
44    }
45  
46    public static class Builder {
47      private ResultMap resultMap = new ResultMap();
48  
49      public Builder(Configuration configuration, String id, Class<?> type, List<ResultMapping> resultMappings) {
50        this(configuration, id, type, resultMappings, null);
51      }
52  
53      public Builder(Configuration configuration, String id, Class<?> type, List<ResultMapping> resultMappings, Boolean autoMapping) {
54        resultMap.id = id;
55        resultMap.type = type;
56        resultMap.resultMappings = resultMappings;
57        resultMap.autoMapping = autoMapping;
58      }
59  
60      public Builder discriminator(Discriminator discriminator) {
61        resultMap.discriminator = discriminator;
62        return this;
63      }
64  
65      public Class<?> type() {
66        return resultMap.type;
67      }
68  
69      public ResultMap build() {
70        if (resultMap.id == null) {
71          throw new IllegalArgumentException("ResultMaps must have an id");
72        }
73        resultMap.mappedColumns = new HashSet<String>();
74        resultMap.idResultMappings = new ArrayList<ResultMapping>();
75        resultMap.constructorResultMappings = new ArrayList<ResultMapping>();
76        resultMap.propertyResultMappings = new ArrayList<ResultMapping>();
77        for (ResultMapping resultMapping : resultMap.resultMappings) {
78          resultMap.hasNestedQueries = resultMap.hasNestedQueries || resultMapping.getNestedQueryId() != null;
79          resultMap.hasNestedResultMaps = resultMap.hasNestedResultMaps || (resultMapping.getNestedResultMapId() != null && resultMapping.getResultSet() == null);
80          final String column = resultMapping.getColumn();
81          if (column != null) {
82            resultMap.mappedColumns.add(column.toUpperCase(Locale.ENGLISH));
83          } else if (resultMapping.isCompositeResult()) {
84            for (ResultMapping compositeResultMapping : resultMapping.getComposites()) {
85              final String compositeColumn = compositeResultMapping.getColumn();
86              if (compositeColumn != null) {
87                resultMap.mappedColumns.add(compositeColumn.toUpperCase(Locale.ENGLISH));
88              }
89            }
90          }
91          if (resultMapping.getFlags().contains(ResultFlag.CONSTRUCTOR)) {
92            resultMap.constructorResultMappings.add(resultMapping);
93          } else {
94            resultMap.propertyResultMappings.add(resultMapping);
95          }
96          if (resultMapping.getFlags().contains(ResultFlag.ID)) {
97            resultMap.idResultMappings.add(resultMapping);
98          }
99        }
100       if (resultMap.idResultMappings.isEmpty()) {
101         resultMap.idResultMappings.addAll(resultMap.resultMappings);
102       }
103       // lock down collections
104       resultMap.resultMappings = Collections.unmodifiableList(resultMap.resultMappings);
105       resultMap.idResultMappings = Collections.unmodifiableList(resultMap.idResultMappings);
106       resultMap.constructorResultMappings = Collections.unmodifiableList(resultMap.constructorResultMappings);
107       resultMap.propertyResultMappings = Collections.unmodifiableList(resultMap.propertyResultMappings);
108       resultMap.mappedColumns = Collections.unmodifiableSet(resultMap.mappedColumns);
109       return resultMap;
110     }
111   }
112 
113   public String getId() {
114     return id;
115   }
116 
117   public boolean hasNestedResultMaps() {
118     return hasNestedResultMaps;
119   }
120 
121   public boolean hasNestedQueries() {
122     return hasNestedQueries;
123   }
124 
125   public Class<?> getType() {
126     return type;
127   }
128 
129   public List<ResultMapping> getResultMappings() {
130     return resultMappings;
131   }
132 
133   public List<ResultMapping> getConstructorResultMappings() {
134     return constructorResultMappings;
135   }
136 
137   public List<ResultMapping> getPropertyResultMappings() {
138     return propertyResultMappings;
139   }
140 
141   public List<ResultMapping> getIdResultMappings() {
142     return idResultMappings;
143   }
144 
145   public Set<String> getMappedColumns() {
146     return mappedColumns;
147   }
148 
149   public Discriminator getDiscriminator() {
150     return discriminator;
151   }
152 
153   public void forceNestedResultMaps() {
154     hasNestedResultMaps = true;
155   }
156   
157   public Boolean getAutoMapping() {
158     return autoMapping;
159   }
160 
161 }