/* ======================================================================
   Parts Copyright 2006 University of Leeds, Oxford University, University of the Highlands and Islands.

   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 org.bodington.server.userimport.oucsir;

import java.io.IOException;
import java.util.Map;

import org.apache.log4j.Logger;
import org.bodington.server.realm.Alias;
import org.bodington.server.realm.Zone;
import org.xmlpull.v1.XmlPullParser;
import org.xmlpull.v1.XmlPullParserException;

/**
 * This parser reads the XML users file supplied by registration and gives
 * the caller back the users.
 * @author buckett
 */
class OUCSUsersParser extends XppIterator
{
  
    private static Logger log = Logger.getLogger(OUCSUsersParser.class);
    
    private Map colleges;
    private Map departments;
    private Map courses;
    private Map statuses;
    private Zone zone;
    private Alias barcodeAlias;
    private Alias personCodeAlias;
    private Alias cardIdAlias;


    protected Object loadNext()
    {
        OUCSGeneratedUser user = new OUCSGeneratedUser();

        try
        {
            // Find the next ROW
            while (xpp.getEventType() != XmlPullParser.END_DOCUMENT)
            {

                if (xpp.getEventType() == XmlPullParser.START_TAG)
                {
                    try
                    {
                        if (("ROW").equalsIgnoreCase(xpp.getName()))
                            user = new OUCSGeneratedUser();
                        else if (checkTagAndProgress("SURNAME"))
                            user.setSurname(xpp.getText());
                        else if (checkTagAndProgress("DISPLAY_NAME"))
                            user.setDisplayName(xpp.getText());
                        else if (checkTagAndProgress("INITIALS"))
                            user.setInitials(xpp.getText());
                        else if (checkTagAndProgress("COLLEGE"))
                            user.setCollege((OUCSCollege)colleges.get(xpp.getText()));
                        else if (checkTagAndProgress("EMAIL"))
                            user.setEmail(xpp.getText());
                        else if (checkTagAndProgress("CARD_ID"))
                            user.setCardId(xpp.getText());
                        else if (checkTagAndProgress("OSS_ID"))
                            user.setOSSId(xpp.getText());
                        else if (checkTagAndProgress("PCODE"))
                            user.setPersonCode(xpp.getText());
                        else if (checkTagAndProgress("BARCODE"))
                            user.setBarcode(xpp.getText());
                        else if (checkTagAndProgress("OSS_COURSE_CODE"))
                            user.setCourse((OUCSCourse)courses.get(xpp.getText()));
                        else if (checkTagAndProgress("USERNAME"))
                            user.setUsername(xpp.getText());
                        else if (checkTagAndProgress("DEPT"))
                            user.setDepartment((OUCSDepartment)departments.get(xpp.getText()));
                        else if (checkTagAndProgress("YEAR_OF_COURSE"))
                            user.setCourseYear(Integer.parseInt(xpp.getText()));
                        else if (checkTagAndProgress("CARD_STATUS"))
                            user.setStatus((OUCSStatus)statuses.get(xpp.getText()));

                    } 
                    catch (IllegalStateException ise)
                    {
                    	log.error(getXppPosition()+ "Data format error: "+ ise.getMessage());
                    }
                    catch (NumberFormatException nfe)
                    {
                    	log.error(getXppPosition()+ "Can't convert to number");
                    }
                }
                else if (xpp.getEventType() == XmlPullParser.END_TAG)
                {
                    if ("ROW".equals(xpp.getName()))
                    {
                        // TODO Process User
                        xpp.next();
                        // As we don't
                        user.setZone(zone);
                        user.setPersonCodeAlias(personCodeAlias);
                        return user;
                    }
                }

                xpp.next();
            }
        }
        catch (IOException ioe)
        {
        	log.error("Problem reading source: "+ ioe.getMessage());
        }
        catch (XmlPullParserException xppe)
        {
            log.error("Problem with XML format: "+xppe.getMessage());
        }


        return null;
    }
    
    /**
     * @param colleges the colleges to set
     */
    void setColleges(Map colleges)
    {
        this.colleges = colleges;
    }

    /**
     * @param courses the courses to set
     */
    void setCourses(Map courses)
    {
        this.courses = courses;
    }

    /**
     * @param departments the departments to set
     */
    void setDepartments(Map departments)
    {
        this.departments = departments;
    }

    /**
     * @param statuses the statuses to set
     */
    void setStatuses(Map statuses)
    {
        this.statuses = statuses;
    }
    
    /**
     * 
     * @param zone this zone that users are created in.
     */
    void setZone(Zone zone)
    {
        this.zone = zone;
    }

    /**
     * @param barcodeAlias the barcodeAlias to set
     */
    void setBarcodeAlias(Alias barcodeAlias)
    {
        this.barcodeAlias = barcodeAlias;
    }

    /**
     * @param cardIdAlias the cardIdAlias to set
     */
    void setCardIdAlias(Alias cardIdAlias)
    {
        this.cardIdAlias = cardIdAlias;
    }

    /**
     * @param personCodeAlias the personCodeAlias to set
     */
    void setPersonCodeAlias(Alias personCodeAlias)
    {
        this.personCodeAlias = personCodeAlias;
    }
}
