View Javadoc

1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements.  See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache License, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License.  You may obtain a copy of the License at
8    * 
9    *      http://www.apache.org/licenses/LICENSE-2.0
10   * 
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
16   */
17  package org.apache.jetspeed.om.folder.impl;
18  
19  import java.util.AbstractList;
20  import java.util.List;
21  
22  import org.apache.jetspeed.page.impl.DatabasePageManagerUtils;
23  
24  /***
25   * FolderMenuDefinitionList
26   *
27   * @author <a href="mailto:rwatler@apache.org">Randy Watler</a>
28   * @version $Id$
29   */
30  class FolderMenuDefinitionList extends AbstractList
31  {
32      private FolderImpl folder;
33  
34      private List removedMenuDefinitions;
35  
36      FolderMenuDefinitionList(FolderImpl folder)
37      {
38          super();
39          this.folder = folder;
40      }
41  
42      /***
43       * validateDefinitionForAdd
44       *
45       * Validates menu definition to be added to this list.
46       *
47       * @param definition menu definition to add
48       * @return list element to add
49       */
50      private FolderMenuDefinitionImpl validateDefinitionForAdd(FolderMenuDefinitionImpl definition)
51      {
52          // only non-null definitions supported
53          if (definition == null)
54          {
55              throw new NullPointerException("Unable to add null to list.");
56          }
57          // make sure element is unique
58          if (folder.accessMenus().contains(definition))
59          {
60              throw new IllegalArgumentException("Unable to add duplicate entry to list: " + (definition).getName());
61          }
62          // retrieve from removed list to reuse
63          // previously removed element copying
64          // menu definition data
65          if (removedMenuDefinitions != null)
66          {
67              int removedIndex = removedMenuDefinitions.indexOf(definition);
68              if (removedIndex >= 0)
69              {
70                  // reuse menu definition with matching name
71                  FolderMenuDefinitionImpl addDefinition = definition;
72                  definition = (FolderMenuDefinitionImpl)removedMenuDefinitions.remove(removedIndex);
73                  // TODO: move this logic to copy methods on implementations
74                  // copy menu definition members
75                  definition.setOptions(addDefinition.getOptions());
76                  definition.setDepth(addDefinition.getDepth());
77                  definition.setPaths(addDefinition.isPaths());
78                  definition.setRegexp(addDefinition.isRegexp());
79                  definition.setProfile(addDefinition.getProfile());
80                  definition.setOrder(addDefinition.getOrder());
81                  definition.setSkin(addDefinition.getSkin());
82                  definition.setTitle(addDefinition.getTitle());
83                  definition.setShortTitle(addDefinition.getShortTitle());
84                  definition.setMenuElements(addDefinition.getMenuElements());
85                  // copy menu definition metadata members
86                  // TODO: strengthen... this code is not robust
87                  // and may fail if multiple edits without a db
88                  // update occur and duplicate metadata members
89                  // are removed in one operation and reinserted
90                  // in a subsequent operation because the
91                  // metadata members are required to be unique
92                  // and a removal list is not maintained for the
93                  // metadata fields collections yet
94                  definition.getMetadata().copyFields(addDefinition.getMetadata().getFields());
95              }
96          }
97          return definition;
98      }
99  
100     /***
101      * getRemovedMenuDefinitions
102      *
103      * @return removed menu definitions tracking collection
104      */
105     private List getRemovedMenuDefinitions()
106     {
107         if (removedMenuDefinitions == null)
108         {
109             removedMenuDefinitions = DatabasePageManagerUtils.createList();
110         }
111         return removedMenuDefinitions;
112     }
113 
114     /* (non-Javadoc)
115      * @see java.util.List#add(int,java.lang.Object)
116      */
117     public void add(int index, Object element)
118     {
119         // implement for modifiable AbstractList:
120         // validate index
121         if ((index < 0) || (index > folder.accessMenus().size()))
122         {
123             throw new IndexOutOfBoundsException("Unable to add to list at index: " + index);
124         }
125         // verify menu definition
126         FolderMenuDefinitionImpl definition = validateDefinitionForAdd((FolderMenuDefinitionImpl)element);
127         // add to underlying ordered list
128         folder.accessMenus().add(index, definition);
129     }
130 
131     /* (non-Javadoc)
132      * @see java.util.List#get(int)
133      */
134     public Object get(int index)
135     {
136         // implement for modifiable AbstractList
137         return folder.accessMenus().get(index);
138     }
139 
140     /* (non-Javadoc)
141      * @see java.util.List#remove(int)
142      */
143     public Object remove(int index)
144     {
145         // implement for modifiable AbstractList:
146         // save removed element 
147         FolderMenuDefinitionImpl removed = (FolderMenuDefinitionImpl)folder.accessMenus().remove(index);
148         if (removed != null)
149         {
150             getRemovedMenuDefinitions().add(removed);
151         }
152         return removed;
153     }
154 
155     /* (non-Javadoc)
156      * @see java.util.List#set(int,java.lang.Object)
157      */
158     public Object set(int index, Object element)
159     {
160         // implement for modifiable AbstractList:
161         // verify menu definition
162         FolderMenuDefinitionImpl newDefinition = validateDefinitionForAdd((FolderMenuDefinitionImpl)element);
163         // set in underlying ordered list
164         FolderMenuDefinitionImpl definition = (FolderMenuDefinitionImpl)folder.accessMenus().set(index, newDefinition);
165         // save replaced element
166         getRemovedMenuDefinitions().add(definition);
167         // return menu definition
168         return definition;
169     }
170 
171     /* (non-Javadoc)
172      * @see java.util.List#size()
173      */
174     public int size()
175     {
176         // implement for modifiable AbstractList
177         return folder.accessMenus().size();
178     }
179 }