All Downloads are FREE. Search and download functionalities are using the official Maven repository.

edu.internet2.middleware.grouper.ui.util.MembershipExporter Maven / Gradle / Ivy

The newest version!
/*******************************************************************************
 * Copyright 2012 Internet2
 * 
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *   http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 ******************************************************************************/
/*
Copyright 2004-2007 University Corporation for Advanced Internet Development, Inc.
Copyright 2004-2007 The University Of Bristol

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

  http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package edu.internet2.middleware.grouper.ui.util;

import java.io.IOException;
import java.io.Serializable;

import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Set;

import java.util.List;
import java.util.Map;
import java.util.MissingResourceException;
import java.util.ResourceBundle;

import org.apache.commons.lang.StringUtils;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.NodeList;

import edu.internet2.middleware.grouper.Group;
import edu.internet2.middleware.grouper.GroupFinder;
import edu.internet2.middleware.grouper.GrouperSession;
import edu.internet2.middleware.grouper.Member;
import edu.internet2.middleware.grouper.Membership;
import edu.internet2.middleware.grouper.SubjectFinder;
import edu.internet2.middleware.grouper.exception.MemberNotFoundException;
import edu.internet2.middleware.grouper.ui.GrouperUiFilter;
import edu.internet2.middleware.subject.Subject;
import edu.internet2.middleware.subject.SubjectNotFoundException;

/**
 * Reads an XML configuration file and exports membership data as specified. The configuration 
 * file is determined by looking up the media.properties key 'membership-export.config'.
 * The base Grouper UI distribution does not have a value set. If you want to allow 
 * membership export you must configure one or more formats appropriate to your site. 
 * 
<membership-export>
    <format  name="CSV (Open with Excel)" separator="," 
            quote="true" 
        extension=".csv" 
        content-type="application/ms-excel"
    >
<headers>
<header name="Id"/>
<header name="Name"/>
<header name="Type"/>
</headers>
<source id="g:gsa">
<field name="id"/>
<field name="displayName"/>
<field value="group"/>
</source>
<source id="qsuob">
<field name="id"/>
<field name="name"/>
<field value="person"/>
</source>
</format> </membership-export>

Currently only simple delimited files are supported

format tag

name is the text seen by the user to identify this format - unless there is only one format, in which case it is used as the default
quote=true indicates that double-quotes will surround each exported field.
extension determines the file extension that will be presented to the web browser - which helps the browser choose the correct application to open, and in the case where an application recognizes different formats, lets the application know the format to expect
content-type if configured, is sent as an HTTP header. This will determine how a web browser tries to handle the data. If no content-type is specified, it will be displayed in the UI as a normal page.
separator String used to separate fields - typically a comma or tab (\t)

headers tag

Optionally specifies column headings

source tag

Specifies which fields should be exported for Subjects with the specified source id. If a Subject to be exported has a source which has no configuration it is ignored.

Each source should specify the same number of fields, which should, if they are present, match the number of header fields specified.

field tag

name the name of the subject attribute, the value of which will be exported
value if name is not specified then the text in the value attribute will be used, as is. This allows 'padding' where Subjects from different sources may nnot always have equivalent fields
*

* * @author Gary Brown. * @version $Id: MembershipExporter.java,v 1.7 2009-10-16 10:30:08 isgwb Exp $ */ public class MembershipExporter implements Serializable{ private boolean active=false; //MCH 20090811 XXX THIS IS NOT SERIALIZABLE, SHOULDNT BE IN SESSION!!!! private transient Document configXml; private transient Map formatCache = new HashMap(); private transient Map fieldsCache = new HashMap(); private String separator=","; private String contextType=null; private boolean quote=false; public static void main(String[] args) throws Exception{ Subject gs = SubjectFinder.findById("GrouperSystem", true); GrouperSession s = GrouperSession.start(gs); Group g = GroupFinder.findByName(s,"qsuob:all", true); Set members=g.getMembers(); PrintWriter writer = new PrintWriter(System.out); ResourceBundle bundle = ResourceBundle.getBundle("resources/grouper/media"); MembershipExporter export= new MembershipExporter(); export.export("Minimal",members,writer); writer.flush(); s.stop(); } /** * */ public MembershipExporter() throws Exception{ super(); init(); } private void init() throws Exception{ if(configXml!=null) { return; } String configResource = null; try { configResource=GrouperUiFilter.retrieveSessionMediaResourceBundle().getString("membership-export.config"); }catch(MissingResourceException e){ return; } configXml = DOMHelper.getDomFromResourceOnClassPath(configResource); List available = getAvailableFormats(); if(!available.isEmpty()) active=true; } public boolean isActive() { return active; } public List getAvailableFormats() throws Exception{ List res = new ArrayList(); init(); NodeList nl = configXml.getElementsByTagName("format"); Element el =null; for (int i=0; i0) writer.print(sep); if(quote) writer.print('"'); writer.print(headers.get(i)); if(quote) writer.print('"'); } if(!headers.isEmpty()) writer.print("\n"); } private void printSubject(PrintWriter writer,String name,Subject subject,String sep) throws Exception,IOException { List fields = getFields(name,subject); if(fields==null) return; Element field; String val="???"; String fieldName; for(int i=0;i0) writer.print(sep); if(quote) writer.print('"'); if(field.hasAttribute("value")) { writer.print(field.getAttribute("value")); }else{ val="???"; fieldName=field.getAttribute("name"); if("name".equals(fieldName)) { val=subject.getName(); }else if("description".equals(fieldName)) { val=subject.getDescription(); }else if("id".equals(fieldName)) { val=subject.getId(); }else { try { val=subject.getAttributeValue(fieldName); }catch(Exception e){} } if(val==null) val="???"; if(quote && val.indexOf("\"") > -1) { writer.print(val.replaceAll("\"","\"\"")); }else{ writer.print(val); } } if(quote) writer.print('"'); } writer.print("\n"); } private Element getFormat(String name) throws Exception{ init(); return (Element)formatCache.get(name); } private List getFields(String format,Subject subject) throws Exception{ String lookup = format + ":" + subject.getSource().getId(); if("".equals(fieldsCache.get(lookup))) return null; List fields = (List)fieldsCache.get(lookup); if(fields==null) { boolean ok=false; Element formatElement = getFormat(format); NodeList nl = formatElement.getElementsByTagName("source"); NodeList fieldList; Element fieldElement; Element sourceElement; for (int i=0;i





© 2015 - 2024 Weber Informatics LLC | Privacy Policy