Example usage for java.util ArrayList remove

List of usage examples for java.util ArrayList remove

Introduction

In this page you can find the example usage for java.util ArrayList remove.

Prototype

public boolean remove(Object o) 

Source Link

Document

Removes the first occurrence of the specified element from this list, if it is present.

Usage

From source file:ch.epfl.lsir.xin.algorithm.core.MatrixFactorization.java

/**
 * This function learns a matrix factorization model using Stochastic Gradient Descent 
 * *///from  www  .j ava 2s  .c om
public void buildSGD() {
    double preError = Double.MAX_VALUE;
    for (int i = 0; i < this.Iterations; i++) {
        System.out.println("Iteration: " + i);
        ArrayList<MatrixEntry2D> entries = this.ratingMatrix.getValidEntries();
        double error = 0; //overall error of this iteration
        while (entries.size() > 0) {
            //find a random entry
            int r = new Random().nextInt(entries.size());
            MatrixEntry2D entry = entries.get(r);
            double prediction = predict(entry.getRowIndex(), entry.getColumnIndex(), false);
            if (prediction > this.maxRating)
                prediction = this.maxRating;
            if (prediction < this.minRating)
                prediction = this.minRating;
            double difference = entry.getValue() - prediction;
            for (int l = 0; l < this.latentFactors; l++) {
                double tempU = this.userMatrix.get(entry.getRowIndex(), l) + this.learningRate * ( /*2 **/
                difference * this.itemMatrix.get(entry.getColumnIndex(), l)
                        - this.regUser * this.userMatrix.get(entry.getRowIndex(), l));
                double tempI = this.itemMatrix.get(entry.getColumnIndex(), l) + this.learningRate * ( /*2 **/
                difference * this.userMatrix.get(entry.getRowIndex(), l)
                        - this.regItem * this.itemMatrix.get(entry.getColumnIndex(), l));
                this.userMatrix.set(entry.getRowIndex(), l, tempU);
                this.itemMatrix.set(entry.getColumnIndex(), l, tempI);
            }
            //one rating is only processed once in an iteration
            entries.remove(r);
        }
        //error
        entries = this.ratingMatrix.getValidEntries();
        for (int k = 0; k < entries.size(); k++) {
            MatrixEntry2D entry = entries.get(k);
            double prediction = predict(entry.getRowIndex(), entry.getColumnIndex(), false);
            if (prediction > this.maxRating)
                prediction = this.maxRating;
            if (prediction < this.minRating)
                prediction = this.minRating;
            error = error + Math.abs(entry.getValue() - prediction);
            //            for( int j = 0 ; j < this.latentFactors ; j++ )
            //            {
            //               error = error + this.regUser/2 * Math.pow(this.userMatrix.get(entry.getRowIndex(), j), 2) + 
            //                     this.regItem/2 * Math.pow(this.itemMatrix.get(entry.getColumnIndex(), j), 2);
            //            }      
        }

        this.logger.println(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()) + " Iteration " + i
                + " : Error ~ " + error);
        this.logger.flush();
        //check for convergence
        if (Math.abs(error - preError) <= this.convergence && error <= preError) {
            logger.println("The algorithm convergences.");
            this.logger.flush();
            break;
        }

        // learning rate update strategy 
        updateLearningRate(error, preError);

        preError = error;
        logger.flush();
    }
}

From source file:com.zimbra.perf.chart.ChartUtil.java

private List<JFreeChart> createJFReeChart(ChartSettings cs) {

    double minValue = Double.MAX_VALUE;
    double maxValue = Double.MIN_VALUE;
    double d = 0;
    double count = 0;
    double total = 0;
    TimeSeriesCollection data = new TimeSeriesCollection();

    ArrayList<ChartSettings> syntheticSettings = new ArrayList<ChartSettings>();
    for (GroupPlotSettings gps : cs.getGroupPlots()) {
        String groupBy = gps.getGroupBy();
        DataColumn dc = new DataColumn(gps.getInfile(), groupBy);
        StringSeries groupBySeries = mStringSeries.get(dc);
        dc = new DataColumn(gps.getInfile(), gps.getDataColumn());
        DataSeries ds = mDataSeries.get(dc);
        int idx = 0;
        Map<String, List<Integer>> groups = new HashMap<String, List<Integer>>();
        for (StringEntry e : groupBySeries.dataCollection) {
            String g = e.getVal();
            List<Integer> indices = groups.get(g);
            if (indices == null) {
                indices = new ArrayList<Integer>();
                groups.put(g, indices);//from   ww w. ja  v  a  2 s .co  m
            }
            indices.add(idx);
            idx++;
        }
        for (Map.Entry<String, List<Integer>> g : groups.entrySet()) {
            String groupByValue = g.getKey();
            if (gps.getIgnoreSet().contains(groupByValue))
                continue;
            List<Integer> indices = g.getValue();
            DataSeries syntheticDS = new DataSeries();
            DataColumn c = new DataColumn(gps.getInfile(),
                    GROUP_PLOT_SYNTHETIC + groupByValue + ":" + gps.getDataColumn());
            for (int i : indices) {
                Entry e = ds.get(i);
                syntheticDS.AddEntry(e.getTimestamp(), e.getVal());
            }
            mDataSeries.put(c, syntheticDS);
            PlotSettings syntheticPlot = new PlotSettings(groupByValue, c.getInfile(), c.getColumn(),
                    gps.getShowRaw(), gps.getShowMovingAvg(), gps.getMovingAvgPoints(), gps.getMultiplier(),
                    gps.getDivisor(), gps.getNonNegative(), gps.getPercentTime(), gps.getDataFunction(),
                    gps.getAggregateFunction(), gps.getOptional(), null, null);
            cs.addPlot(syntheticPlot);
            if (cs.getOutDocument() != null) {
                ChartSettings s = new ChartSettings(String.format(cs.getTitle(), groupByValue),
                        cs.getCategory(), String.format(cs.getOutfile(), groupByValue), cs.getXAxis(),
                        cs.getYAxis(), cs.getAllowLogScale(), cs.getPlotZero(), cs.getWidth(), cs.getHeight(),
                        null, cs.getTopPlots(), cs.getTopPlotsType());
                s.addPlot(syntheticPlot);
                syntheticSettings.add(s);
            }
        }
    }
    if (cs.getOutDocument() != null && cs.getGroupPlots().size() != 0) {
        ArrayList<JFreeChart> charts = new ArrayList<JFreeChart>();
        for (ChartSettings c : syntheticSettings) {
            charts.addAll(createJFReeChart(c));
            c.setOutDocument(cs.getOutDocument());
        }
        mSyntheticChartSettings.addAll(syntheticSettings);
        return charts;
    }

    List<PlotSettings> plots = cs.getPlots();
    if (cs.getTopPlots() > 0 && plots.size() > cs.getTopPlots()) {
        String aggregateFunction = cs.getTopPlotsType().name().toLowerCase();
        System.out.println(String.format("Reducing %d to %d plots for chart '%s'", plots.size(),
                cs.getTopPlots(), cs.getTitle()));
        ArrayList<PlotAggregatePair> aggregates = new ArrayList<PlotAggregatePair>();
        for (PlotSettings ps : plots) {
            DataColumn dc = new DataColumn(ps.getInfile(), ps.getDataColumn());
            String key = ps.getInfile() + ":" + ps.getDataColumn() + ":" + ps.getAggregateFunction();
            PlotDataIterator pdIter = new PlotDataIterator(ps, mDataSeries.get(dc));
            double aggregate = mAggregator.compute(pdIter, aggregateFunction, mAggregateStartAt,
                    mAggregateEndAt, key);
            aggregates.add(new PlotAggregatePair(ps, aggregate));
        }
        Collections.sort(aggregates);
        while (aggregates.size() > cs.getTopPlots()) {
            PlotAggregatePair pair = aggregates.remove(0);
            plots.remove(pair.ps);
        }
    }
    for (PlotSettings ps : plots) {
        String columnName = ps.getDataColumn();
        if (columnName == null) {
            columnName = RATIO_PLOT_SYNTHETIC + ps.getRatioTop() + "/" + ps.getRatioBottom();
            String infile = ps.getInfile();

            String[] top = ps.getRatioTop().split("\\+");
            String[] bottom = ps.getRatioBottom().split("\\+");

            DataColumn[] ratioTop = new DataColumn[top.length];
            DataColumn[] ratioBottom = new DataColumn[bottom.length];

            for (int i = 0, j = top.length; i < j; i++)
                ratioTop[i] = new DataColumn(infile, top[i]);
            for (int i = 0, j = bottom.length; i < j; i++)
                ratioBottom[i] = new DataColumn(infile, bottom[i]);

            DataSeries[] topData = new DataSeries[ratioTop.length];
            DataSeries[] bottomData = new DataSeries[ratioBottom.length];

            for (int i = 0, j = ratioTop.length; i < j; i++)
                topData[i] = mDataSeries.get(ratioTop[i]);
            for (int i = 0, j = ratioBottom.length; i < j; i++)
                bottomData[i] = mDataSeries.get(ratioBottom[i]);

            DataSeries ds = new DataSeries();
            for (int i = 0, j = topData[0].size(); i < j; i++) {
                double topValue = 0.0;
                double bottomValue = 0.0;
                double ratio = 0.0;
                Entry lastEntry = null;
                for (int m = 0, n = topData.length; m < n; m++) {
                    Entry e = topData[m].get(i);
                    topValue += e.getVal();
                }
                for (int m = 0, n = bottomData.length; m < n; m++) {
                    Entry e = bottomData[m].get(i);
                    bottomValue += e.getVal();
                    lastEntry = e;
                }
                if (bottomValue != 0.0) {
                    ratio = topValue / bottomValue;
                }
                // should never be null
                assert lastEntry != null;
                ds.AddEntry(lastEntry.getTimestamp(), ratio);
            }
            mDataSeries.put(new DataColumn(infile, columnName), ds);
            ps.setDataColumn(columnName);
        }
        DataColumn dc = new DataColumn(ps.getInfile(), ps.getDataColumn());
        DataSeries ds = mDataSeries.get(dc);
        TimeSeries ts = new TimeSeries(ps.getLegend(), FixedMillisecond.class);
        int numSamples = 0;
        for (PlotDataIterator pdIter = new PlotDataIterator(ps, ds); pdIter.hasNext(); numSamples++) {
            Pair<Date, Double> entry = pdIter.next();
            Date tstamp = entry.getFirst();
            double val = entry.getSecond().doubleValue();
            if (val != 0 || cs.getPlotZero()) {
                if (d < minValue)
                    minValue = val;
                if (d > maxValue)
                    maxValue = val;
                count++;
                total += val;
                try {
                    ts.addOrUpdate(new FixedMillisecond(tstamp), val);
                } catch (SeriesException e) {
                    e.printStackTrace(System.out);
                }
            }
        }
        if (numSamples == 0 && ps.getOptional()) {
            System.out.format("Skipping optional plot %s (no data sample found)\n\n", ps.getLegend());
            continue;
        }
        System.out.format("Adding %d %s points to %s.\n\n", ds.size(), ps.getLegend(), cs.getOutfile());
        if (ps.getShowRaw()) {
            data.addSeries(ts);
        }
        if (ps.getShowMovingAvg()) {
            int numPoints = ps.getMovingAvgPoints();
            if (numPoints == PlotSettings.DEFAULT_PLOT_MOVING_AVG_POINTS) {
                // Display 200 points for moving average.
                // Divide the total number of points by 200 to
                // determine the number of samples to average
                // for each point.
                numPoints = ts.getItemCount() / 200;
            }
            if (numPoints >= 2) {
                TimeSeries ma = MovingAverage.createPointMovingAverage(ts, ps.getLegend() + " (moving avg)",
                        numPoints);
                data.addSeries(ma);
            } else {
                System.out.println("Not enough data to display moving average for " + ps.getLegend());
                data.addSeries(ts);

            }
        }

    }
    // Create chart
    boolean legend = (data.getSeriesCount() > 1);
    JFreeChart chart = ChartFactory.createTimeSeriesChart(null, cs.getXAxis(), cs.getYAxis(), data, legend,
            false, false);

    // Make Y-axis logarithmic if a spike was detected
    if (cs.getAllowLogScale() && (minValue > 0) && (maxValue > 0) && (maxValue > 20 * (total / count))) {
        if (maxValue / minValue > 100) {
            XYPlot plot = (XYPlot) chart.getPlot();
            ValueAxis oldAxis = plot.getRangeAxis();
            LogarithmicAxis newAxis = new LogarithmicAxis(oldAxis.getLabel());
            plot.setRangeAxis(newAxis);
        }
    }

    mChartMap.put(cs, chart);
    return Arrays.asList(chart);

}

From source file:org.kuali.kra.proposaldevelopment.bo.DevelopmentProposal.java

/**
 * In the case where a person is in the proposal twice (Investigator and Key Person),
 * this method's return list contains only the Investigator.
 * //w  w  w. ja  v a 2  s  .  c  o m
 * @see org.kuali.kra.budget.core.BudgetParent#getPersonRolodexList()
 */
public List<PersonRolodex> getPersonRolodexList() {
    ArrayList<PersonRolodex> filtered = new ArrayList<PersonRolodex>();
    for (ProposalPerson person : getProposalPersons()) {
        if (!filtered.contains(person))
            filtered.add(person);
        else {
            if (person.isInvestigator()) {
                filtered.remove(person);
                filtered.add(person);
            }
        }
    }
    return filtered;
}

From source file:com.stratelia.silverpeas.versioningPeas.servlets.VersioningRequestRouter.java

@Override
public String getDestination(String function, VersioningSessionController versioningSC,
        HttpServletRequest request) {//from w  ww .  j  a v  a 2s.c om
    String destination = "";
    SilverTrace.info("versioningPeas", "VersioningRequestRouter.getDestination()", "root.MSG_GEN_PARAM_VALUE",
            "User=" + versioningSC.getUserId() + " Function=" + function);
    String rootDestination = "/versioningPeas/jsp/";
    ResourceLocator messages = new ResourceLocator(
            "com.stratelia.silverpeas.versioningPeas.multilang.versioning", versioningSC.getLanguage());
    try {
        String flag = versioningSC.getProfile();

        request.setAttribute("Profile", flag);
        if (function.startsWith("ViewReadersList")) {
            List<Group> groups = new ArrayList<Group>();
            List<String> users = new ArrayList<String>();
            ProfileInst profile = versioningSC.getCurrentProfile(VersioningSessionController.READER);
            if (profile != null) {
                groups = versioningSC.groupIds2Groups(profile.getAllGroups());
                users = versioningSC.userIds2Users(profile.getAllUsers());
            }
            request.setAttribute("Groups", groups);
            request.setAttribute("Users", users);
            destination = rootDestination + "ReadList.jsp";
        } else if (function.startsWith("ViewWritersList")) {
            Document document = versioningSC.getEditingDocument();

            ProfileInst profile = versioningSC.getCurrentProfile(VersioningSessionController.WRITER);
            ArrayList<Worker> workers = new ArrayList<Worker>();
            if (profile != null) {
                workers = document.getWorkList();
                if (document.getCurrentWorkListOrder() == Integer
                        .parseInt(VersioningSessionController.WRITERS_LIST_ORDERED)
                        && !versioningSC.isAlreadyMerged() && !profile.getAllGroups().isEmpty()) {
                    // Need to merge users from groups with other users
                    workers = versioningSC.mergeUsersFromGroupsWithWorkers(profile.getAllGroups(), workers);
                }
            }
            request.setAttribute("Workers", workers);
            destination = rootDestination + "WorkList.jsp";
        } else if (function.startsWith("ChangeOrder")) {
            String lines = request.getParameter("lines");
            Document document = versioningSC.getEditingDocument();
            ArrayList<Worker> users = document.getWorkList();
            if (lines != null) {
                int users_count = Integer.parseInt(lines);
                if (users_count == users.size()) {
                    ArrayList<Worker> new_users = new ArrayList<Worker>(users_count);

                    for (int i = 0; i < users_count; i++) {
                        Worker user = users.get(i);
                        boolean v_value = false;

                        // Validator
                        String chvi = request.getParameter("chv" + i);
                        if (chvi != null) {
                            v_value = true;
                        }
                        user.setApproval(v_value);
                        new_users.add(user);
                    }

                    // Sorting begin
                    int upIndex = Integer.parseInt(request.getParameter("up"));
                    int downIndex = Integer.parseInt(request.getParameter("down"));
                    int addIndex = Integer.parseInt(request.getParameter("add"));

                    // Remove user to change order
                    if (upIndex > 0 && upIndex < users_count) {
                        Worker user = new_users.remove(upIndex);
                        new_users.add(upIndex - 1, user);
                    }
                    if (downIndex >= 0 && downIndex < users_count - 1) {
                        Worker user = new_users.remove(downIndex);
                        new_users.add(downIndex + 1, user);
                    }

                    if (addIndex >= 0 && addIndex < users_count) {
                        Worker user = new_users.get(addIndex);
                        Worker new_user = new Worker(user.getUserId(),
                                Integer.parseInt(versioningSC.getEditingDocument().getPk().getId()), 0,
                                user.isApproval(), true, versioningSC.getComponentId(), user.getType(),
                                user.isSaved(), user.isUsed(), user.getListType());
                        new_users.add(addIndex + 1, new_user);
                        users_count++;
                    }

                    for (int i = 0; i < users_count; i++) {
                        new_users.get(i).setOrder(i);
                    }
                    document.setWorkList(new_users);
                }
            }
            destination = getDestination("ViewWritersList", versioningSC, request);
        } else if (function.startsWith("ChangeListType")) {
            Document document = versioningSC.getEditingDocument();
            String listType = request.getParameter("ListType");
            if (!StringUtil.isDefined(listType)) {
                listType = VersioningSessionController.WRITERS_LIST_SIMPLE;
            }
            document.setCurrentWorkListOrder(Integer.parseInt(listType));
            ProfileInst profile = versioningSC.getProfile(VersioningSessionController.WRITER);
            ArrayList<Worker> workers = new ArrayList<Worker>();
            if (profile != null) {
                if (listType.equals(VersioningSessionController.WRITERS_LIST_ORDERED)) {
                    // Need to merge users from groups with other users
                    workers = document.getWorkList();
                    workers = versioningSC.mergeUsersFromGroupsWithWorkers(profile.getAllGroups(), workers);
                    versioningSC.setAlreadyMerged(true);
                } else {
                    ArrayList<Worker> workersUsers = new ArrayList<Worker>();
                    ArrayList<Worker> workersGroups = new ArrayList<Worker>();

                    workersGroups = versioningSC.convertGroupsToWorkers(workers, profile.getAllGroups());
                    workers.addAll(workersGroups);

                    workersUsers = versioningSC.convertUsersToWorkers(workers, profile.getAllUsers());
                    workers.addAll(workersUsers);
                }
            }
            document.setWorkList(workers);
            versioningSC.updateWorkList(document);
            versioningSC.updateDocument(document);
            destination = getDestination("ViewWritersList", versioningSC, request);
        } else if (function.startsWith("SaveListType")) {
            Document document = versioningSC.getEditingDocument();
            ArrayList<Worker> users = document.getWorkList();
            ArrayList<Worker> updateUsers = new ArrayList<Worker>();
            for (int i = 0; i < users.size(); i++) {
                Worker user = users.get(i);
                // Set approval rights to users
                String chvi = request.getParameter("chv" + i);
                boolean v_value = false;
                if (chvi != null) {
                    v_value = true;
                }
                user.setApproval(v_value);
                updateUsers.add(user);
            }
            versioningSC.updateWorkList(document);
            versioningSC.updateDocument(document);
            destination = getDestination("ViewWritersList", versioningSC, request);
        } else if (function.startsWith("ViewVersions")) {
            request.setAttribute("Document", versioningSC.getEditingDocument());
            destination = rootDestination + "versions.jsp";
        } else if (function.equals("SelectUsersGroupsProfileInstance")) {
            String role = request.getParameter("Role");
            String listType = request.getParameter("ListType");
            if (StringUtil.isDefined(listType)) {
                versioningSC.getEditingDocument().setCurrentWorkListOrder(Integer.parseInt(listType));
            }
            versioningSC.initUserPanelInstanceForGroupsUsers(role);
            destination = Selection.getSelectionURL(Selection.TYPE_USERS_GROUPS);
        } else if (function.startsWith("DocumentProfileSetUsersAndGroups")) {
            String role = request.getParameter("Role");
            ProfileInst profile = versioningSC.getProfile(role);
            versioningSC.updateDocumentProfile(profile);
            if (role.equals(VersioningSessionController.WRITER)) {

                ArrayList<Worker> oldWorkers = versioningSC.getEditingDocument().getWorkList();
                ArrayList<Worker> workers = new ArrayList<Worker>();
                ArrayList<Worker> workersUsers = new ArrayList<Worker>();
                ArrayList<Worker> workersGroups = new ArrayList<Worker>();

                workersGroups = versioningSC.convertGroupsToWorkers(oldWorkers, profile.getAllGroups());
                workers.addAll(workersGroups);

                workersUsers = versioningSC.convertUsersToWorkers(oldWorkers, profile.getAllUsers());
                workers.addAll(workersUsers);
                ArrayList<Worker> sortedWorkers = new ArrayList<Worker>();
                if (workers != null) {
                    for (int i = 0; i < workers.size(); i++) {
                        Worker sortedWorker = workers.get(i);
                        sortedWorker.setOrder(i);
                        sortedWorkers.add(sortedWorker);
                    }
                    workers = sortedWorkers;
                }

                versioningSC.getEditingDocument().setWorkList(workers);
                versioningSC.updateWorkList(versioningSC.getEditingDocument());
                request.setAttribute("urlToReload", "ViewWritersList");
            } else {
                request.setAttribute("urlToReload", "ViewReadersList");
            }
            destination = rootDestination + "closeWindow.jsp";
        } else if (function.startsWith("SaveList")) {
            String role = request.getParameter("Role");
            String fromFunction = request.getParameter("From");
            if (versioningSC.isAccessListExist(role)) {
                versioningSC.removeAccessList(role);
            }
            versioningSC.saveAccessList(role);
            request.setAttribute("Message", messages.getString("versioning.ListSaved", ""));
            destination = getDestination(fromFunction, versioningSC, request);
        } else if (function.startsWith("DeleteReaderProfile")) {
            ProfileInst profile = versioningSC.getDocumentProfile(VersioningSessionController.READER);
            if (profile != null) {
                profile.removeAllGroups();
                profile.removeAllUsers();
                versioningSC.updateProfileInst(profile);
            }
            destination = getDestination("ViewReadersList", versioningSC, request);
        } else if (function.startsWith("DeleteWriterProfile")) {
            ProfileInst profile = versioningSC.getDocumentProfile(VersioningSessionController.WRITER);
            if (profile != null) {
                profile.removeAllGroups();
                profile.removeAllUsers();
                versioningSC.updateProfileInst(profile);
            }
            versioningSC.deleteWorkers(true);
            versioningSC.setAlreadyMerged(false);
            destination = getDestination("ViewWritersList", versioningSC, request);
        } else if (function.startsWith("Update")) {
            String docId = request.getParameter("DocId");
            String name = request.getParameter("name");
            String description = request.getParameter("description");
            String comments = request.getParameter("comments");
            Document document = versioningSC
                    .getDocument(new DocumentPK(Integer.parseInt(docId), versioningSC.getComponentId()));
            document.setDescription(description);
            document.setName(name);
            document.setAdditionalInfo(comments);
            versioningSC.updateDocument(document);
            versioningSC.setEditingDocument(document);
            destination = getDestination("ViewVersions", versioningSC, request);
        } else if (function.equals("CloseWindow")) {
            destination = rootDestination + "closeWindow.jsp";
        } else if (function.equals("AddNewVersion")) {

            // Display xmlForm if used
            if (StringUtil.isDefined(versioningSC.getXmlForm())) {
                setXMLFormIntoRequest(request.getParameter("documentId"), versioningSC, request);
            }

            destination = rootDestination + "newVersion.jsp";
        } else if (function.equals("AddNewOnlineVersion")) {
            String documentId = request.getParameter("documentId");

            request.setAttribute("DocumentId", documentId);
            // Display xmlForm if used
            if (StringUtil.isDefined(versioningSC.getXmlForm())) {
                setXMLFormIntoRequest(documentId, versioningSC, request);
            }

            destination = rootDestination + "newOnlineVersion.jsp";
        } else if (function.equals("ChangeValidator")) {
            String setTypeId = request.getParameter("VV");
            String setType = request.getParameter("SetType"); // 'U'for users or 'G'
            // for groups
            versioningSC.setWorkerValidator(versioningSC.getEditingDocument().getWorkList(),
                    Integer.parseInt(setTypeId), setType);
            destination = getDestination("ViewWritersList", versioningSC, request);
        } else if (function.equals("ListPublicVersionsOfDocument")) {
            String documentId = request.getParameter("DocId");
            String isAlias = request.getParameter("Alias");
            DocumentPK documentPK = new DocumentPK(Integer.parseInt(documentId), versioningSC.getSpaceId(),
                    versioningSC.getComponentId());

            Document document = versioningSC.getDocument(documentPK);
            List<DocumentVersion> publicVersions = versioningSC.getPublicDocumentVersions(documentPK);

            request.setAttribute("Document", document);
            request.setAttribute("PublicVersions", publicVersions);
            request.setAttribute("Alias", isAlias);
            destination = "/versioningPeas/jsp/publicVersions.jsp";
        } else if ("ViewAllVersions".equals(function)) {
            return viewVersions(request, versioningSC);
        } else if ("saveOnline".equals(function)) {
            if (!StringUtil.isDefined(request.getCharacterEncoding())) {
                request.setCharacterEncoding("UTF-8");
            }
            String encoding = request.getCharacterEncoding();
            List<FileItem> items = FileUploadUtil.parseRequest(request);

            String documentId = FileUploadUtil.getParameter(items, "documentId", "-1", encoding);
            DocumentPK documentPK = new DocumentPK(Integer.parseInt(documentId), versioningSC.getSpaceId(),
                    versioningSC.getComponentId());
            Document document = versioningSC.getDocument(documentPK);
            String userId = versioningSC.getUserId();
            String radio = FileUploadUtil.getParameter(items, "radio", "", encoding);
            String comments = FileUploadUtil.getParameter(items, "comments", "", encoding);
            boolean force = "true".equalsIgnoreCase(request.getParameter("force_release"));

            String callback = FileUploadUtil.getParameter(items, "Callback");
            request.setAttribute("Callback", callback);
            destination = "/versioningPeas/jsp/documentSaved.jsp";

            boolean addXmlForm = !isXMLFormEmpty(versioningSC, items);

            DocumentVersionPK newVersionPK = versioningSC.saveOnline(document, comments, radio,
                    Integer.parseInt(userId), force, addXmlForm);
            if (newVersionPK != null) {
                request.setAttribute("DocumentId", documentId);
                DocumentVersion version = versioningSC.getLastVersion(documentPK);
                request.setAttribute("Version", version);
                if (addXmlForm) {
                    saveXMLData(versioningSC, newVersionPK, items);
                }
            } else {
                if ("admin".equals(versioningSC.getUserRoleLevel())) {
                    // TODO MANU ecrire la page pour ressoumettre en forcant
                    destination = "/versioningPeas/jsp/forceDocumentLocked.jsp";
                } else {
                    destination = "/versioningPeas/jsp/documentLocked.jsp";
                }
            }
        } else if ("Checkout".equals(function)) {
            String documentId = request.getParameter("DocId");
            DocumentPK documentPK = new DocumentPK(Integer.parseInt(documentId), versioningSC.getSpaceId(),
                    versioningSC.getComponentId());
            Document document = versioningSC.getDocument(documentPK);
            document.setStatus(1);
            document.setLastCheckOutDate(new Date());
            versioningSC.checkDocumentOut(documentPK, Integer.parseInt(versioningSC.getUserId()), new Date());
            document = versioningSC.getDocument(documentPK);
            versioningSC.setEditingDocument(document);
            request.setAttribute("Document", document);
            destination = rootDestination + "versions.jsp";
        } else if ("DeleteDocumentRequest".equals(function)) {
            String documentId = request.getParameter("DocId");
            String url = request.getParameter("Url");
            request.setAttribute("DocId", documentId);
            request.setAttribute("Url", url);
            destination = rootDestination + "deleteDocument.jsp";
        } else if (function.equals("DeleteDocument")) {
            String documentId = request.getParameter("DocId");
            String url = request.getParameter("Url");
            DocumentPK documentPK = new DocumentPK(Integer.parseInt(documentId), versioningSC.getSpaceId(),
                    versioningSC.getComponentId());
            versioningSC.deleteDocument(documentPK);
            SilverTrace.info("versioningPeas", "VersioningRequestRouter.getDestination()",
                    "root.MSG_GEN_PARAM_VALUE", "url=" + url);
            request.setAttribute("urlToReload", url);
            destination = rootDestination + "closeWindow.jsp";
        } else if (function.equals("AddNewDocument")) {
            String pubId = request.getParameter("PubId");
            request.setAttribute("PubId", pubId);

            if (StringUtil.isDefined(versioningSC.getXmlForm())) {
                setXMLFormIntoRequest(null, versioningSC, request);
            }

            destination = rootDestination + "newDocument.jsp";
        } else if (function.equals("SaveNewDocument")) {
            saveNewDocument(request, versioningSC);
            destination = getDestination("ViewVersions", versioningSC, request);
        } else if (function.equals("SaveNewVersion")) {
            if (!StringUtil.isDefined(request.getCharacterEncoding())) {
                request.setCharacterEncoding("UTF-8");
            }
            String encoding = request.getCharacterEncoding();
            List<FileItem> items = FileUploadUtil.parseRequest(request);

            String type = FileUploadUtil.getParameter(items, "type", "", encoding);
            String comments = FileUploadUtil.getParameter(items, "comments", "", encoding);
            String radio = FileUploadUtil.getParameter(items, "radio", "", encoding);
            String documentId = FileUploadUtil.getParameter(items, "documentId", "-1", encoding);

            // Save file on disk
            FileItem fileItem = FileUploadUtil.getFile(items, "file_upload");
            boolean runOnUnix = !FileUtil.isWindows();
            String logicalName = fileItem.getName();
            String physicalName = "dummy";
            String mimeType = "dummy";
            File dir = null;
            int size = 0;
            if (logicalName != null) {

                if (runOnUnix) {
                    logicalName = logicalName.replace('\\', File.separatorChar);
                }

                logicalName = logicalName.substring(logicalName.lastIndexOf(File.separator) + 1,
                        logicalName.length());
                type = logicalName.substring(logicalName.lastIndexOf(".") + 1, logicalName.length());
                physicalName = new Long(new Date().getTime()).toString() + "." + type;
                mimeType = FileUtil.getMimeType(logicalName);
                if (!StringUtil.isDefined(mimeType)) {
                    mimeType = "unknown";
                }
                dir = new File(versioningSC.createPath(versioningSC.getComponentId(), null) + physicalName);
                size = new Long(fileItem.getSize()).intValue();
                fileItem.write(dir);
            }

            // create DocumentVersion
            String componentId = versioningSC.getComponentId();
            DocumentPK docPK = new DocumentPK(Integer.parseInt(documentId), "useless", componentId);
            int userId = Integer.parseInt(versioningSC.getUserId());

            DocumentVersion documentVersion = null;
            DocumentVersion lastVersion = versioningSC.getLastVersion(docPK);
            if (com.stratelia.silverpeas.versioning.ejb.RepositoryHelper.getJcrDocumentService()
                    .isNodeLocked(lastVersion)) {
                destination = rootDestination + "documentLocked.jsp";
            } else {

                List<DocumentVersion> versions = versioningSC.getDocumentVersions(docPK);
                int majorNumber = 0;
                int minorNumber = 1;
                if (versions != null && versions.size() > 0) {
                    documentVersion = versions.get(0);
                    majorNumber = documentVersion.getMajorNumber();
                    minorNumber = documentVersion.getMinorNumber();
                    DocumentVersion newVersion = new DocumentVersion(null, docPK, majorNumber, minorNumber,
                            userId, new Date(), comments, Integer.parseInt(radio), documentVersion.getStatus(),
                            physicalName, logicalName, mimeType, size, componentId);

                    boolean addXmlForm = !isXMLFormEmpty(versioningSC, items);
                    if (addXmlForm) {
                        newVersion.setXmlForm(versioningSC.getXmlForm());
                    }

                    newVersion = versioningSC.addNewDocumentVersion(newVersion);
                    ResourceLocator settings = new ResourceLocator(
                            "com.stratelia.webactiv.util.attachment.Attachment", "");
                    boolean actifyPublisherEnable = settings.getBoolean("ActifyPublisherEnable", false);
                    // Specific case: 3d file to convert by Actify Publisher
                    if (actifyPublisherEnable) {
                        String extensions = settings.getString("Actify3dFiles");
                        StringTokenizer tokenizer = new StringTokenizer(extensions, ",");
                        // 3d native file ?
                        boolean fileForActify = false;
                        SilverTrace.info("versioningPeas", "saveFile.jsp", "root.MSG_GEN_PARAM_VALUE",
                                "nb tokenizer =" + tokenizer.countTokens());
                        while (tokenizer.hasMoreTokens() && !fileForActify) {
                            String extension = tokenizer.nextToken();
                            if (type.equalsIgnoreCase(extension)) {
                                fileForActify = true;
                            }
                        }
                        if (fileForActify) {
                            String dirDestName = "v_" + componentId + "_" + documentId;
                            String actifyWorkingPath = settings.getString("ActifyPathSource") + File.separator
                                    + dirDestName;

                            String destPath = FileRepositoryManager.getTemporaryPath() + actifyWorkingPath;
                            if (!new File(destPath).exists()) {
                                FileRepositoryManager.createGlobalTempPath(actifyWorkingPath);
                            }

                            String destFile = FileRepositoryManager.getTemporaryPath() + actifyWorkingPath
                                    + File.separator + logicalName;
                            FileRepositoryManager.copyFile(
                                    versioningSC.createPath(componentId, null) + File.separator + physicalName,
                                    destFile);
                        }
                    }
                    if (addXmlForm) {
                        saveXMLData(versioningSC, newVersion.getPk(), items);
                    }
                }

                String returnURL = FileUploadUtil.getParameter(items, "ReturnURL");
                if (!StringUtil.isDefined(returnURL)) {
                    destination = getDestination("ViewVersions", versioningSC, request);
                } else {
                    request.setAttribute("urlToReload", returnURL);
                    destination = rootDestination + "closeWindow.jsp";
                }
            }
        } else {
            destination = rootDestination + function;
        }
    } catch (Exception e) {
        SilverTrace.error("versioning", "VersioningRequestRouter.getDestination",
                "root.EX_CANT_GET_REQUEST_DESTINATION", e);
        request.setAttribute("javax.servlet.jsp.jspException", e);
        destination = "/admin/jsp/errorpageMain.jsp";
    }
    SilverTrace.info("versioningPeas", "VersioningRequestRouter.getDestination()", "root.MSG_GEN_PARAM_VALUE",
            "Destination=" + destination);
    return destination;
}

From source file:gov.nih.nci.cabig.caaers.api.impl.AdverseEventManagementServiceImpl.java

/**
 * To Create or Update Advese Events.//from   ww  w  .ja v  a 2  s .  c o  m
 * Sync Flag is used only incase of SAE Evaluation service, As service can soft-delete the Adverse Events.
 * @param rpSrc
 * @param errors
 * @param syncFlag
 * @return
 */

public AdverseEventReportingPeriod createOrUpdateAdverseEvents(AdverseEventReportingPeriod rpSrc,
        ValidationErrors errors, boolean syncFlag) {

    Study study = fetchStudy(rpSrc.getStudy().getFundingSponsorIdentifierValue());
    if (study == null) {
        logger.error("Study not present in caAERS with the sponsor identifier : "
                + rpSrc.getStudy().getFundingSponsorIdentifierValue());
        errors.addValidationError("WS_AEMS_003",
                "Study with sponsor identifier " + rpSrc.getStudy().getFundingSponsorIdentifierValue()
                        + " does not exist in caAERS",
                new String[] { rpSrc.getStudy().getFundingSponsorIdentifierValue() });
        return null;
    }
    try {
        adeersIntegrationFacade.updateStudy(study.getId(), true);
    } catch (Exception e) {
        logger.warn("Study synchronization failed.", e);
    }
    //migrate the domain object
    AdverseEventReportingPeriod rpDest = new AdverseEventReportingPeriod();
    DomainObjectImportOutcome<AdverseEventReportingPeriod> rpOutcome = new DomainObjectImportOutcome<AdverseEventReportingPeriod>();
    reportingPeriodMigrator.migrate(rpSrc, rpDest, rpOutcome);
    logger.info("Reporting period migration result :" + String.valueOf(rpOutcome.getMessages()));
    if (rpOutcome.hasErrors()) {
        //translate error and create a response.
        logger.error("Errors while migrating :" + String.valueOf(rpOutcome.getErrorMessages()));
        errors.addValidationErrors(rpOutcome.getValidationErrors().getErrors());
        return null;
    }
    //check if we need the create path or update path.
    String tac = rpDest.getTreatmentAssignment() != null ? rpDest.getTreatmentAssignment().getCode()
            : rpDest.getTreatmentAssignmentDescription();
    String epochName = rpDest.getEpoch() != null ? rpDest.getEpoch().getName() : null;
    AdverseEventReportingPeriod rpFound = rpDest.getAssignment().findReportingPeriod(rpDest.getExternalId(),
            rpDest.getStartDate(), rpDest.getEndDate(), rpDest.getCycleNumber(), epochName, tac);
    ArrayList<AdverseEventReportingPeriod> reportingPeriodList = new ArrayList<AdverseEventReportingPeriod>(
            rpDest.getAssignment().getActiveReportingPeriods());
    if (rpFound != null) {
        // This is used only incase of SAE Evaluation Service.
        if (syncFlag) {
            syncAdverseEventWithSrc(rpFound, rpSrc);
        }
        int i = findIndexFromReportPeriodList(reportingPeriodList, rpFound);
        if (i >= 0)
            reportingPeriodList.remove(i);
    }

    ValidationErrors dateValidationErrors = validateRepPeriodDates(rpDest, reportingPeriodList,
            rpDest.getAssignment().getStartDateOfFirstCourse(), rpDest.getEpoch());
    logger.info("Reporting period validation result :" + String.valueOf(dateValidationErrors));
    if (dateValidationErrors.hasErrors()) {
        //translate errors and create a response
        logger.error("Errors while migrating :" + String.valueOf(dateValidationErrors));
        errors.addValidationErrors(dateValidationErrors.getErrors());
        return null;
    }

    //validate adverse events
    for (AdverseEvent adverseEvent : rpDest.getAdverseEvents()) {
        Set<ConstraintViolation<AdverseEvent>> constraintViolations = validator.validate(adverseEvent,
                AdverseEventGroup.class, Default.class);
        if (!constraintViolations.isEmpty()) {
            //translate errors to response.
            for (ConstraintViolation<AdverseEvent> v : constraintViolations) {
                errors.addValidationError("WS_GEN_006", v.getMessage(), v.getPropertyPath());
            }
            return null;
        }
    }
    // validate Reporting Period.
    AdverseEventReportingPeriod rpTarget = rpFound;
    if (rpTarget == null)
        rpTarget = rpDest;

    Set<ConstraintViolation<AdverseEventReportingPeriod>> constraintViolations = validator.validate(rpTarget,
            CourseCycleGroup.class, Default.class);
    if (!constraintViolations.isEmpty()) {
        //translate errors to response.
        for (ConstraintViolation<AdverseEventReportingPeriod> v : constraintViolations) {
            errors.addValidationError("WS_GEN_006", v.getMessage(), v.getPropertyPath());
        }
        return null;
    }

    if (rpFound == null) {
        //new reporting period
        rpFound = rpDest;
        rpFound.getAssignment().addReportingPeriod(rpFound);
        // Validate the Reporting Period before saving.
        adverseEventValidatior.validate(rpFound, rpFound.getStudy(), errors);
        adverseEventReportingPeriodDao.save(rpFound);
        if (isWorkflowEnabled()) {
            Long wfId = adverseEventRoutingAndReviewRepository.enactReportingPeriodWorkflow(rpFound);
            logger.debug("Enacted workflow : " + wfId);
        }
    } else {
        //existing reporting period.
        reportingPeriodSynchronizer.migrate(rpDest, rpFound, rpOutcome);
        // Validate the Reporting Period before saving.
        adverseEventValidatior.validate(rpFound, rpFound.getStudy(), errors);
        if (errors.hasErrors()) {
            logger.error(
                    "Error(s) while validating with Adverse Event " + String.valueOf(errors.getErrorCount()));
            return null;
        }

        adverseEventReportingPeriodDao.save(rpFound);

    }
    return rpFound;
}

From source file:fitnesserefactor.FitnesseRefactor.java

private void AddColumnActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_AddColumnActionPerformed

    if (ParentFolder.isSelected()) {
        if (!"".equals(JtreePath)) {
            File dir = new File(JtreePath);
            try {
                if (dir.getParent() != null && dir.isFile()) {
                    String Testcase = dir.getParent();
                    File dir1 = new File(Testcase);
                    if (dir1.getParent() != null && dir1.isDirectory()) {
                        String parent = dir1.getParent();
                        File dir2 = new File(Testcase);
                        if (dir2.getParent() != null) {
                            String GrandParent = dir2.getParent();
                            File dir3 = new File(GrandParent);
                            File[] allFiles = dir3.listFiles();
                            ArrayList al = new ArrayList();
                            for (File file : allFiles) {
                                if (allFiles.length <= al.size())
                                    break;
                                String TestcaseNames = file.getName();
                                al.add(TestcaseNames);
                            }//w w w. j a va 2  s  .co m
                            if (!fromTxt.getText().isEmpty() && !toTxt.getText().isEmpty()) {
                                int FromTxtInt = Integer.parseInt(fromTxt.getText());
                                int ToTxtInt = Integer.parseInt(toTxt.getText());
                                int TotFiles = al.size();
                                if ((ToTxtInt > FromTxtInt) && (ToTxtInt > 0) && (FromTxtInt > 0)
                                        && ToTxtInt <= al.size() - 2) {
                                    for (int removeAfterToIndx = (ToTxtInt
                                            + 2); removeAfterToIndx < TotFiles; removeAfterToIndx++) {
                                        al.remove(ToTxtInt + 2);
                                    }
                                    if (FromTxtInt > 1) {
                                        for (int removeFromIndx = 1; removeFromIndx < FromTxtInt; removeFromIndx++) {
                                            al.remove(2);
                                        }
                                    }

                                } else {
                                    JOptionPane.showMessageDialog(null,
                                            "FROM test case number cannot be greater/equal to TO test case number.\nFROM and TO test case numbers should be greater than zero. \nTO test case number should not be greater than the testcase count ",
                                            "Warning", JOptionPane.WARNING_MESSAGE);
                                }
                            }
                            for (int i = 2; i <= al.size(); i++) {
                                System.out.println(dir2.getParent() + "\\" + al.get(i) + "\\content.txt");
                                File EachFile = new File(dir2.getParent() + "\\" + al.get(i) + "\\content.txt");
                                for (int j = 0; j < FileUtils.readLines(EachFile).size(); j++) {
                                    String line = (String) FileUtils.readLines(EachFile).get(j);
                                    if (line.matches("(.*)" + FitnesseTablesList.getSelectedItem().toString()
                                            + "(.*)")) {
                                        p = j;
                                        ReadAllLines(EachFile);
                                        int linNum = CountLines(EachFile);
                                        ModifiedTable(EachFile, Integer.parseInt(ColumnField.getText()));
                                        int ModifiedLen = ModifiedLines.size();
                                        System.out.println(ModifiedLen);
                                        int tblIndex = 1;
                                        for (int SetNew = 0; SetNew < ModifiedLen; SetNew++) {
                                            AllLines.set(p + tblIndex, ModifiedLines.get(SetNew).toString());
                                            tblIndex = tblIndex + 1;
                                        }
                                        WriteAllLines(EachFile);
                                    }
                                }
                            }
                        } else {
                            JOptionPane.showMessageDialog(null,
                                    "There are no testcases under the parent folder", "Error",
                                    JOptionPane.ERROR_MESSAGE);
                        }
                    } else {
                        JOptionPane.showMessageDialog(null, "There are no testcases under the parent folder",
                                "Error", JOptionPane.ERROR_MESSAGE);
                    }
                } else {
                    JOptionPane.showMessageDialog(null, "Need to select atleast one content.txt file",
                            "Warning", JOptionPane.WARNING_MESSAGE);
                }
            } catch (Exception E) {
                //JOptionPane.showMessageDialog(null, E, "Java Runtime Error", JOptionPane.ERROR );
            }
        }

    }
}

From source file:com.hygenics.parser.GetImages.java

private void getImages() {
    // controls the web process from a removed method
    log.info("Setting Up Pull");
    String[] proxyarr = (proxies == null) ? null : proxies.split(",");
    // cleanup// w  w w  . j  ava 2  s  .c o m
    if (cleanup) {
        cleanupDir(fpath);
    }

    // image grab
    CookieManager cm = new CookieManager();
    cm.setCookiePolicy(CookiePolicy.ACCEPT_ALL);
    CookieHandler.setDefault(cm);
    int numimages = 0;
    InputStream is;
    byte[] bytes;
    int iter = 0;
    int found = 0;

    // set proxy if needed
    if (proxyuser != null) {
        proxy(proxyhost, proxyport, https, proxyuser, proxypass);
    }

    int i = 0;
    ArrayList<String> postImages = new ArrayList<String>();
    ForkJoinPool fjp = new ForkJoinPool(Runtime.getRuntime().availableProcessors());
    Set<Callable<String>> pulls = new HashSet<Callable<String>>();
    Set<Callable<ArrayList<String>>> sqls = new HashSet<Callable<ArrayList<String>>>();
    List<Future<String>> imageFutures;

    ArrayList<String> images;
    int chunksize = (int) Math.ceil(commitsize / numqueries);
    log.info("Chunksize: " + chunksize);
    if (baseurl != null || baseurlcolumn != null) {
        do {
            log.info("Offset: " + offset);
            log.info("Getting Images");
            images = new ArrayList<String>(commitsize);
            log.info("Getting Columns");
            for (int n = 0; n < numqueries; n++) {
                String tempsql = sql + " WHERE " + idString + " >= " + offset + " AND " + idString + " < "
                        + (offset + chunksize);

                if (conditions != null) {
                    tempsql += conditions;
                }

                sqls.add(new QueryDatabase(
                        ((extracondition != null) ? tempsql + " " + extracondition : tempsql)));

                offset += chunksize;
            }

            List<Future<ArrayList<String>>> futures = fjp.invokeAll(sqls);

            int w = 0;
            while (fjp.isQuiescent() && fjp.getActiveThreadCount() > 0) {
                w++;
            }

            for (Future<ArrayList<String>> f : futures) {
                try {
                    ArrayList<String> fjson;
                    fjson = f.get();
                    if (fjson.size() > 0) {
                        images.addAll(fjson);
                    }

                    if (f.isDone() == false) {
                        f.cancel(true);
                    }
                } catch (InterruptedException e) {
                    e.printStackTrace();
                } catch (ExecutionException e) {
                    e.printStackTrace();
                }
            }
            log.info(Integer.toString(images.size()) + " image links found. Pulling.");

            ArrayList<String> tempproxies = new ArrayList<String>();

            if (proxyarr != null) {
                for (String proxy : proxyarr) {
                    tempproxies.add(proxy.trim());
                }
            }

            if (maxproxies > 0) {
                maxproxies -= 1;// 0 and 1 should be equivalent conditions
                // --num is not like most 0 based still due
                // to >=
            }

            // get images
            for (int num = 0; num < images.size(); num++) {
                String icols = images.get(num);
                int proxnum = (int) Math.random() * (tempproxies.size() - 1);
                String proxy = (tempproxies.size() == 0) ? null : tempproxies.get(proxnum);

                // add grab
                pulls.add(new ImageGrabber(icols, proxy));

                if (proxy != null) {
                    tempproxies.remove(proxy);
                }

                // check for execution
                if (num + 1 == images.size() || pulls.size() >= commitsize || tempproxies.size() == 0) {
                    if (tempproxies.size() == 0 && proxies != null) {
                        tempproxies = new ArrayList<String>(proxyarr.length);

                        for (String p : proxyarr) {
                            tempproxies.add(p.trim());
                        }
                    }

                    imageFutures = fjp.invokeAll(pulls);
                    w = 0;

                    while (fjp.isQuiescent() == false && fjp.getActiveThreadCount() > 0) {
                        w++;
                    }

                    for (Future<String> f : imageFutures) {
                        String add;
                        try {
                            add = f.get();

                            if (add != null) {
                                postImages.add(add);
                            }
                        } catch (InterruptedException e) {
                            e.printStackTrace();
                        } catch (ExecutionException e) {
                            e.printStackTrace();
                        }
                    }
                    imageFutures = null;// garbage collect elligible
                    pulls = new HashSet<Callable<String>>(commitsize);
                }

                if (postImages.size() >= commitsize && addtoDB == true) {
                    if (addtoDB) {
                        log.info("Posting to Database");
                        log.info("Found " + postImages.size() + " images");
                        numimages += postImages.size();
                        int size = (int) Math.floor(postImages.size() / numqueries);
                        for (int n = 0; n < numqueries; n++) {
                            if (((n + 1) * size) < postImages.size() && (n + 1) < numqueries) {
                                fjp.execute(new ImagePost(postImages.subList(n * size, (n + 1) * size)));
                            } else {
                                fjp.execute(new ImagePost(postImages.subList(n * size, postImages.size() - 1)));
                            }
                        }

                        w = 0;
                        while (fjp.isQuiescent() && fjp.getActiveThreadCount() > 0) {
                            w++;
                        }
                    }
                    found += postImages.size();
                    postImages.clear();
                }

            }

            if (postImages.size() > 0 && addtoDB == true) {
                log.info("Posting to Database");
                numimages += postImages.size();
                int size = (int) Math.floor(postImages.size() / numqueries);
                for (int n = 0; n < numqueries; n++) {
                    if (((n + 1) * size) < postImages.size()) {
                        fjp.execute(new ImagePost(postImages.subList(n * size, (n + 1) * size)));
                    } else {
                        fjp.execute(new ImagePost(postImages.subList(n * size, postImages.size())));
                    }
                }

                w = 0;
                while (fjp.isQuiescent() && fjp.getActiveThreadCount() > 0) {
                    w++;
                }

                found += postImages.size();
                postImages.clear();
            }

            // handle iterations specs
            iter += 1;
            log.info("Iteration: " + iter);
            if ((iter < iterations && found < images.size()) || tillfound == true) {
                log.info("Not All Images Obtained Trying Iteration " + iter + " of " + iterations);
                offset -= commitsize;
            } else if ((iter < iterations && found >= images.size()) && tillfound == false) {
                log.info("Images Obtained in " + iter + " iterations. Continuing.");
                iter = 0;
            } else {
                // precautionary
                log.info("Images Obtained in " + iter + " iterations. Continuing");
                iter = 0;
            }

        } while (images.size() > 0 && iter < iterations);

        if (fjp.isShutdown()) {
            fjp.shutdownNow();
        }
    }

    log.info("Complete. Check for Errors \n " + numimages + " Images Found");
}

From source file:de.tudarmstadt.tk.statistics.importer.ExternalResultsReader.java

public static List<SampleData> splitData(SampleData data, StatsConfig config) {

    List<SampleData> splitted = new ArrayList<SampleData>();

    //Use lists instead of sets to maintain order of model metadata
    ArrayList<String> featureSets = new ArrayList<String>();
    ArrayList<String> classifiers = new ArrayList<String>();
    for (Pair<String, String> metadata : data.getModelMetadata()) {
        if (!classifiers.contains(metadata.getLeft())) {
            classifiers.add(metadata.getLeft());
        }/* w w w  . ja v  a  2s  .c  o m*/
        if (!featureSets.contains(metadata.getRight())) {
            featureSets.add(metadata.getRight());
        }
    }

    //Only separate data if there's more than one independent variable
    if (!(featureSets.size() > 1 && classifiers.size() > 1)) {
        splitted.add(data);
        return splitted;
    }

    List<String> it = (config
            .getFixIndependentVariable() == StatsConfigConstants.INDEPENDENT_VARIABLES_VALUES.Classifier)
                    ? classifiers
                    : featureSets;
    for (String fixed : it) {
        ArrayList<Pair<String, String>> modelMetadata = new ArrayList<Pair<String, String>>();
        HashMap<String, ArrayList<ArrayList<Double>>> samples = new HashMap<String, ArrayList<ArrayList<Double>>>();
        HashMap<String, ArrayList<Double>> sampleAverages = new HashMap<String, ArrayList<Double>>();
        for (int i = 0; i < data.getModelMetadata().size(); i++) {
            Pair<String, String> model = data.getModelMetadata().get(i);
            boolean eq = (config
                    .getFixIndependentVariable() == StatsConfigConstants.INDEPENDENT_VARIABLES_VALUES.Classifier)
                            ? model.getLeft().equals(fixed)
                            : model.getRight().equals(fixed);
            if (eq) {
                modelMetadata.add(model);
                for (String measure : data.getSamples().keySet()) {
                    if (!samples.containsKey(measure)) {
                        samples.put(measure, new ArrayList<ArrayList<Double>>());
                        sampleAverages.put(measure, new ArrayList<Double>());
                    }
                    samples.get(measure).add(data.getSamples().get(measure).get(i));
                    sampleAverages.get(measure).add(data.getSamplesAverage().get(measure).get(i));
                }
            }
        }
        ArrayList<Pair<String, String>> baselineModelData = new ArrayList<Pair<String, String>>();
        if (data.isBaselineEvaluation()) {
            Pair<String, String> baselineModel = null;
            for (int i = 0; i < data.getBaselineModelMetadata().size(); i++) {
                boolean eq = (config
                        .getFixIndependentVariable() == StatsConfigConstants.INDEPENDENT_VARIABLES_VALUES.Classifier)
                                ? data.getBaselineModelMetadata().get(i).getLeft().equals(fixed)
                                : data.getBaselineModelMetadata().get(i).getRight().equals(fixed);
                if (eq) {
                    baselineModel = data.getBaselineModelMetadata().get(i);
                    break;
                }
            }
            if (baselineModel != null) {
                baselineModelData.add(baselineModel);
                int modelIndex = modelMetadata.indexOf(baselineModel);
                modelMetadata.remove(modelIndex);
                modelMetadata.add(0, baselineModel);
                for (String measure : data.getSamples().keySet()) {
                    ArrayList<Double> s = samples.get(measure).get(modelIndex);
                    samples.get(measure).remove(modelIndex);
                    samples.get(measure).add(0, s);
                    double a = sampleAverages.get(measure).get(modelIndex);
                    sampleAverages.get(measure).remove(modelIndex);
                    sampleAverages.get(measure).add(0, a);
                }
            } else {
                logger.log(Level.ERROR,
                        "Missing baseline model! Please check if baseline indicators are set correctly in the input file, and if they correspond correctly to the fixIndependentVariable property in the configuration. In case of both varying feature sets and classifiers, baseline indicators have to be set multiple times.");
                System.err.println(
                        "Missing baseline model! Please check if baseline indicators are set correctly in the input file, and if they correspond correctly to the fixIndependentVariable property in the configuration. In case of both varying feature sets and classifiers, baseline indicators have to be set multiple times.");
                System.exit(1);
            }
        }
        SampleData newData = new SampleData(null, samples, sampleAverages, data.getDatasetNames(),
                modelMetadata, baselineModelData, data.getPipelineType(), data.getnFolds(),
                data.getnRepetitions());
        splitted.add(newData);
    }
    return splitted;
}

From source file:com.nononsenseapps.notepad.sync.SyncAdapter.java

@SuppressWarnings("unchecked")
@Override//from  w w  w . java 2s . co m
public void onPerformSync(Account account, Bundle extras, String authority, ContentProviderClient provider,
        SyncResult syncResult) {
    final SharedPreferences settings = PreferenceManager.getDefaultSharedPreferences(mContext);

    // Only sync if it has been enabled by the user, and account is selected
    // Issue on reinstall where account approval is remembered by system
    if (settings.getBoolean(SyncPrefs.KEY_SYNC_ENABLE, false)
            && !settings.getString(SyncPrefs.KEY_ACCOUNT, "").isEmpty()
            && account.name.equals(settings.getString(SyncPrefs.KEY_ACCOUNT, ""))) {

        if (SYNC_DEBUG_PRINTS)
            Log.d(TAG, "onPerformSync");
        Intent i = new Intent(SYNC_STARTED);
        mContext.sendBroadcast(i);
        // For later
        Intent doneIntent = new Intent(SYNC_FINISHED);
        doneIntent.putExtra(SYNC_RESULT, ERROR);

        // Initialize necessary stuff
        GoogleDBTalker dbTalker = new GoogleDBTalker(account.name, provider);
        GoogleAPITalker apiTalker = new GoogleAPITalker();

        try {
            boolean connected = apiTalker.initialize(accountManager, account, AUTH_TOKEN_TYPE,
                    NOTIFY_AUTH_FAILURE);

            if (connected) {
                if (SYNC_DEBUG_PRINTS)
                    Log.d(TAG, "We got an authToken atleast");

                try {
                    // FIrst of all, we need the latest updated time later.
                    // So
                    // save
                    // that for now.
                    // This is the latest time we synced
                    String lastUpdate = dbTalker.getLastUpdated(account.name);
                    //String lastUpdate = settings.getString(PREFS_LAST_SYNC_DATE, null);
                    // Get the latest hash value we saw on the server
                    String localEtag = settings.getString(PREFS_LAST_SYNC_ETAG, "");

                    // Prepare lists for items
                    ArrayList<GoogleTaskList> listsToSaveToDB = new ArrayList<GoogleTaskList>();
                    HashMap<GoogleTaskList, ArrayList<GoogleTask>> tasksInListToSaveToDB = new HashMap<GoogleTaskList, ArrayList<GoogleTask>>();

                    HashMap<Long, ArrayList<GoogleTask>> tasksInListToUpload = new HashMap<Long, ArrayList<GoogleTask>>();
                    HashMap<Long, ArrayList<GoogleTask>> allTasksInList = new HashMap<Long, ArrayList<GoogleTask>>();

                    // gets all tasks in one query
                    ArrayList<GoogleTask> allTasks = dbTalker.getAllTasks(allTasksInList, tasksInListToUpload);

                    ArrayList<GoogleTaskList> listsToUpload = new ArrayList<GoogleTaskList>();
                    ArrayList<GoogleTaskList> allLocalLists = new ArrayList<GoogleTaskList>();

                    // gets all lists in one query
                    dbTalker.getAllLists(allLocalLists, listsToUpload);

                    // Get the current hash value on the server and all
                    // remote
                    // lists if upload is not true

                    String serverEtag = apiTalker.getModifiedLists(localEtag, allLocalLists, listsToSaveToDB);

                    // IF the tags match, then nothing has changed on
                    // server.
                    if (localEtag.equals(serverEtag)) {
                        if (SYNC_DEBUG_PRINTS)
                            Log.d(TAG, "Etags match, nothing to download");
                    } else {
                        if (SYNC_DEBUG_PRINTS)
                            Log.d(TAG, "Etags dont match, downloading new tasks");
                        // Download tasks which have been updated since last
                        // time
                        for (GoogleTaskList list : listsToSaveToDB) {
                            if (list.id != null && !list.id.isEmpty()) {
                                if (SYNC_DEBUG_PRINTS)
                                    Log.d(TAG, "Saving remote modified tasks for: " + list.id);
                                tasksInListToSaveToDB.put(list,
                                        list.downloadModifiedTasks(apiTalker, allTasks, lastUpdate));
                            }
                        }
                    }

                    if (SYNC_DEBUG_PRINTS)
                        Log.d(TAG, "Getting stuff we want to upload");
                    // Get stuff we would like to upload to server
                    // In case of lists, locally modified versions always
                    // wins
                    // in
                    // conflict, so nothing more to do

                    for (GoogleTaskList list : allLocalLists) {
                        ArrayList<GoogleTask> moddedTasks = tasksInListToUpload.get(list.dbId);
                        if (moddedTasks != null && !moddedTasks.isEmpty()) {
                            // There are some tasks here which we want to
                            // upload
                            if (SYNC_DEBUG_PRINTS)
                                Log.d(TAG, "List id " + list.dbId + ", Locally modified tasks found: "
                                        + moddedTasks.size());

                            // Now we need to handle possible conflicts in
                            // the
                            // tasks. But this has already been sorted when
                            // we
                            // downloaded them
                            // For any task which exists in stuffToSaveToDB,
                            // we
                            // should not upload it
                            // Iterate over a clone to avoid concurrency
                            // problems since we will be modifying
                            // the list during iteration
                            for (GoogleTask moddedTask : (ArrayList<GoogleTask>) moddedTasks.clone()) {
                                ArrayList<GoogleTask> tasksToBeSaved = tasksInListToSaveToDB.get(list);
                                if (tasksToBeSaved != null && tasksToBeSaved.contains(moddedTask)) {
                                    if (SYNC_DEBUG_PRINTS)
                                        Log.d(TAG,
                                                "This modified task was newer on server, removing from upload list: "
                                                        + moddedTask.title);
                                    moddedTasks.remove(moddedTask);
                                }
                                // In the case that a task has been deleted
                                // before it was synced the first time
                                // We should definitely not sync it. Only
                                // delete
                                // it later
                                if (moddedTask.deleted == 1
                                        && (moddedTask.id == null || moddedTask.id.isEmpty())) {
                                    moddedTasks.remove(moddedTask);
                                }
                            }
                        }
                    }

                    if (SYNC_DEBUG_PRINTS)
                        Log.d(TAG, "Uploading lists");
                    // First thing we want to do is upload stuff, because
                    // some
                    // values are updated then
                    boolean uploadedStuff = false;
                    // Start with lists
                    for (GoogleTaskList list : listsToUpload) {
                        GoogleTaskList result = apiTalker.uploadList(list);
                        uploadedStuff = true;
                        if (result != null) {
                            // Make sure that local version is the same as
                            // server's
                            for (GoogleTaskList localList : allLocalLists) {
                                if (result.equals(localList)) {
                                    localList.title = result.title;
                                    localList.id = result.id;
                                    result.dbId = localList.dbId;
                                    break;
                                }
                            }
                            listsToSaveToDB.add(result);
                        }
                    }

                    if (SYNC_DEBUG_PRINTS)
                        Log.d(TAG, "Uploading tasks");
                    // Right, now upload tasks
                    for (GoogleTaskList list : allLocalLists) {
                        ArrayList<GoogleTask> tasksToUpload = tasksInListToUpload.get(list.dbId);
                        if (tasksToUpload != null) {
                            for (GoogleTask task : tasksToUpload) {
                                GoogleTask result = apiTalker.uploadTask(task, list);
                                uploadedStuff = true;
                                // Task now has relevant fields set. Add to
                                // DB-list
                                if (tasksInListToSaveToDB.get(list) == null)
                                    tasksInListToSaveToDB.put(list, new ArrayList<GoogleTask>());
                                tasksInListToSaveToDB.get(list).add(result);
                            }
                        }
                    }

                    // Finally, get the updated etag from the server and
                    // save.
                    // Only worth doing if we actually uploaded anything
                    // Also, only do this if we are doing a full sync
                    String currentEtag = serverEtag;
                    if (uploadedStuff) {
                        currentEtag = apiTalker.getEtag();
                        //lastUpdate = dbTalker.getLastUpdated(account.name);
                    }

                    settings.edit().putString(PREFS_LAST_SYNC_ETAG, currentEtag)
                            //.putString(PREFS_LAST_SYNC_DATE, lastUpdate)
                            .commit();

                    // Now, set sorting values.
                    for (GoogleTaskList list : tasksInListToSaveToDB.keySet()) {
                        if (SYNC_DEBUG_PRINTS)
                            Log.d(TAG, "Setting position values in: " + list.id);
                        ArrayList<GoogleTask> tasks = tasksInListToSaveToDB.get(list);
                        if (tasks != null) {
                            if (SYNC_DEBUG_PRINTS)
                                Log.d(TAG, "Setting position values for #tasks: " + tasks.size());
                            ArrayList<GoogleTask> allListTasks = allTasksInList.get(list.dbId);
                            list.setSortingValues(tasks, allListTasks);
                        }
                    }

                    // Save to database in a single transaction
                    if (SYNC_DEBUG_PRINTS)
                        Log.d(TAG, "Save stuff to DB");
                    dbTalker.SaveToDatabase(listsToSaveToDB, tasksInListToSaveToDB);
                    // Commit it
                    ContentProviderResult[] result = dbTalker.apply();

                    if (SYNC_DEBUG_PRINTS)
                        Log.d(TAG, "Sync Complete!");
                    doneIntent.putExtra(SYNC_RESULT, SUCCESS);

                } catch (ClientProtocolException e) {
                    if (SYNC_DEBUG_PRINTS)
                        Log.d(TAG, "ClientProtocolException: " + e.getLocalizedMessage());
                } catch (JSONException e) {
                    if (SYNC_DEBUG_PRINTS)
                        Log.d(TAG, "JSONException: " + e.getLocalizedMessage());
                } catch (IOException e) {
                    syncResult.stats.numIoExceptions++;
                    if (SYNC_DEBUG_PRINTS)
                        Log.d(TAG, "IOException: " + e.getLocalizedMessage());
                } catch (RemoteException e) {
                    if (SYNC_DEBUG_PRINTS)
                        Log.d(TAG, "RemoteException: " + e.getLocalizedMessage());
                } catch (OperationApplicationException e) {
                    Log.d(TAG, "Joined operation failed: " + e.getLocalizedMessage());
                } catch (ClassCastException e) {
                    // GetListofLists will cast this if it returns a string. It should not return a string
                    // but it did...
                    Log.d(TAG, "ClassCastException: " + e.getLocalizedMessage());
                }

            } else {
                // return real failure
                if (SYNC_DEBUG_PRINTS)
                    Log.d(TAG, "Could not get authToken. Reporting authException");
                syncResult.stats.numAuthExceptions++;
                doneIntent.putExtra(SYNC_RESULT, LOGIN_FAIL);
            }

        } finally {
            // This must always be called or we will leak resources
            if (apiTalker != null) {
                apiTalker.closeClient();
            }

            mContext.sendBroadcast(doneIntent);

            if (SYNC_DEBUG_PRINTS)
                Log.d(TAG, "SyncResult: " + syncResult.toDebugString());
        }
    }
}