Package com.collective2.signalEntry.approval

Source Code of com.collective2.signalEntry.approval.ApprovalRequestableConsole

/**
* This notice shall not be removed.
* See the "LICENSE.txt" file found in the root folder
* for the full license governing this code.
* Nathan Tippy  9/20/12
*/
package com.collective2.signalEntry.approval;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import com.collective2.signalEntry.implementation.Request;

public class ApprovalRequestableConsole implements C2EntryHumanApproval {

    private List<Request> needsApprovalList = new ArrayList<Request>();

    @Override
    public void waitForApproval(Iterator<Request> needsApproval) {
        int size;
        synchronized (needsApprovalList) {
            needsApprovalList.clear();
            while (needsApproval.hasNext()) {
                needsApprovalList.add(needsApproval.next());
            }
            size = needsApprovalList.size();
        }

        int i = 0;
        while (i<size) { //note: by design may grow while its walked
            Request request;
            synchronized (needsApprovalList) {
                request = needsApprovalList.get(i);
            }
            //only ask for approval once even if items are still on the list
            if (!request.isApprovalKnown()) {
                System.out.println("");
                System.out.println(request.buildURL());
                System.out.print("approve (Y/N):");

                char c = System.console().readLine().trim().charAt(0);
                request.setApproved('Y'==c || 'y'==c);
                System.out.println(c);
            }
            i++;
            synchronized (needsApprovalList) {
                size = needsApprovalList.size();
            }

        }
        synchronized(needsApprovalList) {
            needsApprovalList.clear();
        }
    }

    @Override
    public void oneMoreRequest(Request request) {
        synchronized(needsApprovalList) {
            if (!needsApprovalList.isEmpty()) {
                needsApprovalList.add(request);
            }
        }
    }
}
TOP

Related Classes of com.collective2.signalEntry.approval.ApprovalRequestableConsole

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.