Example usage for java.util Stack peek

List of usage examples for java.util Stack peek

Introduction

In this page you can find the example usage for java.util Stack peek.

Prototype

public synchronized E peek() 

Source Link

Document

Looks at the object at the top of this stack without removing it from the stack.

Usage

From source file:org.apache.tajo.plan.LogicalPlanner.java

private LogicalNode insertWindowAggNode(PlanContext context, LogicalNode child, Stack<Expr> stack,
        String[] referenceNames, ExprNormalizer.WindowSpecReferences[] windowSpecReferenceses)
        throws TajoException {
    LogicalPlan plan = context.plan;/*from   w ww  . j  av a 2  s .c  o m*/
    QueryBlock block = context.queryBlock;
    WindowAggNode windowAggNode = context.plan.createNode(WindowAggNode.class);
    if (child.getType() == NodeType.LIMIT) {
        LimitNode limitNode = (LimitNode) child;
        windowAggNode.setChild(limitNode.getChild());
        windowAggNode.setInSchema(limitNode.getChild().getOutSchema());
        limitNode.setChild(windowAggNode);
    } else if (child.getType() == NodeType.SORT) {
        SortNode sortNode = (SortNode) child;
        windowAggNode.setChild(sortNode.getChild());
        windowAggNode.setInSchema(sortNode.getChild().getOutSchema());
        sortNode.setChild(windowAggNode);
    } else {
        windowAggNode.setChild(child);
        windowAggNode.setInSchema(child.getOutSchema());
    }

    List<String> winFuncRefs = new ArrayList<String>();
    List<WindowFunctionEval> winFuncs = new ArrayList<WindowFunctionEval>();
    List<WindowSpec> rawWindowSpecs = Lists.newArrayList();
    for (Iterator<NamedExpr> it = block.namedExprsMgr.getIteratorForUnevaluatedExprs(); it.hasNext();) {
        NamedExpr rawTarget = it.next();
        try {
            EvalNode evalNode = exprAnnotator.createEvalNode(context, rawTarget.getExpr(),
                    NameResolvingMode.SUBEXPRS_AND_RELS);
            if (evalNode.getType() == EvalType.WINDOW_FUNCTION) {
                winFuncRefs.add(rawTarget.getAlias());
                winFuncs.add((WindowFunctionEval) evalNode);
                block.namedExprsMgr.markAsEvaluated(rawTarget.getAlias(), evalNode);

                // TODO - Later, we also consider the possibility that a window function contains only a window name.
                rawWindowSpecs.add(((WindowFunctionExpr) (rawTarget.getExpr())).getWindowSpec());
            }
        } catch (UndefinedColumnException uc) {
        }
    }

    // we only consider one window definition.
    if (windowSpecReferenceses[0].hasPartitionKeys()) {
        Column[] partitionKeyColumns = new Column[windowSpecReferenceses[0].getPartitionKeys().length];
        int i = 0;
        for (String partitionKey : windowSpecReferenceses[0].getPartitionKeys()) {
            if (block.namedExprsMgr.isEvaluated(partitionKey)) {
                partitionKeyColumns[i++] = block.namedExprsMgr.getTarget(partitionKey).getNamedColumn();
            } else {
                throw makeSyntaxError("Each grouping column expression must be a scalar expression.");
            }
        }
        windowAggNode.setPartitionKeys(partitionKeyColumns);
    }

    SortSpec[][] sortGroups = new SortSpec[rawWindowSpecs.size()][];

    for (int winSpecIdx = 0; winSpecIdx < rawWindowSpecs.size(); winSpecIdx++) {
        WindowSpec spec = rawWindowSpecs.get(winSpecIdx);
        if (spec.hasOrderBy()) {
            Sort.SortSpec[] sortSpecs = spec.getSortSpecs();
            int sortNum = sortSpecs.length;
            String[] sortKeyRefNames = windowSpecReferenceses[winSpecIdx].getOrderKeys();
            SortSpec[] annotatedSortSpecs = new SortSpec[sortNum];

            Column column;
            for (int i = 0; i < sortNum; i++) {
                if (block.namedExprsMgr.isEvaluated(sortKeyRefNames[i])) {
                    column = block.namedExprsMgr.getTarget(sortKeyRefNames[i]).getNamedColumn();
                } else {
                    throw new IllegalStateException("Unexpected State: " + StringUtils.join(sortSpecs));
                }
                annotatedSortSpecs[i] = new SortSpec(column, sortSpecs[i].isAscending(),
                        sortSpecs[i].isNullFirst());
            }

            sortGroups[winSpecIdx] = annotatedSortSpecs;
        } else {
            sortGroups[winSpecIdx] = null;
        }
    }

    for (int i = 0; i < winFuncRefs.size(); i++) {
        WindowFunctionEval winFunc = winFuncs.get(i);
        if (sortGroups[i] != null) {
            winFunc.setSortSpecs(sortGroups[i]);
        }
    }

    Target[] targets = new Target[referenceNames.length];
    List<Integer> windowFuncIndices = Lists.newArrayList();
    Projection projection = (Projection) stack.peek();
    int windowFuncIdx = 0;
    for (NamedExpr expr : projection.getNamedExprs()) {
        if (expr.getExpr().getType() == OpType.WindowFunction) {
            windowFuncIndices.add(windowFuncIdx);
        }
        windowFuncIdx++;
    }
    windowAggNode.setWindowFunctions(winFuncs.toArray(new WindowFunctionEval[winFuncs.size()]));

    int targetIdx = 0;
    for (int i = 0; i < referenceNames.length; i++) {
        if (!windowFuncIndices.contains(i)) {
            if (block.isConstReference(referenceNames[i])) {
                targets[targetIdx++] = new Target(block.getConstByReference(referenceNames[i]),
                        referenceNames[i]);
            } else {
                targets[targetIdx++] = block.namedExprsMgr.getTarget(referenceNames[i]);
            }
        }
    }
    for (int i = 0; i < winFuncRefs.size(); i++) {
        targets[targetIdx++] = block.namedExprsMgr.getTarget(winFuncRefs.get(i));
    }
    windowAggNode.setTargets(targets);
    verifyProjectedFields(block, windowAggNode);

    block.registerNode(windowAggNode);
    postHook(context, stack, null, windowAggNode);

    if (child.getType() == NodeType.LIMIT) {
        LimitNode limitNode = (LimitNode) child;
        limitNode.setInSchema(windowAggNode.getOutSchema());
        limitNode.setOutSchema(windowAggNode.getOutSchema());
        return null;
    } else if (child.getType() == NodeType.SORT) {
        SortNode sortNode = (SortNode) child;
        sortNode.setInSchema(windowAggNode.getOutSchema());
        sortNode.setOutSchema(windowAggNode.getOutSchema());
        return null;
    } else {
        return windowAggNode;
    }
}

From source file:org.sakaiproject.calendar.impl.BaseCalendarService.java

/**
 * {@inheritDoc}/*from  www . j  a  v  a  2  s. c o  m*/
 */
public String archive(String siteId, Document doc, Stack stack, String archivePath, List attachments) {
    // prepare the buffer for the results log
    StringBuilder results = new StringBuilder();

    // start with an element with our very own (service) name
    Element element = doc.createElement(CalendarService.class.getName());
    ((Element) stack.peek()).appendChild(element);
    stack.push(element);

    // get the channel associated with this site
    String calRef = calendarReference(siteId, SiteService.MAIN_CONTAINER);

    results.append("archiving calendar " + calRef + ".\n");

    try {
        // do the channel
        Calendar cal = getCalendar(calRef);
        Element containerElement = cal.toXml(doc, stack);
        stack.push(containerElement);

        // do the messages in the channel
        Iterator events = cal.getEvents(null, null).iterator();
        while (events.hasNext()) {
            CalendarEvent event = (CalendarEvent) events.next();
            event.toXml(doc, stack);

            // collect message attachments
            List atts = event.getAttachments();
            for (int i = 0; i < atts.size(); i++) {
                Reference ref = (Reference) atts.get(i);
                // if it's in the attachment area, and not already in the list
                if ((ref.getReference().startsWith("/content/attachment/")) && (!attachments.contains(ref))) {
                    attachments.add(ref);
                }
            }
        }

        stack.pop();
    } catch (Exception any) {
        M_log.warn(".archve: exception archiving messages for service: " + CalendarService.class.getName()
                + " channel: " + calRef);
    }

    stack.pop();

    return results.toString();
}

From source file:org.sakaiproject.content.tool.ResourcesAction.java

/**
 * Access the top item on the suspended-operations stack
 * @param state The current session state, including the STATE_SUSPENDED_OPERATIONS_STACK attribute.
 * @return The top item on the stack, or null if the stack is empty.
 *///from  ww w. j av  a2s .c om
private static Map peekAtStack(SessionState state) {
    logger.debug("ResourcesAction.peekAtStack()");
    Map current_stack_frame = null;
    Stack operations_stack = (Stack) state.getAttribute(STATE_SUSPENDED_OPERATIONS_STACK);
    if (operations_stack == null) {
        operations_stack = new Stack();
        state.setAttribute(STATE_SUSPENDED_OPERATIONS_STACK, operations_stack);
    }
    if (!operations_stack.isEmpty()) {
        current_stack_frame = (Map) operations_stack.peek();
    }
    return current_stack_frame;

}

From source file:org.osmdroid.reader.Reader.java

/**
 * imports the osm bz2 file into the database
 *
 * @param path// www  .j a  va  2  s.c o  m
 * @param connection
 * @throws Exception if the file wasn't found, can't write the output file, or there's some kind of IO exception while reading
 */
public void read(String path, Connection connection) throws Exception {

    if (path == null)
        throw new IllegalArgumentException("path");
    if (!new File(path).exists())
        throw new FileNotFoundException("File Not Found");
    PreparedStatement p;
    try {
        p = connection.prepareStatement(
                "CREATE TABLE IF NOT EXISTS \"nodes\" (\"id\" INTEGER PRIMARY KEY  NOT NULL , \"lat\" DOUBLE NOT NULL , \"lon\" DOUBLE NOT NULL , \"version\" INTEGER, \"timestamp\" DATETIME, \"uid\" INTEGER, \"user\" TEXT, \"changeset\" INTEGER)");
        p.execute();
        p.close();
    } catch (Exception ex) {
        ex.printStackTrace();
    }
    try {
        p = connection.prepareStatement(
                "CREATE TABLE IF NOT EXISTS  \"relation_members\" (\"type\" TEXT NOT NULL , \"ref\" INTEGER NOT NULL , \"role\" TEXT, \"id\" INTEGER PRIMARY KEY  AUTOINCREMENT  NOT NULL )");
        p.execute();
        p.close();
    } catch (Exception ex) {
        ex.printStackTrace();
    }
    try {
        p = connection.prepareStatement(
                "CREATE TABLE IF NOT EXISTS  \"relations\" (\"id\" INTEGER PRIMARY KEY  NOT NULL , \"user\" TEXT, \"uid\" INTEGER, \"version\" INTEGER, \"changeset\" INTEGER, \"timestamp\" DATETIME)");
        p.execute();
        p.close();
    } catch (Exception ex) {
        ex.printStackTrace();
    }
    try {
        p = connection.prepareStatement(
                "CREATE TABLE IF NOT EXISTS  \"tag\" (\"id\" INTEGER NOT NULL , \"k\" TEXT NOT NULL , \"v\" TEXT NOT NULL , \"reftype\" INTEGER NOT NULL  DEFAULT -1, PRIMARY KEY( \"reftype\",\"k\" ,\"id\" )   )");
        p.execute();
        p.close();
    } catch (Exception ex) {
        ex.printStackTrace();
    }
    try {
        p = connection.prepareStatement(
                "CREATE TABLE IF NOT EXISTS  \"way_no\" (\"way_id\" INTEGER NOT NULL , \"node_id\" INTEGER NOT NULL, PRIMARY KEY (\"way_id\", \"node_id\")  )  ");
        p.execute();
        p.close();
    } catch (Exception ex) {
        ex.printStackTrace();
    }
    try {
        p = connection.prepareStatement(
                "CREATE TABLE IF NOT EXISTS  \"ways\" (\"id\" INTEGER PRIMARY KEY  NOT NULL , \"changeset\" INTEGER, \"version\" INTEGER, \"user\" TEXT, \"uid\" INTEGER, \"timestamp\" DATETIME)");
        p.execute();
        p.close();

    } catch (Exception ex) {
        ex.printStackTrace();
    }

    BufferedReader xmlInputStream = getBufferedReaderForBZ2File(path);
    XmlPullParserFactory factory = XmlPullParserFactory.newInstance();
    factory.setNamespaceAware(false);
    XmlPullParser xmlStreamReader = factory.newPullParser();

    xmlStreamReader.setInput(xmlInputStream);

    Stack<String> xpath = new Stack<String>();
    long recordCount = 0;
    long batchCount = 0;
    long lastId = -1;
    long start = System.currentTimeMillis();
    OsmType lastType = OsmType.NODE;
    long id = -1;
    long changset = -1;
    double lat = 0.0;
    double lon = 0.0;
    long version = -1;
    String user = "";
    long uid = -1;
    long inserts = 0;
    String key = "";
    String val = "";

    //int eventType = -1;
    Date timestamp = new Date(System.currentTimeMillis());
    //connection.setAutoCommit(false);
    int eventType = xmlStreamReader.getEventType();
    while (eventType != XmlPullParser.END_DOCUMENT) {
        String tagname = xmlStreamReader.getName();
        recordCount++;
        key = "";
        val = "";
        id = -1;
        changset = -1;
        version = -1;
        user = "";
        uid = -1;
        //timestamp = new Date(System.currentTimeMillis());

        //System.out.println(recordCount);
        //System.out.println ("XMLEvent " + eventType + " " + tagname);

        //long btime = System.currentTimeMillis();
        switch (eventType) {
        case XmlPullParser.START_TAG:

            if (xmlStreamReader.getName().toString().equalsIgnoreCase("osm")) {

            }

            if (xmlStreamReader.getName().toString().equalsIgnoreCase("bounds")) {

            }

            if (xmlStreamReader.getName().toString().equalsIgnoreCase("node")) {
                xpath.push(xmlStreamReader.getName().toString());
                p = connection
                        .prepareStatement("INSERT INTO nodes (id,changeset,version,user,uid,timestamp,lat,lon) "
                                + "VALUES (?,?,?,?,?,?,?,?); ");

                for (int i = 0; i < xmlStreamReader.getAttributeCount(); i++) {
                    //      System.out.println(xmlStreamReader.getAttributeName(i) + "="
                    //         + xmlStreamReader.getAttributeValue(i));

                    if (xmlStreamReader.getAttributeName(i).equalsIgnoreCase("id")) {
                        id = Long.parseLong(xmlStreamReader.getAttributeValue(i));
                    }
                    if (xmlStreamReader.getAttributeName(i).equalsIgnoreCase("user")) {
                        user = xmlStreamReader.getAttributeValue(i);
                    }
                    if (xmlStreamReader.getAttributeName(i).equalsIgnoreCase("uid")) {
                        uid = Long.parseLong(xmlStreamReader.getAttributeValue(i));
                    }
                    if (xmlStreamReader.getAttributeName(i).equalsIgnoreCase("changeset")) {
                        changset = Long.parseLong(xmlStreamReader.getAttributeValue(i));
                    }
                    if (xmlStreamReader.getAttributeName(i).equalsIgnoreCase("version")) {
                        version = Long.parseLong(xmlStreamReader.getAttributeValue(i));
                    }
                    if (xmlStreamReader.getAttributeName(i).equalsIgnoreCase("lat")) {
                        lat = Double.parseDouble(xmlStreamReader.getAttributeValue(i));
                    }
                    if (xmlStreamReader.getAttributeName(i).equalsIgnoreCase("lon")) {
                        lon = Double.parseDouble(xmlStreamReader.getAttributeValue(i));
                    }
                    if (xmlStreamReader.getAttributeName(i).equalsIgnoreCase("timestamp")) {
                        timestamp.setTime(sdf.parse(xmlStreamReader.getAttributeValue(i)).getTime());
                    }
                }
                if (id != -1) {
                    p.setLong(1, id);
                    p.setLong(2, changset);
                    p.setLong(3, version);
                    p.setString(4, user);
                    p.setLong(5, uid);
                    p.setDate(6, timestamp);
                    p.setDouble(7, lat);
                    p.setDouble(8, lon);
                    try {
                        p.executeUpdate();
                        inserts++;
                        //batchCount++;
                    } catch (Exception ex) {
                        System.out.println(p.toString());
                        ex.printStackTrace();
                    }
                    p.close();
                    p = null;
                }
                lastId = id;
                lastType = OsmType.NODE;
            } else if (xmlStreamReader.getName().toString().equalsIgnoreCase("relation")) {
                xpath.push(xmlStreamReader.getName().toString());
                p = connection
                        .prepareStatement("INSERT INTO relations (id,changeset,version,user,uid,timestamp) "
                                + "VALUES (?,?,?,?,?,?); ");

                for (int i = 0; i < xmlStreamReader.getAttributeCount(); i++) {
                    //      System.out.println(xmlStreamReader.getAttributeName(i) + "="
                    //         + xmlStreamReader.getAttributeValue(i));
                    if (xmlStreamReader.getAttributeName(i).equalsIgnoreCase("id")) {
                        id = Long.parseLong(xmlStreamReader.getAttributeValue(i));
                    } else if (xmlStreamReader.getAttributeName(i).equalsIgnoreCase("user")) {
                        user = xmlStreamReader.getAttributeValue(i);
                    } else if (xmlStreamReader.getAttributeName(i).equalsIgnoreCase("uid")) {
                        uid = Long.parseLong(xmlStreamReader.getAttributeValue(i));
                    } else if (xmlStreamReader.getAttributeName(i).equalsIgnoreCase("changeset")) {
                        changset = Long.parseLong(xmlStreamReader.getAttributeValue(i));
                    } else if (xmlStreamReader.getAttributeName(i).equalsIgnoreCase("version")) {
                        version = Long.parseLong(xmlStreamReader.getAttributeValue(i));
                    } else if (xmlStreamReader.getAttributeName(i).equalsIgnoreCase("timestamp")) {
                        timestamp.setTime(sdf.parse(xmlStreamReader.getAttributeValue(i)).getTime());
                    } else {
                        System.err.println("relation attrib unhandled " + xmlStreamReader.getAttributeName(i));
                    }
                }
                if (id != -1) {
                    p.setLong(1, id);
                    p.setLong(2, changset);
                    p.setLong(3, version);
                    p.setString(4, user);
                    p.setLong(5, uid);
                    p.setDate(6, timestamp);
                    try {
                        p.executeUpdate();
                        //   batchCount++;

                        inserts++;
                    } catch (Exception ex) {
                        System.out.println(p.toString());
                        ex.printStackTrace();
                    }
                    p.close();
                    p = null;
                }
                lastId = id;
                lastType = OsmType.RELATION;
            } else if (xmlStreamReader.getName().toString().equalsIgnoreCase("way")) {
                xpath.push(xmlStreamReader.getName().toString());
                p = connection.prepareStatement("INSERT INTO ways (id,changeset,version,user,uid,timestamp) "
                        + "VALUES (?,?,?,?,?,?); ");

                for (int i = 0; i < xmlStreamReader.getAttributeCount(); i++) {
                    //System.out.println(xmlStreamReader.getAttributeName(i) + "="
                    //   + xmlStreamReader.getAttributeValue(i));
                    if (xmlStreamReader.getAttributeName(i).equalsIgnoreCase("id")) {
                        id = Long.parseLong(xmlStreamReader.getAttributeValue(i));
                    }
                    if (xmlStreamReader.getAttributeName(i).equalsIgnoreCase("user")) {
                        user = xmlStreamReader.getAttributeValue(i);
                    }
                    if (xmlStreamReader.getAttributeName(i).equalsIgnoreCase("uid")) {
                        uid = Long.parseLong(xmlStreamReader.getAttributeValue(i));
                    }
                    if (xmlStreamReader.getAttributeName(i).equalsIgnoreCase("changeset")) {
                        changset = Long.parseLong(xmlStreamReader.getAttributeValue(i));
                    }
                    if (xmlStreamReader.getAttributeName(i).equalsIgnoreCase("version")) {
                        version = Long.parseLong(xmlStreamReader.getAttributeValue(i));
                    }
                    if (xmlStreamReader.getAttributeName(i).equalsIgnoreCase("timestamp")) {
                        timestamp.setTime(sdf.parse(xmlStreamReader.getAttributeValue(i)).getTime());
                    }
                }
                if (id != -1) {
                    p.setLong(1, id);
                    p.setLong(2, changset);
                    p.setLong(3, version);
                    p.setString(4, user);
                    p.setLong(5, uid);
                    p.setDate(6, timestamp);
                    try {
                        p.executeUpdate();
                        inserts++;
                        //    batchCount++;
                    } catch (Exception ex) {
                        System.out.println(p.toString());
                        ex.printStackTrace();
                    }
                    p.close();
                    p = null;
                }
                lastId = id;
                lastType = OsmType.WAY;

            } else if (xmlStreamReader.getName().toString().equalsIgnoreCase("tag")) {
                if (!xpath.isEmpty()
                        && ((xpath.peek().equalsIgnoreCase("way") || xpath.peek().equalsIgnoreCase("node")
                                || xpath.peek().equalsIgnoreCase("relation")) && lastId != -1)) {

                    for (int i = 0; i < xmlStreamReader.getAttributeCount(); i++) {
                        //System.out.println(xmlStreamReader.getAttributeName(i) + "="
                        //   + xmlStreamReader.getAttributeValue(i));
                        if (xmlStreamReader.getAttributeName(i).equalsIgnoreCase("k")) {
                            key = xmlStreamReader.getAttributeValue(i);
                        } else if (xmlStreamReader.getAttributeName(i).equalsIgnoreCase("v")) {
                            val = xmlStreamReader.getAttributeValue(i);
                        } else {
                            //uncaptured attribute
                            System.out.println(xmlStreamReader.getAttributeName(i) + "="
                                    + xmlStreamReader.getAttributeValue(i));
                        }

                    }
                    if (lastId != -1) {
                        p = connection
                                .prepareStatement("INSERT INTO tag (id,k,v,reftype) " + "VALUES (?,?,?,?); ");
                        p.setLong(1, lastId);
                        p.setString(2, key);
                        p.setString(3, val);
                        p.setInt(4, lastType.ordinal());
                        try {
                            p.executeUpdate();
                            inserts++;
                        } catch (Exception ex) {
                            System.out.println(p.toString());
                            ex.printStackTrace();
                        }
                        //  batchCount++;
                        p.close();
                        p = null;
                    } else {
                        System.err.println("ERR0003");
                    }
                } else {
                    System.err.println("ERR0002");
                }
            } else if (xmlStreamReader.getName().toString().equalsIgnoreCase("nd")) {
                if (xpath.peek().equalsIgnoreCase("way") && lastId != -1) {
                    id = -1;
                    for (int i = 0; i < xmlStreamReader.getAttributeCount(); i++) {
                        //   System.out.println(xmlStreamReader.getAttributeName(i) + "="
                        //      + xmlStreamReader.getAttributeValue(i));
                        if (xmlStreamReader.getAttributeName(i).equalsIgnoreCase("ref")) {
                            id = Long.parseLong(xmlStreamReader.getAttributeValue(i));
                        }

                    }
                    if (id != -1) {
                        p = connection
                                .prepareStatement("INSERT INTO way_no (way_id,node_id) " + "VALUES (?,?); ");
                        p.setLong(1, lastId);
                        p.setLong(2, id);
                        try {
                            p.executeUpdate();
                            inserts++;
                        } catch (Exception ex) {
                            System.out.println(p.toString());
                            ex.printStackTrace();
                        }
                        p.close();
                        p = null;
                        //batchCount++;
                    }
                } else {
                    System.err.println("ERR0001");
                }

            } else if (xmlStreamReader.getName().toString().equalsIgnoreCase("member")) {

                if ((xpath.peek().equalsIgnoreCase("relation")) && lastId != -1) {

                    //String type = "";
                    id = -1;
                    //String role = "";
                    for (int i = 0; i < xmlStreamReader.getAttributeCount(); i++) {
                        //   System.out.println(xmlStreamReader.getAttributeName(i) + "="
                        //      + xmlStreamReader.getAttributeValue(i));
                        if (xmlStreamReader.getAttributeName(i).equalsIgnoreCase("type")) {
                            key = xmlStreamReader.getAttributeValue(i);
                        }
                        if (xmlStreamReader.getAttributeName(i).equalsIgnoreCase("ref")) {
                            id = Long.parseLong(xmlStreamReader.getAttributeValue(i));
                        }
                        if (xmlStreamReader.getAttributeName(i).equalsIgnoreCase("role")) {
                            val = xmlStreamReader.getAttributeValue(i);
                        }

                    }
                    if (lastId != -1) {
                        p = connection.prepareStatement(
                                "INSERT INTO relation_members (id,type,ref,role) " + "VALUES (?,?,?,?); ");
                        p.setLong(1, lastId);
                        p.setString(2, key);
                        p.setLong(3, id);
                        p.setString(4, val);
                        try {
                            p.executeUpdate();
                            inserts++;
                        } catch (Exception ex) {
                            System.out.println(p.toString());
                            ex.printStackTrace();
                        }
                        p.close();
                        p = null;
                        // batchCount++;
                    } else {
                        System.err.println("ERR0006");
                    }
                } else {
                    System.err.println("ERR0005");
                }

            } else {
                System.err.println("unhandled node! " + xmlStreamReader.getName().toString());
            }

            break;
        case XmlPullParser.TEXT:
            //System.out.print("text!" + xmlStreamReader.getText());
            break;
        case XmlPullParser.END_TAG:
            //System.out.println("</" + xmlStreamReader.getName().toString() + ">");

            if (xmlStreamReader.getName().toString().equalsIgnoreCase("node")) {
                if (!xpath.isEmpty()) {
                    xpath.pop();
                }
            }
            if (xmlStreamReader.getName().toString().equalsIgnoreCase("way")) {
                if (!xpath.isEmpty()) {
                    xpath.pop();
                }
            }
            if (xmlStreamReader.getName().toString().equalsIgnoreCase("relation")) {
                if (!xpath.isEmpty()) {
                    xpath.pop();
                }
            }

            break;
        default:
            //do nothing
            break;
        }

        //if (batchCount == 100) {
        //connection.commit();
        //  long txPerSecond = (System.currentTimeMillis() - btime);
        System.out.println((start - System.currentTimeMillis()) + " total elements processed " + recordCount
                + " inserts " + inserts + " stack " + xpath.size());

        //batchCount = 0;
        //System.gc();
        //System.out.println();
        //  }
        eventType = xmlStreamReader.next();
    }

    System.out.println(System.currentTimeMillis() - start + "ms");
}

From source file:org.sakaiproject.content.impl.BaseContentService.java

/**
 * {@inheritDoc}/*ww  w .j  av  a  2 s .  c  o  m*/
 */
public String archiveResources(List attachments, Document doc, Stack stack, String archivePath) {
    // prepare the buffer for the results log
    StringBuilder results = new StringBuilder();

    // start with an element with our very own name
    Element element = doc.createElement(ContentHostingService.class.getName());
    ((Element) stack.peek()).appendChild(element);
    stack.push(element);

    for (Iterator i = attachments.iterator(); i.hasNext();) {
        Reference ref = (Reference) i.next();
        try {
            ContentResource resource = (ContentResource) ref.getEntity();

            if (resource != null) {
                results.append(archiveResource(resource, doc, stack, archivePath, null));
            }
        } catch (Exception any) {
            results.append("Error archiving resource: " + ref + " " + any.toString() + "\n");
            M_log.warn("archveResources: exception archiving resource: " + ref + ": ", any);
        }
    }

    stack.pop();

    return results.toString();
}

From source file:org.sakaiproject.content.impl.BaseContentService.java

/**
 * {@inheritDoc}//w ww  . j  a v  a  2  s . c o  m
 */
public String archive(String siteId, Document doc, Stack stack, String archivePath, List attachments) {
    // prepare the buffer for the results log
    StringBuilder results = new StringBuilder();

    // start with an element with our very own name
    Element element = doc.createElement(ContentHostingService.class.getName());
    ((Element) stack.peek()).appendChild(element);
    stack.push(element);

    // the root collection for the site
    String siteCollectionId = getSiteCollection(siteId);

    try {
        // get the collection for the site
        ContentCollection collection = getCollection(siteCollectionId);

        archiveCollection(collection, doc, stack, archivePath, siteCollectionId, results);
    } catch (Exception any) {
        results.append("Error archiving collection from site: " + siteId + " " + any.toString() + "\n");
    }

    stack.pop();

    return results.toString();

}

From source file:org.sakaiproject.assignment.impl.BaseAssignmentService.java

/**
 * {@inheritDoc}/*ww  w.j  a  v  a2  s .  com*/
 */
public String archive(String siteId, Document doc, Stack stack, String archivePath, List attachments) {
    // prepare the buffer for the results log
    StringBuilder results = new StringBuilder();

    // String assignRef = assignmentReference(siteId, SiteService.MAIN_CONTAINER);
    results.append("archiving " + getLabel() + " context " + Entity.SEPARATOR + siteId + Entity.SEPARATOR
            + SiteService.MAIN_CONTAINER + ".\n");

    // start with an element with our very own (service) name
    Element element = doc.createElement(AssignmentService.class.getName());
    ((Element) stack.peek()).appendChild(element);
    stack.push(element);

    Iterator assignmentsIterator = getAssignmentsForContext(siteId);

    while (assignmentsIterator.hasNext()) {
        Assignment assignment = (Assignment) assignmentsIterator.next();

        // archive this assignment
        Element el = assignment.toXml(doc, stack);
        element.appendChild(el);

        // in order to make the assignment.xml have a better structure
        // the content id attribute removed from the assignment node
        // the content will be a child of assignment node
        el.removeAttribute("assignmentcontent");

        // then archive the related content
        AssignmentContent content = (AssignmentContent) assignment.getContent();
        if (content != null) {
            Element contentEl = content.toXml(doc, stack);

            // assignment node has already kept the context info
            contentEl.removeAttribute("context");

            // collect attachments
            List atts = content.getAttachments();

            for (int i = 0; i < atts.size(); i++) {
                Reference ref = (Reference) atts.get(i);
                // if it's in the attachment area, and not already in the list
                if ((ref.getReference().startsWith("/content/attachment/")) && (!attachments.contains(ref))) {
                    attachments.add(ref);
                }

                // in order to make assignment.xml has the consistent format with the other xml files
                // move the attachments to be the children of the content, instead of the attributes
                String attributeString = "attachment" + i;
                String attRelUrl = contentEl.getAttribute(attributeString);
                contentEl.removeAttribute(attributeString);
                Element attNode = doc.createElement("attachment");
                attNode.setAttribute("relative-url", attRelUrl);
                contentEl.appendChild(attNode);

            } // for

            // make the content a childnode of the assignment node
            el.appendChild(contentEl);

            Iterator submissionsIterator = getSubmissions(assignment).iterator();
            while (submissionsIterator.hasNext()) {
                AssignmentSubmission submission = (AssignmentSubmission) submissionsIterator.next();

                // archive this assignment
                Element submissionEl = submission.toXml(doc, stack);
                el.appendChild(submissionEl);

            }
        } // if
    } // while

    stack.pop();

    return results.toString();

}

From source file:com.aurel.track.exchange.excel.ExcelImportBL.java

/**
 * Get the workItems list and validate if all the fields of the excel sheet
 * are correct/*from w w  w.  j av a  2 s.co  m*/
 * 
 * @param workbook
 * @param selectedSheet
 * @param personID
 * @param locale
 * @param columnIndexToFieldIDMap
 * @param fieldIDToColumnIndexMap
 * @param lastSavedIdentifierFieldIDIsSet
 * @param defaultValuesMap
 * @param invalidValueHandlingMap
 * @param gridErrorsMap
 * @param rowErrorsMap
 * @return
 */
static SortedMap<Integer, TWorkItemBean> getAndValidateGridData(Workbook workbook, Integer selectedSheet,
        Integer personID, Locale locale, Map<Integer, Integer> columnIndexToFieldIDMap,
        Map<Integer, Integer> fieldIDToColumnIndexMap, Set<Integer> lastSavedIdentifierFieldIDIsSet,
        Map<Integer, Integer> defaultValuesMap, Map<Integer, Integer> invalidValueHandlingMap,
        Map<Integer, Map<Integer, List<Integer>>> rowNoToPseudoFieldsOriginal,
        Map<Integer, Map<Integer, List<Integer>>> rowNoToPseudoFieldsExcel,
        Map<Integer, SortedMap<Integer, SortedMap<String, ErrorData>>> gridErrorsMap,
        Map<Integer, SortedSet<Integer>> rowErrorsMap, Map<Integer, SortedSet<Integer>> requiredFieldErrorsMap,
        Map<Integer, Integer> rowToParentRow) {
    SortedMap<Integer, TWorkItemBean> workItemBeansMap = new TreeMap<Integer, TWorkItemBean>();
    Sheet sheet = workbook.getSheetAt(selectedSheet.intValue());
    // get the column indexes for project and issueType
    Integer projectColumn = fieldIDToColumnIndexMap.get(SystemFields.INTEGER_PROJECT);
    Integer issueTypeColumn = fieldIDToColumnIndexMap.get(SystemFields.INTEGER_ISSUETYPE);
    // Maps to spare additional database accesses for default values
    Map<Integer, String> defaultShowValuesMap = new HashMap<Integer, String>();
    Map<Integer, String> defaultLocalizedFieldLabels = new HashMap<Integer, String>();
    Integer originalProject = null;
    Integer originalIssueType = null;
    Set<Integer> mandatoryIdentifierFields = ExcelFieldMatchBL.getMandatoryIdentifierFields();
    Map<Integer, Map<String, ILabelBean>> systemLookups = loadBaseLookups(personID, locale);
    Map<String, ILabelBean> projectLookups = systemLookups.get(SystemFields.INTEGER_PROJECT);
    Map<Integer, Map<Integer, Map<String, ILabelBean>>> projectSpecificLookups = null;
    if (projectLookups != null) {
        projectSpecificLookups = loadProjectLookups(GeneralUtils
                .createIntegerListFromBeanList(GeneralUtils.createListFromCollection(projectLookups.values())));
    }
    boolean projectSpecificIDsActive = ApplicationBean.getInstance().getSiteBean().getProjectSpecificIDsOn();
    Map<Integer, TProjectBean> projectBeansMap = new HashMap<Integer, TProjectBean>();
    if (projectSpecificIDsActive) {
        List<TProjectBean> projectBeans = ProjectBL.loadUsedProjectsFlat(personID);
        if (projectBeans != null) {
            for (TProjectBean projectBean : projectBeans) {
                Integer projectID = projectBean.getObjectID();
                projectBeansMap.put(projectID, projectBean);
                String label = projectBean.getLabel();
                String projectPrefix = projectBean.getPrefix();
                if (projectPrefix == null || "".equals(projectPrefix)) {
                    LOGGER.info("The project " + label + " with ID " + projectID
                            + " has no prefix, consquently project specific item numbers might not be recognized");
                }
            }
        }
    }
    /**
     * Process the rows only to gather the projects to issueTypes to get the
     * roles and restrictions once for all issues
     */
    Map<Integer, Set<Integer>> projectToIssueTypesMap = new HashMap<Integer, Set<Integer>>();
    for (Row row : sheet) {
        int rowNum = row.getRowNum();
        if (rowNum == 0) {
            // only the data rows are processed (the header row is not
            // important now)
            continue;
        }
        SerializableBeanAllowedContext serializableBeanAllowedContext = new SerializableBeanAllowedContext();
        serializableBeanAllowedContext.setPersonID(personID);
        serializableBeanAllowedContext.setNew(true);
        // get the project and issueType first because the other fields
        // could depend on these issueTypes
        // process the project column
        Integer projectID = null;
        if (projectColumn != null) {
            try {
                projectID = (Integer) getAttributeValue(row.getCell(projectColumn),
                        SystemFields.INTEGER_PROJECT, null, serializableBeanAllowedContext, locale,
                        invalidValueHandlingMap, systemLookups, projectSpecificLookups);
            } catch (Exception e) {
            }
        }
        if (projectID == null) {
            // no project column exists on the sheet: take the default value
            // which is
            // surely specified, otherwise it would fail at
            // validateRequiredColumns()
            projectID = defaultValuesMap.get(SystemFields.INTEGER_PROJECT);
        }
        if (projectID != null) {
            serializableBeanAllowedContext.setProjectID(projectID);
        }
        // process the issueType column
        Integer issueTypeID = null;
        if (issueTypeColumn != null) {
            try {
                issueTypeID = (Integer) getAttributeValue(row.getCell(issueTypeColumn),
                        SystemFields.INTEGER_ISSUETYPE, null, serializableBeanAllowedContext, locale,
                        invalidValueHandlingMap, systemLookups, projectSpecificLookups);
            } catch (Exception e) {
            }
        }
        if (issueTypeID == null) {
            // no issue type column exists on the sheet: take the default
            // value which is
            // surely specified, otherwise it would fail at
            // validateRequiredColumns()
            issueTypeID = defaultValuesMap.get(SystemFields.INTEGER_ISSUETYPE);
        }
        if (projectID != null) {
            Set<Integer> issueTypes = projectToIssueTypesMap.get(projectID);
            if (issueTypes == null) {
                issueTypes = new HashSet<Integer>();
                projectToIssueTypesMap.put(projectID, issueTypes);
            }
            if (issueTypeID != null) {
                issueTypes.add(issueTypeID);
            }
        }
    }
    Map<Integer, Map<Integer, Map<Integer, TFieldConfigBean>>> projectsToIssueTypesToFieldConfigsMapForBottomUpFields = null;
    Map<Integer, Map<Integer, Map<String, Object>>> projectsIssueTypesFieldSettingsMapForBottomUpFields = null;
    Map<Integer, Map<Integer, Map<Integer, Integer>>> fieldRestrictions = AccessBeans
            .getFieldRestrictions(personID, projectToIssueTypesMap, null, true);
    Set<Integer> possibleBottomUpFields = FieldRuntimeBL.getPossibleBottomUpFields();
    for (Iterator<Integer> iterator = possibleBottomUpFields.iterator(); iterator.hasNext();) {
        if (!fieldIDToColumnIndexMap.containsKey(iterator.next())) {
            // remove possible bottom up field if not mapped
            iterator.remove();
        }
        if (!possibleBottomUpFields.isEmpty()) {
            // at least one bottom up date was mapped
            projectsToIssueTypesToFieldConfigsMapForBottomUpFields = FieldRuntimeBL
                    .loadFieldConfigsInContextsAndTargetProjectAndIssueType(projectToIssueTypesMap,
                            possibleBottomUpFields, locale, null, null);
            projectsIssueTypesFieldSettingsMapForBottomUpFields = FieldRuntimeBL
                    .getFieldSettingsForFieldConfigs(projectsToIssueTypesToFieldConfigsMapForBottomUpFields);
        }
    }

    /**
     * now process the rows in detail one by one
     */
    Stack<Integer> parentStack = new Stack<Integer>();
    Map<Integer, Integer> rowToIndent = new HashMap<Integer, Integer>();
    for (Row row : sheet) {
        int rowNum = row.getRowNum();
        if (rowNum == 0) {
            // only the data rows are processed (the header row is not
            // important now)
            continue;
        }
        boolean excelValueFound = false;
        // whether the project column is mapped and excel value if found for
        // project
        boolean mappedProject = false;
        SerializableBeanAllowedContext serializableBeanAllowedContext = new SerializableBeanAllowedContext();
        serializableBeanAllowedContext.setPersonID(personID);
        serializableBeanAllowedContext.setNew(true);
        // get the project and issueType first because the other fields
        // could depend on these issueTypes
        // process the project column
        Integer projectID = null;
        if (projectColumn != null) {
            try {
                projectID = (Integer) getAttributeValue(row.getCell(projectColumn),
                        SystemFields.INTEGER_PROJECT, null, serializableBeanAllowedContext, locale,
                        invalidValueHandlingMap, systemLookups, projectSpecificLookups);
                if (projectID != null) {
                    mappedProject = true;
                    excelValueFound = true;
                }
            } catch (ExcelImportNotExistingCellValueException e) {
                addGridError(gridErrorsMap, NOT_EXISTING_ERRORS, rowNum,
                        ExcelFieldMatchBL.colNumericToLetter(projectColumn), SystemFields.INTEGER_PROJECT,
                        e.getMessage());
            } catch (ExcelImportNotAllowedCellValueException e) {
                addGridError(gridErrorsMap, NOT_ALLOWED_ERRORS, rowNum,
                        ExcelFieldMatchBL.colNumericToLetter(projectColumn), SystemFields.INTEGER_PROJECT,
                        e.getMessage());
            } catch (ExcelImportInvalidCellValueException e) {
                addGridError(gridErrorsMap, INVALID_ERRORS, rowNum,
                        ExcelFieldMatchBL.colNumericToLetter(projectColumn), SystemFields.INTEGER_PROJECT,
                        e.getMessage());
            }
        }
        if (projectID == null) {
            // no project column exists on the sheet: take the default value
            // which is
            // surely specified, otherwise it would fail at
            // validateRequiredColumns()
            projectID = defaultValuesMap.get(SystemFields.INTEGER_PROJECT);
        }
        if (projectID != null) {
            serializableBeanAllowedContext.setProjectID(projectID);
        }
        // process the issueType column
        Integer issueTypeID = null;
        if (issueTypeColumn != null) {
            try {
                issueTypeID = (Integer) getAttributeValue(row.getCell(issueTypeColumn),
                        SystemFields.INTEGER_ISSUETYPE, null, serializableBeanAllowedContext, locale,
                        invalidValueHandlingMap, systemLookups, projectSpecificLookups);
                if (issueTypeID != null) {
                    excelValueFound = true;
                }
            } catch (ExcelImportNotExistingCellValueException e) {
                addGridError(gridErrorsMap, NOT_EXISTING_ERRORS, rowNum,
                        ExcelFieldMatchBL.colNumericToLetter(issueTypeColumn), SystemFields.INTEGER_ISSUETYPE,
                        e.getMessage());
            } catch (ExcelImportNotAllowedCellValueException e) {
                addGridError(gridErrorsMap, NOT_ALLOWED_ERRORS, rowNum,
                        ExcelFieldMatchBL.colNumericToLetter(issueTypeColumn), SystemFields.INTEGER_ISSUETYPE,
                        e.getMessage());
            } catch (ExcelImportInvalidCellValueException e) {
                addGridError(gridErrorsMap, INVALID_ERRORS, rowNum,
                        ExcelFieldMatchBL.colNumericToLetter(issueTypeColumn), SystemFields.INTEGER_ISSUETYPE,
                        e.getMessage());
            }
        }
        if (issueTypeID == null) {
            // no issue type column exists on the sheet: take the default
            // value which is
            // surely specified, otherwise it would fail at
            // validateRequiredColumns()
            issueTypeID = defaultValuesMap.get(SystemFields.INTEGER_ISSUETYPE);
        }
        if (issueTypeID != null) {
            serializableBeanAllowedContext.setIssueTypeID(issueTypeID);
        }
        /*
         * gather the values for the identifier fields and try to get an
         * existing workItem by these fields
         */
        Map<Integer, Object> identifierFieldValues = new HashMap<Integer, Object>();
        if (lastSavedIdentifierFieldIDIsSet != null && !lastSavedIdentifierFieldIDIsSet.isEmpty()) {
            for (Integer fieldID : lastSavedIdentifierFieldIDIsSet) {
                Integer attributeFieldID = fieldID;
                if (SystemFields.INTEGER_ISSUENO.equals(fieldID) && projectSpecificIDsActive) {
                    attributeFieldID = SystemFields.INTEGER_PROJECT_SPECIFIC_ISSUENO;
                }
                Object attributeValue = null;
                Integer columnIndex = null;
                try {
                    columnIndex = fieldIDToColumnIndexMap.get(fieldID);
                    attributeValue = getAttributeValue(row.getCell(columnIndex), attributeFieldID, null,
                            serializableBeanAllowedContext, locale, invalidValueHandlingMap, systemLookups,
                            projectSpecificLookups);
                    if (attributeValue != null) {
                        identifierFieldValues.put(fieldID, attributeValue);
                        excelValueFound = true;
                    }
                } catch (ExcelImportNotExistingCellValueException e) {
                    if (!SystemFields.INTEGER_PROJECT.equals(fieldID)
                            && !SystemFields.INTEGER_ISSUETYPE.equals(fieldID)) {
                        // if project or issueType are set as identifier
                        // fields and
                        // have grid error they should be already collected
                        // in gridErrorsMap
                        addGridError(gridErrorsMap, NOT_EXISTING_ERRORS, rowNum,
                                ExcelFieldMatchBL.colNumericToLetter(columnIndex), fieldID, e.getMessage());
                    }
                } catch (ExcelImportNotAllowedCellValueException e) {
                    if (!SystemFields.INTEGER_PROJECT.equals(fieldID)
                            && !SystemFields.INTEGER_ISSUETYPE.equals(fieldID)) {
                        // if project or issueType are set as identifier
                        // fields and
                        // have grid error they should be already collected
                        // in gridErrorsMap
                        addGridError(gridErrorsMap, NOT_ALLOWED_ERRORS, rowNum,
                                ExcelFieldMatchBL.colNumericToLetter(columnIndex), fieldID, e.getMessage());
                    }
                } catch (ExcelImportInvalidCellValueException e) {
                    if (!SystemFields.INTEGER_PROJECT.equals(fieldID)
                            && !SystemFields.INTEGER_ISSUETYPE.equals(fieldID)) {
                        // if project or issueType are set as identifier
                        // fields and
                        // have grid error they should be already collected
                        // in gridErrorsMap
                        addGridError(gridErrorsMap, INVALID_ERRORS, rowNum,
                                ExcelFieldMatchBL.colNumericToLetter(columnIndex), fieldID, e.getMessage());
                    }
                }
            }
        }
        // always initialize the next workItem to null
        TWorkItemBean workItemBean = null;
        boolean itemIsNew = false;
        if (!identifierFieldValues.isEmpty()) {
            if (identifierFieldValues.get(SystemFields.INTEGER_ISSUENO) != null) {
                // is issueNo field mapped?
                if (projectSpecificIDsActive) {
                    // get by project specific itemID
                    String projectSpecificID = null;
                    try {
                        projectSpecificID = (String) identifierFieldValues.get(SystemFields.INTEGER_ISSUENO);
                    } catch (Exception e) {
                    }
                    if (projectSpecificID != null) {
                        // it should be trimmed because in excel the child
                        // issues are indented
                        workItemBean = ItemBL.loadWorkItemByProjectSpecificID(projectID, mappedProject,
                                projectBeansMap, projectSpecificID.trim());
                        if (workItemBean != null && LOGGER.isDebugEnabled()) {
                            LOGGER.debug("WorkItem " + projectSpecificID + " from row " + rowNum
                                    + " found by projectSpecificID");
                        }
                    }
                } else {
                    // get by "global" workItemID
                    Integer workItemID = null;
                    try {
                        workItemID = (Integer) identifierFieldValues.get(SystemFields.INTEGER_ISSUENO);
                    } catch (Exception e) {
                    }
                    if (workItemID != null) {
                        workItemBean = ItemBL.loadWorkItemSystemAttributes(workItemID);
                    }
                    if (workItemBean != null && LOGGER.isDebugEnabled()) {
                        LOGGER.debug("WorkItem " + workItemID + " from row " + rowNum + " found by workItemID");
                    }
                }
                if (workItemBean == null) {
                    // the issueNo field is set as identifier and the
                    // corresponding issue does't exist, report as error
                    addGridError(gridErrorsMap, WORKITEM_NOTEXIST_ERRORS, rowNum,
                            ExcelFieldMatchBL.colNumericToLetter(
                                    fieldIDToColumnIndexMap.get(SystemFields.INTEGER_ISSUENO)),
                            SystemFields.INTEGER_ISSUENO,
                            identifierFieldValues.get(SystemFields.INTEGER_ISSUENO).toString());
                    continue;
                }
            }
            if (workItemBean == null) {
                // workItem was not found by issueNo
                // (issueNo field was not mapped or issueNo value is missing
                // from excel or
                // the issue's project is not accessible if
                // projectSpecificIDsActive)
                // try with user defined identifier fields
                try {
                    workItemBean = ItemBL.loadWorkItemSystemAttributes(identifierFieldValues);
                } catch (ExcelImportNotUniqueIdentifiersException e) {
                    addRowError(rowErrorsMap, WORKITEM_MORE_THAN_ONE_EXIST, rowNum);
                    continue;
                }
                if (workItemBean != null && LOGGER.isDebugEnabled()) {
                    LOGGER.debug("WorkItem from row " + rowNum + " found by user defined identifier fields");
                }
            }
            if (workItemBean != null) {
                // existing workItem
                originalProject = workItemBean.getProjectID();
                originalIssueType = workItemBean.getListTypeID();
                // is it editable by the current person?
                if (!AccessBeans.isAllowedToChange(workItemBean, personID)) {
                    addRowError(rowErrorsMap, WORKITEM_NO_EDIT_RIGHT, rowNum);
                    continue;
                }
                // load also the custom attributes because when the workItem
                // will be updated
                // the custom attributes will also be compared to the
                // original value
                ItemBL.loadWorkItemCustomAttributes(workItemBean);
                serializableBeanAllowedContext.setWorkItemBeanOriginal(workItemBean);
                serializableBeanAllowedContext.setNew(false);
                // LOGGER.debug("WorkItem " + workItemBean.getObjectID() +
                // " from row " + rowNum + " found");
            }
        }
        boolean missingRequiredFound = false;
        if (workItemBean == null) {
            // not existing found by identifier fields, create a new one
            workItemBean = new TWorkItemBean();
            if (identifierFieldValues != null) {
                // preset the new workItem with the processed identifier
                // values
                for (Map.Entry<Integer, Object> identifierEntry : identifierFieldValues.entrySet()) {
                    workItemBean.setAttribute(identifierEntry.getKey(), identifierEntry.getValue());
                }
            }
            itemIsNew = true;
            LOGGER.debug("WorkItem from row " + rowNum + " not found. A new one will be created.");
        }
        if (projectID != null) {
            workItemBean.setAttribute(SystemFields.INTEGER_PROJECT, null, projectID);
        } else {
            if (itemIsNew) {
                // project column not mapped
                addRowError(requiredFieldErrorsMap, SystemFields.INTEGER_PROJECT, rowNum);
                missingRequiredFound = true;
            }
        }
        if (issueTypeID != null) {
            workItemBean.setAttribute(SystemFields.INTEGER_ISSUETYPE, null, issueTypeID);
        } else {
            if (itemIsNew) {
                // project column not mapped
                addRowError(requiredFieldErrorsMap, SystemFields.INTEGER_ISSUETYPE, rowNum);
                missingRequiredFound = true;
            }
        }
        if (missingRequiredFound) {
            continue;
        }
        Map<Integer, Integer> restrictedFields = null;
        projectID = workItemBean.getProjectID();
        issueTypeID = workItemBean.getListTypeID();
        if (projectID != null && issueTypeID != null) {
            Map<Integer, Map<Integer, Integer>> issueTypeRestrictions = fieldRestrictions.get(projectID);
            if (issueTypeRestrictions != null) {
                restrictedFields = issueTypeRestrictions.get(issueTypeID);
            }
            if (restrictedFields == null) {
                // no project or issue type mapped get the restriction now
                restrictedFields = AccessBeans.getFieldRestrictions(personID, projectID, issueTypeID, true);
                issueTypeRestrictions = new HashMap<Integer, Map<Integer, Integer>>();
                issueTypeRestrictions.put(issueTypeID, restrictedFields);
                fieldRestrictions.put(projectID, issueTypeRestrictions);
            }
            // new values exist
            if (originalProject != null && originalIssueType != null) {
                // workItem existed
                if (!projectID.equals(originalProject) || !issueTypeID.equals(originalIssueType)) {
                    if (!AccessBeans.isAllowedToChange(workItemBean, personID)) {
                        // move not allowed
                        addRowError(rowErrorsMap, WORKITEM_NO_EDIT_RIGHT, rowNum);
                        continue;
                    }
                }
            } else {
                // new workItem
                if (!AccessBeans.isAllowedToCreate(personID, projectID, issueTypeID)) {
                    // create not allowed
                    addRowError(rowErrorsMap, WORKITEM_NO_CREATE_RIGHT, rowNum);
                    continue;
                }
            }
        }
        // process the remaining cells
        Map<Integer, Integer> rowNoToIndentLevel = new HashMap<Integer, Integer>();
        for (Cell cell : row) {
            boolean attributeChanged = false;
            int columnIndex = cell.getColumnIndex();
            Integer fieldID = columnIndexToFieldIDMap.get(columnIndex);
            Integer fieldForRestriction = fieldID;
            if (fieldID == null) {
                // LOGGER.debug("No mapping found for column " +
                // columnIndex);
                continue;
            }
            if (fieldID.equals(SystemFields.INTEGER_PROJECT) || fieldID.equals(SystemFields.INTEGER_ISSUETYPE)
                    || identifierFieldValues.containsKey(fieldID)
                    || mandatoryIdentifierFields.contains(fieldID)) {
                // these values are already read
                continue;
            }
            if (fieldID.intValue() < 0) {
                // pseudo field: now only watchers
                if (fieldID.intValue() == TReportLayoutBean.PSEUDO_COLUMNS.INFORMANT_LIST
                        || fieldID.intValue() == TReportLayoutBean.PSEUDO_COLUMNS.CONSULTANT_LIST) {
                    fieldForRestriction = FieldsRestrictionsToRoleBL.PSEUDO_COLUMNS.WATCHERS;
                    String watcherValue = getStringCellValue(cell);
                    if (watcherValue == null || "".equals(watcherValue.trim())) {
                        continue;
                    }
                    Map<Integer, List<Integer>> watcherMapOriginal = rowNoToPseudoFieldsOriginal.get(rowNum);
                    if (watcherMapOriginal == null) {
                        watcherMapOriginal = new HashMap<Integer, List<Integer>>();
                        rowNoToPseudoFieldsOriginal.put(rowNum, watcherMapOriginal);
                    }
                    List<Integer> watcherListOriginal = null;
                    TWorkItemBean workItemBeanOriginal = serializableBeanAllowedContext
                            .getWorkItemBeanOriginal();
                    if (workItemBeanOriginal != null) {
                        if (fieldID.intValue() == TReportLayoutBean.PSEUDO_COLUMNS.INFORMANT_LIST) {
                            watcherListOriginal = GeneralUtils.createIntegerListFromBeanList(
                                    PersonBL.getDirectInformants(workItemBeanOriginal.getObjectID()));
                        } else {
                            watcherListOriginal = GeneralUtils.createIntegerListFromBeanList(
                                    PersonBL.getDirectConsultants(workItemBeanOriginal.getObjectID()));
                        }
                        watcherMapOriginal.put(fieldID, watcherListOriginal);
                    }
                    List<Integer> watcherListExcel = new LinkedList<Integer>();
                    String[] watcherNames = watcherValue
                            .split("\\" + ConsultedInformedLoaderBL.WATCHER_SPLITTER_VALUES_STRING);
                    if (watcherNames != null) {
                        Map<Integer, List<Integer>> watcherMapExcel = rowNoToPseudoFieldsExcel.get(rowNum);
                        if (watcherMapExcel == null) {
                            watcherMapExcel = new HashMap<Integer, List<Integer>>();
                            rowNoToPseudoFieldsExcel.put(rowNum, watcherMapExcel);
                        }
                        watcherMapExcel.put(fieldID, watcherListExcel);
                        for (int i = 0; i < watcherNames.length; i++) {
                            String watcherName = watcherNames[i];
                            Integer objectID = null;
                            try {
                                objectID = getWatcherValue(watcherName, fieldID, systemLookups,
                                        watcherListOriginal, serializableBeanAllowedContext, locale);
                            } catch (ExcelImportNotExistingCellValueException e) {
                                addGridError(gridErrorsMap, NOT_EXISTING_ERRORS, rowNum,
                                        ExcelFieldMatchBL.colNumericToLetter(columnIndex), fieldID,
                                        e.getMessage());
                            } catch (ExcelImportNotAllowedCellValueException e) {
                                addGridError(gridErrorsMap, NOT_ALLOWED_ERRORS, rowNum,
                                        ExcelFieldMatchBL.colNumericToLetter(columnIndex), fieldID,
                                        e.getMessage());
                            }
                            if (objectID != null) {
                                watcherListExcel.add(objectID);
                                excelValueFound = true;
                            }
                        }
                    }
                    attributeChanged = ConsInfBL.watcherChanged(watcherListOriginal, watcherListExcel);
                } else {
                    if (fieldID.intValue() == ExcelFieldMatchBL.LOCAL_PARENT_PSEUDO_COLUMN) {
                        // local parent - child hierarchy (for new items)
                        Integer pseudoHierarchyColumn = fieldIDToColumnIndexMap
                                .get(ExcelFieldMatchBL.LOCAL_PARENT_PSEUDO_COLUMN);
                        if (pseudoHierarchyColumn != null) {
                            String hierarchyColumn = getStringCellValue(row.getCell(pseudoHierarchyColumn));
                            if (hierarchyColumn != null && hierarchyColumn.length() > 0) {
                                int previousIndent = 0;
                                if (!parentStack.isEmpty()) {
                                    Integer previousRow = parentStack.peek();
                                    if (rowToIndent.get(previousRow) != null) {
                                        previousIndent = rowToIndent.get(previousRow).intValue();
                                    }
                                }
                                int actualIndent = hierarchyColumn.length();
                                rowToIndent.put(rowNum, actualIndent);
                                rowNoToIndentLevel.put(rowNum, actualIndent);
                                if (previousIndent == actualIndent) {
                                    // sibling: same parent as the sibling's
                                    // parent
                                    if (!parentStack.isEmpty()) {
                                        // remove the sibling from stack
                                        parentStack.pop();
                                        if (!parentStack.isEmpty()) {
                                            // if the stack is still not
                                            // empty then the peek is teh
                                            // parent
                                            Integer parentRow = parentStack.peek();
                                            rowToParentRow.put(rowNum, parentRow);
                                        }
                                    }
                                } else {
                                    if (actualIndent > previousIndent) {
                                        // child of the previous row
                                        if (actualIndent - previousIndent > 1) {
                                            // jump more than one in deep is
                                            // error
                                            addGridError(gridErrorsMap, INCONSISTENT_HIERARCHY_ERRORS, rowNum,
                                                    ExcelFieldMatchBL.colNumericToLetter(columnIndex), fieldID,
                                                    hierarchyColumn);
                                        }
                                        if (!parentStack.isEmpty()) {
                                            // add previous row as parent
                                            Integer parentRow = parentStack.peek();
                                            rowToParentRow.put(rowNum, parentRow);
                                        }
                                    } else {
                                        // new hierarchy: nothing to do with
                                        // the previous row
                                        int difference = previousIndent - actualIndent;
                                        for (int i = 0; i <= difference; i++) {
                                            // pop to find the parent
                                            if (!parentStack.isEmpty()) {
                                                parentStack.pop();
                                            }
                                        }
                                        if (!parentStack.isEmpty()) {
                                            Integer parentRow = parentStack.peek();
                                            rowToParentRow.put(rowNum, parentRow);
                                        }
                                    }
                                }
                            } else {
                                // no hierarchy string: top level item
                                while (!parentStack.isEmpty()) {
                                    // empty the stack
                                    parentStack.pop();
                                }
                            }
                            // add row to stack for possible children
                            parentStack.push(rowNum);
                        }
                    }
                }
            } else {
                IFieldTypeRT fieldTypeRT = FieldTypeManager.getFieldTypeRT(fieldID);
                Object attributeValue = null;
                if (fieldTypeRT.isComposite() || fieldTypeRT.isMultipleValues()) {
                    String compositeOrMultipleValue = getStringCellValue(cell);
                    if (compositeOrMultipleValue == null || "".equals(compositeOrMultipleValue.trim())) {
                        workItemBean.setAttribute(fieldID, null, null);
                        continue;
                    }
                    // we suppose that all composite and multiple values are
                    // lookup values
                    // TODO refactor if that is not true
                    String[] parts;
                    if (fieldTypeRT.isMultipleValues()) {
                        parts = compositeOrMultipleValue
                                .split(CustomSelectBaseRT.OPTION_SPLITTER_VALUES_STRING);
                        List<Integer> multipleValues = new ArrayList<Integer>();
                        for (int i = 0; i < parts.length; i++) {
                            String part = parts[i];
                            Integer objectID = null;
                            try {
                                objectID = getLookupValue(part, fieldTypeRT, fieldID, systemLookups,
                                        projectSpecificLookups, serializableBeanAllowedContext, null,
                                        invalidValueHandlingMap, locale);
                            } catch (ExcelImportNotExistingCellValueException e) {
                                addGridError(gridErrorsMap, NOT_EXISTING_ERRORS, rowNum,
                                        ExcelFieldMatchBL.colNumericToLetter(columnIndex), fieldID,
                                        e.getMessage());
                            } catch (ExcelImportNotAllowedCellValueException e) {
                                addGridError(gridErrorsMap, NOT_ALLOWED_ERRORS, rowNum,
                                        ExcelFieldMatchBL.colNumericToLetter(columnIndex), fieldID,
                                        e.getMessage());
                            }
                            if (objectID != null) {
                                multipleValues.add(objectID);
                            }
                        }
                        if (!multipleValues.isEmpty()) {
                            attributeValue = multipleValues.toArray();
                            excelValueFound = true;
                        }
                    } else {
                        int numberOfParts = ((CustomCompositeBaseRT) fieldTypeRT).getNumberOfParts();
                        parts = compositeOrMultipleValue
                                .split("\\" + CustomCompositeBaseRT.PART_SPLITTER_VALUES_STRING);
                        if (parts != null && parts.length > numberOfParts) {
                            addGridError(gridErrorsMap, WRONG_COMPOSITE_SIZE, rowNum,
                                    ExcelFieldMatchBL.colNumericToLetter(columnIndex), fieldID,
                                    compositeOrMultipleValue);
                        }
                        Map<Integer, Integer> componentPartsMap = new HashMap<Integer, Integer>();
                        attributeValue = new HashMap<Integer, Object>();
                        if (parts != null) {
                            for (int i = 0; i < parts.length; i++) {
                                String part = parts[i];
                                Integer objectID = null;
                                IFieldTypeRT componentFieldType = ((CustomCompositeBaseRT) fieldTypeRT)
                                        .getCustomFieldType(i + 1);
                                if (componentFieldType != null) {
                                    try {
                                        objectID = getLookupValue(part, componentFieldType, fieldID,
                                                systemLookups, projectSpecificLookups,
                                                serializableBeanAllowedContext, componentPartsMap,
                                                invalidValueHandlingMap, locale);
                                    } catch (ExcelImportNotExistingCellValueException e) {
                                        addGridError(gridErrorsMap, NOT_EXISTING_ERRORS, rowNum,
                                                ExcelFieldMatchBL.colNumericToLetter(columnIndex), fieldID,
                                                e.getMessage());
                                    } catch (ExcelImportNotAllowedCellValueException e) {
                                        addGridError(gridErrorsMap, NOT_ALLOWED_ERRORS, rowNum,
                                                ExcelFieldMatchBL.colNumericToLetter(columnIndex), fieldID,
                                                e.getMessage());
                                    }
                                    if (objectID == null) {
                                        // workItemBean.setAttribute(fieldID,
                                        // Integer.valueOf(i+1), null);
                                        ((Map<Integer, Object>) attributeValue).put(Integer.valueOf(i + 1),
                                                null);
                                    } else {
                                        componentPartsMap.put(Integer.valueOf(i + 1), objectID);
                                        // workItemBean.setAttribute(fieldID,
                                        // Integer.valueOf(i+1), new
                                        // Object[] {objectID});
                                        ((Map<Integer, Object>) attributeValue).put(Integer.valueOf(i + 1),
                                                new Object[] { objectID });
                                        excelValueFound = true;
                                    }
                                }
                            }
                        }
                    }
                } else {
                    // simple field
                    // Object attributeValue = null;
                    try {
                        attributeValue = getAttributeValue(cell, fieldID, null, serializableBeanAllowedContext,
                                locale, invalidValueHandlingMap, systemLookups, projectSpecificLookups);
                    } catch (ExcelImportNotExistingCellValueException e) {
                        addGridError(gridErrorsMap, NOT_EXISTING_ERRORS, rowNum,
                                ExcelFieldMatchBL.colNumericToLetter(columnIndex), fieldID, e.getMessage());
                    } catch (ExcelImportNotAllowedCellValueException e) {
                        addGridError(gridErrorsMap, NOT_ALLOWED_ERRORS, rowNum,
                                ExcelFieldMatchBL.colNumericToLetter(columnIndex), fieldID, e.getMessage());
                    } catch (ExcelImportInvalidCellValueException e) {
                        addGridError(gridErrorsMap, INVALID_ERRORS, rowNum,
                                ExcelFieldMatchBL.colNumericToLetter(columnIndex), fieldID, e.getMessage());
                    }
                    if (attributeValue != null) {
                        excelValueFound = true;
                        if (possibleBottomUpFields.contains(fieldID)) {
                            TFieldConfigBean fieldConfigBean = FieldRuntimeBL
                                    .getFieldConfigForProjectIssueTypeField(
                                            projectsToIssueTypesToFieldConfigsMapForBottomUpFields, projectID,
                                            issueTypeID, fieldID);
                            Object fieldSettings = FieldRuntimeBL.getFieldSettingsForProjectIssueTypeField(
                                    projectsIssueTypesFieldSettingsMapForBottomUpFields, projectID, issueTypeID,
                                    fieldID);
                            if (fieldTypeRT.getHierarchicalBehavior(fieldID, fieldConfigBean,
                                    fieldSettings) == HIERARCHICAL_BEHAVIOR_OPTIONS.COMPUTE_BOTTOM_UP
                                    && ItemBL.hasChildren(workItemBean.getObjectID())) {
                                Date trackPlusAttributeValue = (Date) workItemBean.getAttribute(fieldID);
                                if (EqualUtils.notEqual(trackPlusAttributeValue, (Date) attributeValue)) {
                                    // add read only restrictions for start
                                    // and end date for non leaf workItems
                                    LOGGER.debug("Parent change restriction for bottom up date " + fieldID);
                                    addGridError(gridErrorsMap, NOT_EDITABLE_ERRORS, rowNum,
                                            ExcelFieldMatchBL.colNumericToLetter(columnIndex), fieldID,
                                            getStringCellValue(cell));
                                }
                            }
                            /*
                             * if
                             * (ApplicationBean.getInstance().getSiteBean
                             * ().getSummaryItemsBehavior() &&
                             * ItemBL2.hasChildren
                             * (workItemBean.getObjectID())) { Date
                             * trackPlusAttributeValue =
                             * (Date)workItemBean.getAttribute(fieldID); if
                             * (EqualUtils.notEqual(trackPlusAttributeValue,
                             * (Date)attributeValue)) { //add read only
                             * restrictions for start and end date for non
                             * leaf workItems LOGGER.debug(
                             * "Summary parent change restriction for date "
                             * + fieldID); addGridError(gridErrorsMap,
                             * NOT_EDITABLE_ERRORS, rowNum,
                             * ExcelFieldMatchBL
                             * .colNumericToLetter(columnIndex), fieldID,
                             * getStringCellValue(cell)); } }
                             */
                        }
                    }
                }
                attributeChanged = fieldTypeRT.valueModified(attributeValue,
                        workItemBean.getAttribute(fieldID));
                workItemBean.setAttribute(fieldID, null, attributeValue);
            }
            if (attributeChanged) {
                try {
                    verifyFieldRestrictions(fieldForRestriction, restrictedFields, cell, locale);
                } catch (ExcelImportNotModifiableCellValueException e) {
                    addGridError(gridErrorsMap, NOT_EDITABLE_ERRORS, rowNum,
                            ExcelFieldMatchBL.colNumericToLetter(columnIndex), fieldID, e.getMessage());
                }
            }
        }
        if (!excelValueFound) {
            // not a single excel value found in any cell from the row
            // simply neglect this row.
            // expanded row count can be greater than the number of real
            // workItem rows
            // for example when the content of some rows is deleted but the
            // rows are not deleted
            // and empty rows may remain in the excel
            LOGGER.info("The row number " + (rowNum + 1) + " contains only empty cells and will be neglected");
            continue;
        }
        // add the default values for those fields which didn't have column
        // in
        // excel sheet or have column but the value is empty or not valid
        Iterator<Integer> itrDefaultValueFields = defaultValuesMap.keySet().iterator();
        while (itrDefaultValueFields.hasNext()) {
            Integer fieldID = itrDefaultValueFields.next();
            if (/*!fieldIDToColumnIndexMap.containsKey(fieldID) ||*/workItemBean.getAttribute(fieldID,
                    null) == null) {
                if (invalidValueHandlingMap.containsKey(fieldID)) {
                    if (DEFAULT_IF_NOT_EXIST_OR_EMPTY.equals(invalidValueHandlingMap.get(fieldID))) {
                        IFieldTypeRT fieldTypeRT = FieldTypeManager.getFieldTypeRT(fieldID, null);
                        ILookup lookup = (ILookup) fieldTypeRT;
                        Integer defaultObjectID = defaultValuesMap.get(fieldID);
                        if (defaultObjectID != null) {
                            boolean allowed = lookup.lookupBeanAllowed(defaultObjectID,
                                    serializableBeanAllowedContext);
                            if (allowed) {
                                workItemBean.setAttribute(fieldID, null, defaultObjectID);
                            } else {
                                // for example when no default project
                                // and/or issue type is specified the
                                // default manager and responsible
                                // lists contain the users which are manager
                                // or responsible in any of the projects
                                // (but maybe not in all)
                                LOGGER.debug("The default value is not allowed for field " + fieldID
                                        + " on row " + rowNum);
                                // cache the show values and localized
                                // labels to spare additional database
                                // accesses
                                String showValue;
                                if (defaultShowValuesMap.containsKey(fieldID)) {
                                    showValue = defaultShowValuesMap.get(fieldID);
                                } else {
                                    showValue = fieldTypeRT.getShowValue(defaultObjectID, locale);
                                    defaultShowValuesMap.put(fieldID, showValue);
                                }
                                String localizedLabel;
                                if (defaultLocalizedFieldLabels.containsKey(fieldID)) {
                                    localizedLabel = defaultLocalizedFieldLabels.get(fieldID);
                                } else {
                                    localizedLabel = FieldRuntimeBL.getLocalizedDefaultFieldLabel(fieldID,
                                            locale);
                                    defaultLocalizedFieldLabels.put(fieldID, localizedLabel);
                                }
                                addGridError(gridErrorsMap, NOT_ALLOWED_DEFAULT_VALUES_ERRORS, rowNum,
                                        localizedLabel, fieldID, showValue);
                            }
                        }
                    }
                }
            }
        }
        workItemBeansMap.put(rowNum, workItemBean);
    }
    return workItemBeansMap;
}