001: /*
002: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
003: *
004: * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
005: *
006: * The contents of this file are subject to the terms of either the GNU
007: * General Public License Version 2 only ("GPL") or the Common
008: * Development and Distribution License("CDDL") (collectively, the
009: * "License"). You may not use this file except in compliance with the
010: * License. You can obtain a copy of the License at
011: * http://www.netbeans.org/cddl-gplv2.html
012: * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
013: * specific language governing permissions and limitations under the
014: * License. When distributing the software, include this License Header
015: * Notice in each file and include the License file at
016: * nbbuild/licenses/CDDL-GPL-2-CP. Sun designates this
017: * particular file as subject to the "Classpath" exception as provided
018: * by Sun in the GPL Version 2 section of the License file that
019: * accompanied this code. If applicable, add the following below the
020: * License Header, with the fields enclosed by brackets [] replaced by
021: * your own identifying information:
022: * "Portions Copyrighted [year] [name of copyright owner]"
023: *
024: * If you wish your version of this file to be governed by only the CDDL
025: * or only the GPL Version 2, indicate your decision by adding
026: * "[Contributor] elects to include this software in this distribution
027: * under the [CDDL or GPL Version 2] license." If you do not indicate a
028: * single choice of license, a recipient has the option to distribute
029: * your version of this file under either the CDDL, the GPL Version 2 or
030: * to extend the choice of license to its licensees as provided above.
031: * However, if you add GPL Version 2 code and therefore, elected the GPL
032: * Version 2 license, then the option applies only if the new code is
033: * made subject to such option by the copyright holder.
034: *
035: * Contributor(s):
036: *
037: * Portions Copyrighted 2008 Sun Microsystems, Inc.
038: */
039:
040: package org.netbeans.modules.websvc.saas.model;
041:
042: import java.awt.Image;
043: import org.netbeans.modules.websvc.saas.model.jaxb.Group;
044: import java.util.ArrayList;
045: import java.util.Collections;
046: import java.util.List;
047: import java.util.SortedMap;
048: import java.util.TreeMap;
049: import org.openide.filesystems.FileObject;
050: import org.openide.util.Exceptions;
051: import org.openide.util.NbBundle;
052:
053: /**
054: *
055: * @author nam
056: */
057: public class SaasGroup {
058: public static final String PROP_GROUP_NAME = "groupName";
059:
060: private final Group delegate;
061: private final SaasGroup parent;
062: private FileObject groupFolder;
063: private boolean userDefined = true; //once set to false, remain false.
064: private SortedMap<String, Saas> services;
065: private SortedMap<String, SaasGroup> children;
066: private String icon16Path;
067: private String icon32Path;
068:
069: public SaasGroup(SaasGroup parent, Group group) {
070: this .parent = parent;
071: this .delegate = group;
072: services = Collections
073: .synchronizedSortedMap(new TreeMap<String, Saas>());
074: }
075:
076: public SaasGroup getParent() {
077: return parent;
078: }
079:
080: public FileObject getGroupFolder() {
081: if (groupFolder == null) {
082: if (parent == null) {
083: return SaasServicesModel.getWebServiceHome();
084: }
085: groupFolder = parent.getGroupFolder().getFileObject(
086: getName(), null);
087: if (groupFolder == null) {
088: try {
089: groupFolder = parent.getGroupFolder().createFolder(
090: getName());
091: } catch (Exception ex) {
092: Exceptions.printStackTrace(ex);
093: }
094: }
095: }
096: return groupFolder;
097: }
098:
099: public Group getDelegate() {
100: return delegate;
101: }
102:
103: public List<Saas> getServices() {
104: return Collections.unmodifiableList(new ArrayList<Saas>(
105: services.values()));
106: }
107:
108: /**
109: * Not a mutation
110: *
111: * @param service saas service
112: */
113: public void addService(Saas service) {
114: getServices();
115: services.put(service.getDisplayName(), service);
116: }
117:
118: /**
119: * If this is part of model mutation, caller is responsible to ensure
120: * SaasServices persists with proper Group information and eventing.
121: *
122: * @param service saas service to remove
123: */
124: public boolean removeService(Saas service) {
125: Saas removed = services.remove(service.getDisplayName());
126: if (removed != null) {
127: return true;
128: }
129: return false;
130: }
131:
132: public void setName(String value) {
133: if (getParent() == null) {
134: throw new IllegalArgumentException(
135: "Cannot rename root group");
136: }
137:
138: if (value == null || value.equals(getName())) {
139: return;
140: }
141:
142: String message = null;
143: if (!isUserDefined()) {
144: message = NbBundle.getMessage(getClass(),
145: "MSG_GroupNotUserDefined");
146: }
147:
148: for (SaasGroup g : getParent().getChildrenGroups()) {
149: if (g.getName().equals(value)) {
150: message = NbBundle.getMessage(getClass(),
151: "MSG_DuplicateGroupName");
152: }
153: }
154:
155: if (message != null) {
156: throw new IllegalArgumentException(message);
157: }
158:
159: delegate.setName(value);
160: resetAllServicesGroupPath();
161: }
162:
163: private void resetAllServicesGroupPath() {
164: for (Saas s : getServices()) {
165: s.computePathFromRoot();
166: }
167: for (SaasGroup g : getChildrenGroups()) {
168: g.resetAllServicesGroupPath();
169: }
170: }
171:
172: public String getName() {
173: return delegate.getName();
174: }
175:
176: public boolean isUserDefined() {
177: return userDefined;
178: }
179:
180: void setUserDefined(boolean v) {
181: if (userDefined) {
182: userDefined = v;
183: }
184: }
185:
186: public String getIcon16Path() {
187: return icon16Path;
188: }
189:
190: protected void setIcon16Path(String icon16Path) {
191: this .icon16Path = icon16Path;
192: }
193:
194: public String getIcon32Path() {
195: return icon32Path;
196: }
197:
198: protected void setIcon32Path(String icon32Path) {
199: this .icon32Path = icon32Path;
200: }
201:
202: public List<SaasGroup> getChildrenGroups() {
203: if (children == null) {
204: children = Collections
205: .synchronizedSortedMap(new TreeMap<String, SaasGroup>());
206: for (Group g : delegate.getGroup()) {
207: SaasGroup sg = new SaasGroup(this , g);
208: children.put(sg.getName(), sg);
209: }
210: }
211: return Collections.unmodifiableList(new ArrayList<SaasGroup>(
212: children.values()));
213: }
214:
215: public SaasGroup getChildGroup(String name) {
216: getChildrenGroups();
217: return children.get(name);
218: }
219:
220: /**
221: * All children services and children groups also removed.
222: * Only group created by users could be removed.
223: * Caller is responsible for persisting changes.
224: * @param group saas group to remove
225: */
226: protected boolean removeChildGroup(SaasGroup group) {
227: if (!group.canRemove()) {
228: return false;
229: }
230:
231: _removeChildGroup(group);
232:
233: return true;
234: }
235:
236: private void _removeChildGroup(SaasGroup child) {
237: if (child != null) {
238: for (Saas saas : child.getServices()) {
239: removeService(saas);
240: try {
241: FileObject saasFile = saas.getSaasFile();
242: if (saasFile != null) {
243: saasFile.delete();
244: }
245: } catch (Exception e) {
246: Exceptions.printStackTrace(e);
247: }
248: }
249: for (SaasGroup c : child.getChildrenGroups()) {
250: _removeChildGroup(c);
251: }
252: getDelegate().getGroup().remove(child.getDelegate());
253: children.remove(child.getName());
254: }
255: }
256:
257: public boolean canRemove() {
258: if (!isUserDefined()) {
259: return false;
260: }
261: for (Saas s : getServices()) {
262: if (!s.isUserDefined()) {
263: return false;
264: }
265: }
266: for (SaasGroup child : getChildrenGroups()) {
267: if (!child.canRemove()) {
268: return false;
269: }
270: }
271: return true;
272: }
273:
274: /**
275: * If this is part of model mutation, caller is responsible to save
276: * changes, whether the group is user-defined or pre-installed.
277: *
278: * @param group saas group to add
279: */
280: protected void addChildGroup(SaasGroup group) {
281: getChildrenGroups();
282: children.put(group.getName(), group);
283: getDelegate().getGroup().add(group.getDelegate());
284: }
285:
286: /**
287: * Return a clone of group element that has one descendant at each level,
288: * excluding root, down to the current group.
289: * @return
290: */
291: public Group getPathFromRoot() {
292: SaasGroup parentGroup = getParent();
293: if (parentGroup == null) {
294: return null;
295: }
296:
297: Group group = new Group();
298: group.setName(getName());
299: while (parentGroup != SaasServicesModel.getInstance()
300: .getRootGroup()) {
301: Group p = new Group();
302: p.setName(parentGroup.getName());
303: p.getGroup().add(group);
304: group = p;
305: parentGroup = parentGroup.getParent();
306: }
307: return group;
308: }
309:
310: /**
311: * Just create a child group node with given name and back parent pointer.
312: * Caller should explicitly mutate model, flush, persist and fire event
313: *
314: * @param name
315: * @return created group
316: */
317: protected SaasGroup createGroup(String name) {
318: Group g = new Group();
319: g.setName(name);
320: SaasGroup child = new SaasGroup(this , g);
321: child.setUserDefined(true);
322: return child;
323: }
324:
325: public String toString() {
326: return getName();
327: }
328:
329: }
|