001/**
002 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
003 *
004 * Copyright (c) 2006 Sun Microsystems Inc. All Rights Reserved
005 *
006 * The contents of this file are subject to the terms
007 * of the Common Development and Distribution License
008 * (the License). You may not use this file except in
009 * compliance with the License.
010 *
011 * You can obtain a copy of the License at
012 * https://opensso.dev.java.net/public/CDDLv1.0.html or
013 * opensso/legal/CDDLv1.0.txt
014 * See the License for the specific language governing
015 * permission and limitations under the License.
016 *
017 * When distributing Covered Code, include this CDDL
018 * Header Notice in each file and include the License file
019 * at opensso/legal/CDDLv1.0.txt.
020 * If applicable, add the following below the CDDL Header,
021 * with the fields enclosed by brackets [] replaced by
022 * your own identifying information:
023 * "Portions Copyrighted [year] [name of copyright owner]"
024 *
025 * $Id: Modify.java,v 1.2 2008/06/25 05:47:10 qcheng Exp $
026 *
027 */
028
029
030package com.sun.identity.liberty.ws.disco;
031
032import java.util.Iterator;
033import java.util.List;
034import java.util.ArrayList;
035import org.w3c.dom.*;
036import com.sun.identity.liberty.ws.disco.common.DiscoConstants;
037import com.sun.identity.liberty.ws.disco.common.DiscoUtils;
038
039/**
040 * This class represents a discovery modify request.
041 * The following schema fragment specifies the expected content within
042 * the <code>Modify</code> object.
043 * <pre>
044 * &lt;xs:element name="Modify" type="ModifyType"/>
045 * &lt;complexType name="ModifyType">
046 *   &lt;complexContent>
047 *     &lt;restriction base="{http://www.w3.org/2001/XMLSchema}anyType">
048 *       &lt;sequence>
049 *         &lt;group ref="{urn:liberty:disco:2003-08}ResourceIDGroup"/>
050 *         &lt;element name="InsertEntry" type="{urn:liberty:disco:2003-08}InsertEntryType" maxOccurs="unbounded" minOccurs="0"/>
051 *         &lt;element name="RemoveEntry" type="{urn:liberty:disco:2003-08}RemoveEntryType" maxOccurs="unbounded" minOccurs="0"/>
052 *       &lt;/sequence>
053 *       &lt;attribute name="id" type="{http://www.w3.org/2001/XMLSchema}ID" />
054 *     &lt;/restriction>
055 *   &lt;/complexContent>
056 * &lt;/complexType>
057 * </pre>
058 * 
059 * @supported.all.api
060 */
061public class Modify {
062
063    private String id = null;
064    private ResourceID resourceID = null;
065    private EncryptedResourceID encryptResID = null;
066    private List inserts = null;
067    private List removes = null;
068
069    /**
070     * Constructor.
071     * @param resourceID ID for the discovery resource to be modified 
072     * @param insertEntry List of insert entries
073     * @param removeEntry List of remove entries
074     */
075    public Modify(ResourceID resourceID,
076                  List insertEntry,
077                  List removeEntry)
078    {
079        this.resourceID = resourceID;
080        inserts = insertEntry;
081        removes = removeEntry;
082    } 
083
084    /**
085     * Constructor.
086     * @param resourceID Encrypted Discovery Resource ID to be modified
087     * @param insertEntry List of insert entries
088     * @param removeEntry List of remove entries
089     */
090    public Modify(EncryptedResourceID resourceID,
091                   List insertEntry,
092                   List removeEntry)
093    {
094        encryptResID = resourceID;
095        inserts = insertEntry;
096        removes = removeEntry;
097    }
098
099    /**
100     * Default constructor.
101     */
102    public Modify() {} 
103
104    /**
105     * Constructor.
106     * @param root Modify DOM element
107     * @exception DiscoveryException if error occurs
108     */
109    public Modify(Element root) throws DiscoveryException {
110        if (root == null) {
111            DiscoUtils.debug.message("Modify(Element): null input.");
112            throw new DiscoveryException(
113                DiscoUtils.bundle.getString("nullInput"));
114        }
115        String nodeName;
116        String nameSpaceURI;
117        if (((nodeName = root.getLocalName()) == null) ||
118            (!nodeName.equals("Modify")) ||
119            ((nameSpaceURI = root.getNamespaceURI()) == null) ||
120            (!nameSpaceURI.equals(DiscoConstants.DISCO_NS)))
121        {
122            DiscoUtils.debug.message("Modify(Element): wrong input");
123            throw new DiscoveryException(
124                DiscoUtils.bundle.getString("wrongInput"));
125        }
126
127        id = root.getAttribute("id");
128
129        NodeList contentnl = root.getChildNodes();
130        Node child;
131        for (int i = 0, length = contentnl.getLength(); i < length; i++) {
132            child = contentnl.item(i);
133            if ((nodeName = child.getLocalName()) != null) {
134                nameSpaceURI = ((Element) child).getNamespaceURI();
135                if ((nameSpaceURI == null) ||
136                    (!nameSpaceURI.equals(DiscoConstants.DISCO_NS)))
137                {
138                    if (DiscoUtils.debug.messageEnabled()) {
139                        DiscoUtils.debug.message("Modify(Element): "
140                            + "invalid namespace for node " + nodeName);
141                    }
142                    throw new DiscoveryException(
143                        DiscoUtils.bundle.getString("wrongInput"));
144                }
145                if (nodeName.equals("ResourceID")) {
146                    if ((resourceID != null) || (encryptResID != null)) {
147                        if (DiscoUtils.debug.messageEnabled()) {
148                            DiscoUtils.debug.message("Modify(Element): Included"
149                                + " more than one ResourceIDGroup element.");
150                        }
151                        throw new DiscoveryException(
152                            DiscoUtils.bundle.getString("moreResourceIDGroup"));
153                    }
154                    resourceID = new ResourceID((Element) child);
155                } else if (nodeName.equals("EncryptedResourceID")) {
156                    if ((resourceID != null) || (encryptResID != null)) {
157                        if (DiscoUtils.debug.messageEnabled()) {
158                            DiscoUtils.debug.message("Modify(Element): Included"
159                                + " more than one ResourceIDGroup element.");
160                        }
161                        throw new DiscoveryException(
162                            DiscoUtils.bundle.getString("moreResourceIDGroup"));
163                    }
164                    encryptResID = new EncryptedResourceID((Element) child);
165                } else if (nodeName.equals("InsertEntry")) {
166                    if (inserts == null) {
167                        inserts = new ArrayList();
168                    }
169                    inserts.add(new InsertEntry((Element) child));
170                } else if (nodeName.equals("RemoveEntry")) {
171                    if (removes == null) {
172                        removes = new ArrayList();
173                    }
174                    removes.add(new RemoveEntry(
175                        ((Element) child).getAttribute("entryID")));
176                } else {
177                    if (DiscoUtils.debug.messageEnabled()) {
178                        DiscoUtils.debug.message("Modify(Element): invalid"
179                            + " node" + nodeName);
180                    }
181                    throw new DiscoveryException(
182                        DiscoUtils.bundle.getString("wrongInput"));
183                }
184            } // if nodeName != null
185        } // done for the nl loop
186
187        // make sure there is a ResourceID or EncryptedResourceID
188        if ((resourceID == null) && (encryptResID == null)) {
189            if (DiscoUtils.debug.messageEnabled()) {
190                DiscoUtils.debug.message("Modify(Element): missing ResourceID "
191                    + "or EncryptedResourceID element.");
192            }
193            throw new DiscoveryException(
194                DiscoUtils.bundle.getString("missingResourceIDGroup"));
195        }
196    }
197
198    /**
199     * Gets the encrypted resource ID for the discovery resource to be modified.
200     *
201     * @return the encrypted resource ID.
202     * @see #setEncryptedResourceID(EncryptedResourceID)
203     */
204    public EncryptedResourceID getEncryptedResourceID() {
205        return encryptResID;
206    }
207
208    /**
209     * Sets the encrypted resource ID for the discovery resource to be modified.
210     *
211     * @param value the encrypted resource ID.
212     * @see #getEncryptedResourceID()
213     */
214    public void setEncryptedResourceID(EncryptedResourceID value) {
215        encryptResID = value;
216    }
217
218    /**
219     * Gets the resource ID for the discovery resource to be modified.
220     *
221     * @return resource ID for the discovery resource to be modified.
222     * @see #setResourceID(ResourceID)
223     */
224    public ResourceID getResourceID() {
225        return resourceID;
226    }
227
228    /**
229     * Sets resource ID for the discovery resource to be modified.
230     * @param resourceID resource ID for the discovery resource to be modified. 
231     * @see #getResourceID()
232     */
233    public void setResourceID(ResourceID resourceID) {
234        this.resourceID = resourceID;
235    }
236
237    /**
238     * Gets id attribute.
239     *
240     * @return id attribute.
241     */
242    public java.lang.String getId() {
243        return id;
244    }
245
246    /**
247     * Gets the value of the <code>RemoveEntry</code> property.
248     *
249     * @return List of <code>RemoveEntry</code> objects
250     * @see #setRemoveEntry(List)
251     */
252    public List getRemoveEntry() {
253        return removes;
254    }
255
256    /**
257     * Sets the value of the <code>RemoveEntry</code> property.
258     *
259     * @param removes List of <code>RemoveEntry</code> object.
260     * @see #getRemoveEntry()
261     */
262    public void setRemoveEntry(List removes) {
263        this.removes = removes;
264    }
265
266    /**
267     * Gets the value of the <code>InsertEntry</code> property.
268     *
269     * @return List of <code>InsertEntry</code> object
270     * @see #setInsertEntry(List)
271     */
272    public java.util.List getInsertEntry() {
273        return inserts;
274    }
275
276    /**
277     * Sets the value of the <code>InsertEntry</code> property.
278     *
279     * @param inserts List of <code>InsertEntry</code> object.
280     * @see #getInsertEntry()
281     */
282    public void setInsertEntry(List inserts) {
283        this.inserts = inserts;
284    }
285     
286    /**
287     * Gets string format.
288     *
289     * @return formatted String.
290     */ 
291    public java.lang.String toString() {
292        StringBuffer sb = new StringBuffer(1200);
293        sb.append("<Modify xmlns=\"").append(DiscoConstants.DISCO_NS).
294                append("\"");
295        if ((id != null) && id.length() != 0) {
296            sb.append(" id=\"").append(id).append("\"");
297        }
298        sb.append(">");
299        if (resourceID != null) {
300            sb.append(resourceID.toString());
301        } else if (encryptResID != null) {
302            sb.append(encryptResID.toString());
303        }
304        if (inserts != null) {
305            Iterator iter = inserts.iterator();
306            while (iter.hasNext()) {
307                sb.append(((InsertEntry) iter.next()).toString());
308            }
309        }
310        if (removes != null) {
311            Iterator iter1 = removes.iterator();
312            while (iter1.hasNext()) {
313                sb.append(((RemoveEntry) iter1.next()).toString());
314            }
315        }
316        sb.append("</Modify>");
317        return sb.toString();
318    }
319}