001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. The ASF licenses this file to You
004: * under the Apache License, Version 2.0 (the "License"); you may not
005: * use this file except in compliance with the License.
006: * You may obtain a copy of the License at
007: *
008: * http://www.apache.org/licenses/LICENSE-2.0
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS,
012: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013: * See the License for the specific language governing permissions and
014: * limitations under the License. For additional information regarding
015: * copyright in this work, please see the NOTICE file in the top level
016: * directory of this distribution.
017: */
018:
019: package org.apache.roller.webservices.adminapi.sdk;
020:
021: import java.io.InputStream;
022: import java.io.IOException;
023: import org.jdom.Document;
024: import org.jdom.Element;
025: import org.jdom.Text;
026: import org.jdom.input.SAXBuilder;
027: import org.jdom.JDOMException;
028: import org.apache.roller.webservices.adminapi.sdk.Entry.Attributes;
029: import org.apache.roller.webservices.adminapi.sdk.Entry.Types;
030:
031: /**
032: * This class describes a member entry.
033: * A member entry is a triple consisting of a user name, a weblog handle,
034: * and a permission.
035: */
036: public class MemberEntry extends Entry {
037: /** Member permissions */
038: public interface Permissions {
039: public static final String ADMIN = "ADMIN";
040: public static final String AUTHOR = "AUTHOR";
041: public static final String LIMITED = "LIMITED";
042: }
043:
044: static interface Tags {
045: public static final String MEMBER = "member";
046: public static final String NAME = "name";
047: public static final String HANDLE = "handle";
048: public static final String PERMISSION = "permission";
049: }
050:
051: private String name;
052: private String handle;
053: private String permission;
054:
055: public MemberEntry(Element e, String urlPrefix) {
056: populate(e, urlPrefix);
057: }
058:
059: public MemberEntry(String handle, String userName, String urlPrefix) {
060: String href = urlPrefix + "/" + EntrySet.Types.MEMBERS + "/"
061: + handle + "/" + userName;
062: setHref(href);
063: setHandle(handle);
064: setName(userName);
065: }
066:
067: public MemberEntry(InputStream stream, String urlPrefix)
068: throws JDOMException, IOException {
069: SAXBuilder sb = new SAXBuilder();
070: Document d = sb.build(stream);
071: Element e = d.detachRootElement();
072:
073: populate(e, urlPrefix);
074: }
075:
076: private void populate(Element e, String urlPrefix) {
077: // all required
078:
079: // name
080: Element nameElement = e.getChild(Tags.NAME, NAMESPACE);
081: if (nameElement != null) {
082: setName(nameElement.getText());
083: }
084:
085: // handle
086: Element handleElement = e.getChild(Tags.HANDLE, NAMESPACE);
087: if (handleElement != null) {
088: setHandle(handleElement.getText());
089: }
090:
091: // href
092: setHref(urlPrefix + "/" + EntrySet.Types.MEMBERS + "/"
093: + getHandle() + "/" + getName());
094:
095: // permission
096: Element permissionElement = e.getChild(Tags.PERMISSION,
097: NAMESPACE);
098: if (permissionElement != null) {
099: setPermission(permissionElement.getText());
100: }
101: }
102:
103: public String getType() {
104: return Types.MEMBER;
105: }
106:
107: public Document toDocument() {
108: Element member = new Element(Tags.MEMBER, NAMESPACE);
109: Document doc = new Document(member);
110:
111: // href
112: member.setAttribute(Attributes.HREF, getHref());
113:
114: // name
115: String name = getName();
116: if (name != null) {
117: Element nameElement = new Element(Tags.NAME,
118: Service.NAMESPACE);
119: Text nameText = new Text(name);
120: nameElement.addContent(nameText);
121: member.addContent(nameElement);
122: }
123:
124: // handle
125: String handle = getHandle();
126: if (handle != null) {
127: Element handleElement = new Element(Tags.HANDLE, NAMESPACE);
128: Text handleText = new Text(handle);
129: handleElement.addContent(handleText);
130: member.addContent(handleElement);
131: }
132:
133: // permission
134: String perm = getPermission();
135: if (perm != null) {
136: Element permissionElement = new Element(Tags.PERMISSION,
137: NAMESPACE);
138: Text permissionText = new Text(perm);
139: permissionElement.addContent(permissionText);
140: member.addContent(permissionElement);
141: }
142:
143: return doc;
144: }
145:
146: public boolean equals(Object o) {
147: if (o == null || o.getClass() != this .getClass()) {
148: return false;
149: }
150:
151: MemberEntry other = (MemberEntry) o;
152:
153: if (!areEqual(getHandle(), other.getHandle())) {
154: return false;
155: }
156: if (!areEqual(getName(), other.getName())) {
157: return false;
158: }
159: if (!areEqual(getPermission(), other.getPermission())) {
160: return false;
161: }
162:
163: return super .equals(o);
164: }
165:
166: public String getName() {
167: return name;
168: }
169:
170: public void setName(String name) {
171: this .name = name;
172: }
173:
174: public String getHandle() {
175: return handle;
176: }
177:
178: public void setHandle(String handle) {
179: this .handle = handle;
180: }
181:
182: public String getPermission() {
183: return permission;
184: }
185:
186: public void setPermission(String permission) {
187: this.permission = permission;
188: }
189: }
|