View Javadoc

1   /*
2    * Copyright 2006 Outsource Cafe, Inc.
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.javagen.agile.db.model;
17  
18  import java.util.ArrayList;
19  import java.util.List;
20  
21  import javax.xml.bind.annotation.XmlAttribute;
22  import javax.xml.bind.annotation.XmlRootElement;
23  import javax.xml.bind.annotation.XmlTransient;
24  
25  import org.javagen.agile.core.model.AbstractModel;
26  import org.javagen.agile.core.model.Model;
27  import org.javagen.agile.core.util.ModelUtil;
28  
29  @XmlRootElement(name="database")
30  public class Database extends AbstractModel {
31  	
32      public static final String DEFAULT_MODEL_TYPE = "DATABASE";
33  
34      protected String catalog;
35  	protected String schema;
36  
37      public Database() { 
38          super();
39          this.setModelType(DEFAULT_MODEL_TYPE);
40      }
41      
42      public Database(String name) { 
43          this();
44          setName(name);
45      }
46      
47      @Override
48      public void copyTo(Model targetModel) {
49          super.copyTo(targetModel);
50          Database target = (Database)targetModel;
51   		if (this.getCatalog()!=null)
52  			target.setCatalog(this.getCatalog());
53  		if (this.getSchema()!=null)
54  			target.setSchema(this.getSchema());
55  	}
56      
57  	////////////////////////////////////////////////////////////////////////////
58  	// Table handling:
59  	////////////////////////////////////////////////////////////////////////////
60  
61  	public Table createTable() {
62  		return new Table(this);
63  	}
64  	
65  	public Table addTable(String name, Boolean linkTable) {
66  		Table table = createTable();
67  		table.setName(name);
68  		table.setLinkTable(linkTable);
69  		this.addTable(table);
70  		return table;
71  	}
72  	
73  	public Table lookupTable(String name) {
74          return (Table)ModelUtil.lookupChildByName(this, name, Table.class, false);
75  	}
76  
77      @XmlTransient
78  	public List<Table> getTables() {
79          List<Table> list = new ArrayList<Table>(getChildModels().size());
80          for(Model child : getChildModels())
81              list.add((Table)child);
82  		return list;
83  	}
84  
85  	public void addTable(Table table) {
86          addChildModel(table);
87  	}
88  
89      ////////////////////////////////////////////////////////////////////////////
90  	// properties
91  	////////////////////////////////////////////////////////////////////////////
92  
93      @XmlAttribute
94  	public String getSchema() {
95  		return schema;
96  	}
97  
98  	public void setSchema(String schema) {
99  		this.schema = schema;
100 	}
101 
102     @XmlAttribute
103 	public String getCatalog() {
104 		return catalog;
105 	}
106 
107 	public void setCatalog(String catalog) {
108 		this.catalog = catalog;
109 	}
110 
111 //	public List<Model> getChildModels() {
112 //		List<Table> list = getTables();
113 //		if (list==null) {
114 //			return null;
115 //		} else {
116 //			List<Model> mList = new ArrayList<Model>(list.size());
117 //			mList.addAll(list);
118 //			return mList;
119 //		}
120 //	}
121 	
122 }