View Javadoc

1   /*
2    * Copyright 2008 FatWire Corporation. All Rights Reserved.
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  
17  package com.fatwire.gst.foundation.facade.runtag.vdm;
18  
19  import COM.FutureTense.Interfaces.ICS;
20  
21  import com.fatwire.gst.foundation.facade.runtag.TagRunner;
22  
23  /**
24   * Sets an alias ONLY if it has not been set before. Avoids unnecessary DB
25   * thrash.
26   * 
27   * @author Tony Field
28   * @since Sep 29, 2008
29   */
30  public class SetAliasWithoutReset implements TagRunner {
31  
32      private final String key;
33  
34      private final String value;
35  
36      public SetAliasWithoutReset(final String key, final String value) {
37          super();
38          this.key = key;
39          this.value = value;
40      }
41  
42      public String execute(final ICS ics) {
43          final String varname = "alias" + ics.genID(true);
44          try {
45              GetAlias getAlias = new GetAlias(key, varname);
46              String getResult = getAlias.execute(ics);
47              String alias = ics.GetVar(varname);
48              if (alias != null && alias.equals(value) || alias == null && value == null) {
49                  // nothing to do. this saves a lot of processing
50                  return getResult;
51              }
52  
53              SetAlias setAlias = new SetAlias(key, value);
54              return setAlias.execute(ics);
55  
56          } finally {
57              // cleaning up
58              ics.RemoveVar(varname);
59          }
60      }
61  
62      public String getValue() {
63          return value;
64      }
65  
66      public String getKey() {
67          return key;
68      }
69  
70  }