Fix the build.
[castle.git] / Experiments / Attic / Generator / Castle.ActiveRecord.Generator.Components / RelationshipBuilder.cs
blob43600bad23f48be896205aec9e5f5c741cdd62d7
1 // Copyright 2004-2007 Castle Project - http://www.castleproject.org/
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
15 namespace Castle.ActiveRecord.Generator.Components
17 using System;
18 using System.Collections;
20 using Castle.ActiveRecord.Generator.Components.Database;
23 public class RelationshipBuilder : IRelationshipBuilder
25 private INamingService _namingService;
27 public RelationshipBuilder(INamingService namingService)
29 _namingService = namingService;
32 #region IRelationshipBuilder Members
34 public ActiveRecordPropertyRelationDescriptor Build(RelationshipInfo info)
36 ActiveRecordPropertyRelationDescriptor desc = null;
38 if (info.Association == AssociationEnum.BelongsTo)
40 desc = CreateBelongsToRelation(info);
42 else if (info.Association == AssociationEnum.HasMany)
44 desc = CreateHasManyRelation(info);
46 else if (info.Association == AssociationEnum.HasAndBelongsToMany)
48 desc = CreateHasManyAndBelongsToRelation(info);
51 PopulateInfoIntoDescriptor(info, desc);
53 return desc;
56 #endregion
58 private ActiveRecordPropertyRelationDescriptor CreateBelongsToRelation(RelationshipInfo info)
60 // Validates first
62 if (info.ParentCol == null)
64 throw new ArgumentException("No parent column specified");
67 String colName = info.ParentCol.Name;
68 String propName = info.TargetDescriptor.ClassName;
70 return new ActiveRecordBelongsToDescriptor(colName, propName, info.TargetDescriptor);
73 private ActiveRecordPropertyRelationDescriptor CreateHasManyRelation(RelationshipInfo info)
75 // Validates first
77 if (info.ChildCol == null)
79 throw new ArgumentException("No column specified");
82 String colName = info.ChildCol.Name;
83 String propName = _namingService.CreateRelationName( info.TargetDescriptor.ClassName );
85 return new ActiveRecordHasManyDescriptor(colName, propName, info.TargetDescriptor);
88 private ActiveRecordPropertyRelationDescriptor CreateHasManyAndBelongsToRelation(RelationshipInfo info)
90 // Validates first
92 if (info.ParentCol == null)
94 throw new ArgumentException("No parent column specified");
96 if (info.ChildCol == null)
98 throw new ArgumentException("No child column specified");
100 if (info.AssociationTable == null)
102 throw new ArgumentException("No association table specified");
105 String colName = info.ChildCol.Name;
106 String colKeyName = info.ParentCol.Name;
107 String propName = _namingService.CreateRelationName( info.TargetDescriptor.ClassName );
109 return new ActiveRecordHasAndBelongsToManyDescriptor(colName,
110 info.AssociationTable.Name, propName, info.TargetDescriptor, colKeyName);
113 private void PopulateInfoIntoDescriptor(RelationshipInfo info, ActiveRecordPropertyRelationDescriptor desc)
115 desc.Insert = info.Insert;
116 desc.Update = info.Update;
117 desc.Inverse = info.Inverse;
118 desc.Proxy = info.UseProxy;
120 desc.Cascade = info.Cascade;
121 desc.OuterJoin = info.OuterJoin;
123 desc.Where = info.Where;
124 desc.OrderBy = info.OrderBy;