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.core.visitor;
17
18 import org.javagen.agile.core.model.Model;
19
20 /***
21 * Traverses the database model tree assigning a unique ID to each node based on its name, type and parent model.
22 * <p>
23 * If the <code>prefix</code> property is set, each ID will be prefixed with its text value.
24 *
25 * @author Richard Easterling
26 */
27 public class IdAssignerVisitor extends TypeLookupVisitor {
28
29 private String prefix = "";
30
31 /***
32 * Default constructor.
33 * <p>
34 * Define <code>defaultVisit</code> to assign model id to <code>prefix+model.name</code>.
35 */
36 public IdAssignerVisitor() {
37 this.setDefualtVisit( new Visit() {
38 public void visit(Model model) {
39 if (model.getId()==null)
40 model.setId( prefix+model.getName() );
41 }
42 });
43 }
44
45 // /////////////////////////////////////////////////////////////////////////
46 // Generator interface methods:
47 // /////////////////////////////////////////////////////////////////////////
48
49 public Model gen(Model model) {
50 if (model==null)
51 throw new NullPointerException("input Model must not be null");
52 if (prefix==null)
53 prefix = "";
54 GenericWalker.walk(model, this);
55 return model;
56 }
57
58 ///////////////////////////////////////////////////////////////////////////
59 // getters and setters:
60 ///////////////////////////////////////////////////////////////////////////
61
62 public String getPrefix() {
63 return prefix;
64 }
65
66 public void setPrefix(String prefix) {
67 this.prefix = prefix;
68 }
69
70 }