001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017: package org.apache.jetspeed.om.folder.impl;
018:
019: import java.util.AbstractList;
020: import java.util.List;
021:
022: import org.apache.jetspeed.page.impl.DatabasePageManagerUtils;
023:
024: /**
025: * FolderMenuDefinitionList
026: *
027: * @author <a href="mailto:rwatler@apache.org">Randy Watler</a>
028: * @version $Id$
029: */
030: class FolderMenuDefinitionList extends AbstractList {
031: private FolderImpl folder;
032:
033: private List removedMenuDefinitions;
034:
035: FolderMenuDefinitionList(FolderImpl folder) {
036: super ();
037: this .folder = folder;
038: }
039:
040: /**
041: * validateDefinitionForAdd
042: *
043: * Validates menu definition to be added to this list.
044: *
045: * @param definition menu definition to add
046: * @return list element to add
047: */
048: private FolderMenuDefinitionImpl validateDefinitionForAdd(
049: FolderMenuDefinitionImpl definition) {
050: // only non-null definitions supported
051: if (definition == null) {
052: throw new NullPointerException(
053: "Unable to add null to list.");
054: }
055: // make sure element is unique
056: if (folder.accessMenus().contains(definition)) {
057: throw new IllegalArgumentException(
058: "Unable to add duplicate entry to list: "
059: + (definition).getName());
060: }
061: // retrieve from removed list to reuse
062: // previously removed element copying
063: // menu definition data
064: if (removedMenuDefinitions != null) {
065: int removedIndex = removedMenuDefinitions
066: .indexOf(definition);
067: if (removedIndex >= 0) {
068: // reuse menu definition with matching name
069: FolderMenuDefinitionImpl addDefinition = definition;
070: definition = (FolderMenuDefinitionImpl) removedMenuDefinitions
071: .remove(removedIndex);
072: // TODO: move this logic to copy methods on implementations
073: // copy menu definition members
074: definition.setOptions(addDefinition.getOptions());
075: definition.setDepth(addDefinition.getDepth());
076: definition.setPaths(addDefinition.isPaths());
077: definition.setRegexp(addDefinition.isRegexp());
078: definition.setProfile(addDefinition.getProfile());
079: definition.setOrder(addDefinition.getOrder());
080: definition.setSkin(addDefinition.getSkin());
081: definition.setTitle(addDefinition.getTitle());
082: definition.setShortTitle(addDefinition.getShortTitle());
083: definition.setMenuElements(addDefinition
084: .getMenuElements());
085: // copy menu definition metadata members
086: // TODO: strengthen... this code is not robust
087: // and may fail if multiple edits without a db
088: // update occur and duplicate metadata members
089: // are removed in one operation and reinserted
090: // in a subsequent operation because the
091: // metadata members are required to be unique
092: // and a removal list is not maintained for the
093: // metadata fields collections yet
094: definition.getMetadata().copyFields(
095: addDefinition.getMetadata().getFields());
096: }
097: }
098: return definition;
099: }
100:
101: /**
102: * getRemovedMenuDefinitions
103: *
104: * @return removed menu definitions tracking collection
105: */
106: private List getRemovedMenuDefinitions() {
107: if (removedMenuDefinitions == null) {
108: removedMenuDefinitions = DatabasePageManagerUtils
109: .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: // implement for modifiable AbstractList:
119: // validate index
120: if ((index < 0) || (index > folder.accessMenus().size())) {
121: throw new IndexOutOfBoundsException(
122: "Unable to add to list at index: " + index);
123: }
124: // verify menu definition
125: FolderMenuDefinitionImpl definition = validateDefinitionForAdd((FolderMenuDefinitionImpl) element);
126: // add to underlying ordered list
127: folder.accessMenus().add(index, definition);
128: }
129:
130: /* (non-Javadoc)
131: * @see java.util.List#get(int)
132: */
133: public Object get(int index) {
134: // implement for modifiable AbstractList
135: return folder.accessMenus().get(index);
136: }
137:
138: /* (non-Javadoc)
139: * @see java.util.List#remove(int)
140: */
141: public Object remove(int index) {
142: // implement for modifiable AbstractList:
143: // save removed element
144: FolderMenuDefinitionImpl removed = (FolderMenuDefinitionImpl) folder
145: .accessMenus().remove(index);
146: if (removed != null) {
147: getRemovedMenuDefinitions().add(removed);
148: }
149: return removed;
150: }
151:
152: /* (non-Javadoc)
153: * @see java.util.List#set(int,java.lang.Object)
154: */
155: public Object set(int index, Object element) {
156: // implement for modifiable AbstractList:
157: // verify menu definition
158: FolderMenuDefinitionImpl newDefinition = validateDefinitionForAdd((FolderMenuDefinitionImpl) element);
159: // set in underlying ordered list
160: FolderMenuDefinitionImpl definition = (FolderMenuDefinitionImpl) folder
161: .accessMenus().set(index, newDefinition);
162: // save replaced element
163: getRemovedMenuDefinitions().add(definition);
164: // return menu definition
165: return definition;
166: }
167:
168: /* (non-Javadoc)
169: * @see java.util.List#size()
170: */
171: public int size() {
172: // implement for modifiable AbstractList
173: return folder.accessMenus().size();
174: }
175: }
|