added delete command to lists and to usergroups

This commit is contained in:
jomu
2015-02-05 18:03:36 +00:00
parent 30261f2819
commit 316bbcfede
7 changed files with 115 additions and 17 deletions

View File

@ -40,6 +40,7 @@ public class ServiceLogHandler implements SOAPHandler<SOAPMessageContext> {
@Override @Override
public boolean handleMessage(SOAPMessageContext arg0) { public boolean handleMessage(SOAPMessageContext arg0) {
/* // TODO make configrable via logger
SOAPMessage message = arg0.getMessage(); SOAPMessage message = arg0.getMessage();
boolean isOutboundMessage = (Boolean) arg0.get(MessageContext.MESSAGE_OUTBOUND_PROPERTY); boolean isOutboundMessage = (Boolean) arg0.get(MessageContext.MESSAGE_OUTBOUND_PROPERTY);
if (isOutboundMessage) { if (isOutboundMessage) {
@ -52,7 +53,8 @@ public class ServiceLogHandler implements SOAPHandler<SOAPMessageContext> {
System.out.println(); System.out.println();
} catch (SOAPException | IOException e) { } catch (SOAPException | IOException e) {
// TODO add error handling // TODO add error handling
} }
*/
return true; return true;
} }

View File

@ -138,8 +138,7 @@ public class SPList extends SPJaxbObject<List> {
KeyManagementException, JAXBException, MalformedURLException, SAXException, ParserConfigurationException, IOException { KeyManagementException, JAXBException, MalformedURLException, SAXException, ParserConfigurationException, IOException {
SPBatch batch = new SPBatch(getContext()); SPBatch batch = new SPBatch(getContext());
for (Integer batchId = 1; batchId <= dataList.size(); for (Integer batchId = 1; batchId <= dataList.size(); batchId++) {
batchId++) {
Method method = new Method(); Method method = new Method();
method.setID(batchId); method.setID(batchId);
method.setCmd(MethodType.NEW); method.setCmd(MethodType.NEW);
@ -156,11 +155,22 @@ public class SPList extends SPJaxbObject<List> {
}); });
batch.addMethod(method); batch.addMethod(method);
} }
UpdateListItems.Updates updates = new UpdateListItems.Updates();
Node node = batch.createSharePointCAMLNode();
updates.getContent().add(node);
getListsPort().updateListItems(getValue().name, updates); executeUpdate(batch);
}
public void removeListItem(Map<String, String> dataMap) throws JAXBException, ParserConfigurationException, NoSuchAlgorithmException,
SAXException, KeyManagementException, IOException {
java.util.List<Map<String, String>> dataList = new ArrayList<>();
dataList.add(dataMap);
removeListItems(dataList);
}
public void removeListItems(java.util.List<Map<String, String>> dataList) throws JAXBException, ParserConfigurationException, NoSuchAlgorithmException,
SAXException, KeyManagementException, IOException {
SPBatch batch = createBatch(dataList, MethodType.DELETE);
executeUpdate(batch);
} }
/* *** information & tools *** */ /* *** information & tools *** */
@ -249,6 +259,37 @@ public class SPList extends SPJaxbObject<List> {
} }
} }
private SPBatch createBatch(java.util.List<Map<String, String>> dataList, MethodType methodType) throws JAXBException {
SPBatch batch = new SPBatch(getContext());
for (Integer batchId = 1; batchId <= dataList.size(); batchId++) {
Method method = new Method();
method.setID(batchId);
method.setCmd(methodType);
Map<String, String> data = dataList.get(batchId - 1);
data.keySet().stream().
map((String key) -> {
Field field = new Field();
field.setName(key.replace(" ", "_x0020_"));
field.setContent(data.get(key));
return field;
}).
forEach((Field field) -> {
method.getFields().add(field);
});
batch.addMethod(method);
}
return batch;
}
private void executeUpdate(SPBatch batch) throws ParserConfigurationException, NoSuchAlgorithmException, SAXException, KeyManagementException, IOException,
JAXBException {
UpdateListItems.Updates updates = new UpdateListItems.Updates();
Node node = batch.createSharePointCAMLNode();
updates.getContent().add(node);
getListsPort().updateListItems(getValue().name, updates);
}
/* *** unsorted *** */ /* *** unsorted *** */
} }

View File

@ -108,7 +108,7 @@ public class SPLists extends SPJaxbObject<de.muehlencord.shared.sharepoint.api.l
// Parse XML file // Parse XML file
Element rootElement = (Element) content; Element rootElement = (Element) content;
String listsString = SPObject.xmlToString(rootElement.getOwnerDocument()); String listsString = SPObject.xmlToString(rootElement.getOwnerDocument());
System.out.println(listsString); // System.out.println(listsString);
this.setValue(listsString); this.setValue(listsString);
} }
} }

View File

@ -25,7 +25,7 @@ public class SPSite extends SPObject {
public void getRootWeb() throws NoSuchAlgorithmException, KeyManagementException, MalformedURLException { public void getRootWeb() throws NoSuchAlgorithmException, KeyManagementException, MalformedURLException {
String rootWebUrl = StringUtils.removeEndIgnoreCase(getContext().getSiteURL().toString(), "/"); String rootWebUrl = StringUtils.removeEndIgnoreCase(getContext().getSiteURL().toString(), "/");
GetWebResponse.GetWebResult result = getWebsPort(getContext().getSiteURL()).getWeb(rootWebUrl); GetWebResponse.GetWebResult result = getWebsPort(getContext().getSiteURL()).getWeb(rootWebUrl);
System.out.println (result); // System.out.println (result);
} }
private WebsSoap getWebsPort(URL webUrl) throws NoSuchAlgorithmException, KeyManagementException, MalformedURLException { private WebsSoap getWebsPort(URL webUrl) throws NoSuchAlgorithmException, KeyManagementException, MalformedURLException {

View File

@ -1,5 +1,6 @@
package de.muehlencord.shared.sharepoint.api.usergroup; package de.muehlencord.shared.sharepoint.api.usergroup;
import com.microsoft.schemas.sharepoint.soap.usergroup.GetUserCollectionFromGroupResponse;
import com.microsoft.schemas.sharepoint.soap.usergroup.GetUserInfoResponse; import com.microsoft.schemas.sharepoint.soap.usergroup.GetUserInfoResponse;
import com.microsoft.schemas.sharepoint.soap.usergroup.UserGroup; import com.microsoft.schemas.sharepoint.soap.usergroup.UserGroup;
import com.microsoft.schemas.sharepoint.soap.usergroup.UserGroupSoap; import com.microsoft.schemas.sharepoint.soap.usergroup.UserGroupSoap;
@ -15,6 +16,9 @@ import javax.xml.parsers.ParserConfigurationException;
import javax.xml.ws.BindingProvider; import javax.xml.ws.BindingProvider;
import javax.xml.ws.handler.Handler; import javax.xml.ws.handler.Handler;
import org.w3c.dom.Element; import org.w3c.dom.Element;
import org.w3c.dom.NamedNodeMap;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException; import org.xml.sax.SAXException;
/** /**
@ -31,11 +35,45 @@ public class SPUserGroup extends SPObject {
NoSuchAlgorithmException, IOException, KeyManagementException { NoSuchAlgorithmException, IOException, KeyManagementException {
SPUser user = getUser(userLoginName); SPUser user = getUser(userLoginName);
if (user == null) { if (user == null) {
throw new SAXException ("User not found"); throw new SAXException("User not found");
} }
getUserGroupPort().addUserToGroup(groupName, user.getUserName(), user.getUserLoginName(), user.getUserEmail(), user.getUserNotes()); getUserGroupPort().addUserToGroup(groupName, user.getUserName(), user.getUserLoginName(), user.getUserEmail(), user.getUserNotes());
} }
public void removeUserFromGroup(String userLoginName, String groupName) throws NoSuchAlgorithmException, KeyManagementException, MalformedURLException,
JAXBException, SAXException {
SPUser user = getUser(userLoginName);
if (user == null) {
throw new SAXException("User not found");
}
getUserGroupPort().removeUserFromGroup(groupName, user.getUserLoginName());
}
public boolean isUserMemberOfGroup(String userLoginName, String groupName) throws NoSuchAlgorithmException, KeyManagementException, MalformedURLException {
GetUserCollectionFromGroupResponse.GetUserCollectionFromGroupResult result = getUserGroupPort().getUserCollectionFromGroup(groupName);
if ((result != null) && (result.getContent() != null) && (!result.getContent().isEmpty())) {
Object value = result.getContent().get(0);
if (value instanceof Element) {
Element rootElement = (Element) value;
Node usersNode = rootElement.getFirstChild(); // users node
NodeList userNodeList = usersNode.getChildNodes(); // all user nodes
for (int i = 0; i < userNodeList.getLength(); i++) {
Node userNode = userNodeList.item(i);
NamedNodeMap attributes = userNode.getAttributes();
if ((attributes.getNamedItem("LoginName") != null)
&& (attributes.getNamedItem("LoginName").getNodeValue().equals("i:0#.w|" + userLoginName))) {
return true; // user found
} else {
}
}
}
return false;
} else {
return false;
}
}
public String getUserId(String userLoginName) throws NoSuchAlgorithmException, KeyManagementException, MalformedURLException, JAXBException, SAXException { public String getUserId(String userLoginName) throws NoSuchAlgorithmException, KeyManagementException, MalformedURLException, JAXBException, SAXException {
SPUser user = getUser(userLoginName); SPUser user = getUser(userLoginName);
if (user == null) { if (user == null) {
@ -45,6 +83,15 @@ public class SPUserGroup extends SPObject {
} }
} }
public String getUserName(String userLoginName) throws NoSuchAlgorithmException, KeyManagementException, MalformedURLException, JAXBException, SAXException {
SPUser user = getUser(userLoginName);
if (user == null) {
return null;
} else {
return user.getUserName();
}
}
private SPUser getUser(String userLoginName) throws NoSuchAlgorithmException, KeyManagementException, MalformedURLException, JAXBException, SAXException { private SPUser getUser(String userLoginName) throws NoSuchAlgorithmException, KeyManagementException, MalformedURLException, JAXBException, SAXException {
GetUserInfoResponse.GetUserInfoResult result = getUserGroupPort().getUserInfo("i:0#.w|" + userLoginName); GetUserInfoResponse.GetUserInfoResult result = getUserGroupPort().getUserInfo("i:0#.w|" + userLoginName);
// TODO why is i:0#.w| in front? // TODO why is i:0#.w| in front?

View File

@ -11,7 +11,7 @@
<s:element minOccurs="0" maxOccurs="1" name="GetUserCollectionFromSiteResult"> <s:element minOccurs="0" maxOccurs="1" name="GetUserCollectionFromSiteResult">
<s:complexType mixed="true"> <s:complexType mixed="true">
<s:sequence> <s:sequence>
<s:any/> <s:any processContents='skip' />
</s:sequence> </s:sequence>
</s:complexType> </s:complexType>
</s:element> </s:element>
@ -27,7 +27,7 @@
<s:element minOccurs="0" maxOccurs="1" name="GetUserCollectionFromWebResult"> <s:element minOccurs="0" maxOccurs="1" name="GetUserCollectionFromWebResult">
<s:complexType mixed="true"> <s:complexType mixed="true">
<s:sequence> <s:sequence>
<s:any/> <s:any processContents='skip' />
</s:sequence> </s:sequence>
</s:complexType> </s:complexType>
</s:element> </s:element>
@ -43,7 +43,7 @@
<s:element minOccurs="0" maxOccurs="1" name="GetAllUserCollectionFromWebResult"> <s:element minOccurs="0" maxOccurs="1" name="GetAllUserCollectionFromWebResult">
<s:complexType mixed="true"> <s:complexType mixed="true">
<s:sequence> <s:sequence>
<s:any/> <s:any processContents='skip' />
</s:sequence> </s:sequence>
</s:complexType> </s:complexType>
</s:element> </s:element>
@ -63,7 +63,7 @@
<s:element minOccurs="0" maxOccurs="1" name="GetUserCollectionFromGroupResult"> <s:element minOccurs="0" maxOccurs="1" name="GetUserCollectionFromGroupResult">
<s:complexType mixed="true"> <s:complexType mixed="true">
<s:sequence> <s:sequence>
<s:any/> <s:any processContents='skip' />
</s:sequence> </s:sequence>
</s:complexType> </s:complexType>
</s:element> </s:element>
@ -83,7 +83,7 @@
<s:element minOccurs="0" maxOccurs="1" name="GetUserCollectionFromRoleResult"> <s:element minOccurs="0" maxOccurs="1" name="GetUserCollectionFromRoleResult">
<s:complexType mixed="true"> <s:complexType mixed="true">
<s:sequence> <s:sequence>
<s:any/> <s:any processContents='skip' />
</s:sequence> </s:sequence>
</s:complexType> </s:complexType>
</s:element> </s:element>

View File

@ -33,7 +33,15 @@ public class SPUserGroupTest extends BaseTest {
@Ignore // Depends on available sharepoint currently @Ignore // Depends on available sharepoint currently
public void testAddUserToGroup() throws Exception { public void testAddUserToGroup() throws Exception {
SPUserGroup ug = new SPUserGroup(context); SPUserGroup ug = new SPUserGroup(context);
ug.addUserToGroup("wincor-nixdorf\\joern.muehlencord", "Test Group"); ug.addUserToGroup("wincor-nixdorf\\joern.muehlencord", "Test Group");
}
@Test
public void testIsUserMemberOfGroup() throws Exception {
SPUserGroup ug = new SPUserGroup(context);
boolean result = ug.isUserMemberOfGroup("wincor-nixdorf\\joern.muehlencord", "HQ All Members");
assertEquals ("HQ All membership", true, result);
} }
} }