Example usage for java.util TreeMap containsKey

List of usage examples for java.util TreeMap containsKey

Introduction

In this page you can find the example usage for java.util TreeMap containsKey.

Prototype

public boolean containsKey(Object key) 

Source Link

Document

Returns true if this map contains a mapping for the specified key.

Usage

From source file:edu.umass.cs.gigapaxos.SQLPaxosLogger.java

public Map<Integer, PValuePacket> getLoggedAccepts(String paxosID, int version, int firstSlot,
        Integer maxSlot) {//from   w  w w.j  a  v a  2  s .  com

    if (ENABLE_JOURNALING && !DB_INDEX_JOURNAL)
        return this.getLoggedFromMessageLog(paxosID, version, firstSlot, maxSlot,
                PaxosPacketType.ACCEPT.getInt());

    // long t1 = System.currentTimeMillis();
    // fetch all accepts and then weed out those below firstSlot
    ArrayList<PaxosPacket> list = this.getLoggedMessages(paxosID, " and packet_type="
            + PaxosPacketType.ACCEPT.getInt() + " and " + getIntegerGTEConstraint("slot", firstSlot)
            // maxSlot is null for getting lower ballot pvalues
            + (maxSlot != null ? " and " + getIntegerLTConstraint("slot", maxSlot) : "") + " and version="
            + version);

    TreeMap<Integer, PValuePacket> accepted = new TreeMap<Integer, PValuePacket>();
    for (PaxosPacket p : list) {
        int slot = AbstractPaxosLogger.getSlotBallot(p)[0];
        assert (p instanceof AcceptPacket) : p.getType() + ":" + p;
        AcceptPacket accept = (AcceptPacket) p;
        if ((slot - firstSlot >= 0) && /* wraparound-arithmetic */
                (!accepted.containsKey(slot) || accepted.get(slot).ballot.compareTo(accept.ballot) < 0))
            accepted.put(slot, accept);
    }
    // DelayProfiler.updateDelay("getAccepts", t1);
    return accepted;
}

From source file:snpviewer.SnpViewer.java

public void writeSavedRegionsToFile() {
    if (savedRegions.size() < 1) {
        Dialogs.showErrorDialog(null, "No Saved Regions exist to write!", "No Saved Regions", "SnpViewer");
        return;/*w  w w.  j  a va 2  s  .  co m*/
    }
    final int flanks = 10;
    FileChooser fileChooser = new FileChooser();
    FileChooser.ExtensionFilter extFilter = new FileChooser.ExtensionFilter("Excel (*.xlsx)", "*.xlsx");
    fileChooser.getExtensionFilters().add(extFilter);
    fileChooser.setTitle("Write regions to Excel file (.xlsx)...");
    File rFile = fileChooser.showSaveDialog(mainWindow);
    if (rFile == null) {
        return;
    } else if (!rFile.getName().endsWith(".xlsx")) {
        rFile = new File(rFile.getAbsolutePath() + ".xlsx");
    }
    final File regionFile = rFile;
    final Task<Boolean> writeTask = new Task() {
        @Override
        protected Boolean call() throws Exception {
            try {
                updateProgress(-1, -1);
                BufferedOutputStream out = new BufferedOutputStream(new FileOutputStream(regionFile));
                Workbook wb = new XSSFWorkbook();
                //first create a summary sheet of all regions
                Sheet sheet = wb.createSheet();
                Row row = null;
                int rowNo = 0;
                int sheetNo = 0;
                wb.setSheetName(sheetNo++, "Summary");
                row = sheet.createRow(rowNo++);
                String header[] = { "Coordinates", "rsIDs", "Size (Mb)" };
                for (int col = 0; col < header.length; col++) {
                    Cell cell = row.createCell(col);
                    cell.setCellValue(header[col]);
                }
                for (int i = 0; i < savedRegions.size(); i++) {
                    row = sheet.createRow(rowNo++);
                    int col = 0;
                    Cell cell = row.createCell(col++);
                    cell.setCellValue("chr" + savedRegions.get(i).getCoordinateString());
                    cell = row.createCell(col++);
                    cell.setCellValue(savedRegions.get(i).getIdLine());
                    cell = row.createCell(col++);
                    double mB = (double) savedRegions.get(i).getLength() / 1000000;
                    cell.setCellValue(mB);
                }

                ArrayList<SnpFile> bothFiles = new ArrayList<>();
                bothFiles.addAll(affFiles);
                bothFiles.addAll(unFiles);
                String prevChrom = new String();
                double prog = 0;
                double total = savedRegions.size() * bothFiles.size() * 2;
                updateProgress(prog, total);
                int regCounter = 0;
                for (RegionSummary reg : savedRegions) {
                    updateMessage("Writing region " + ++regCounter + " of " + savedRegions.size());
                    //create a sheet for each chromosome
                    if (!reg.getChromosome().equalsIgnoreCase(prevChrom)) {
                        if (!prevChrom.isEmpty()) {

                            CellRangeAddress[] regions = {
                                    new CellRangeAddress(0, rowNo, 2, 2 + bothFiles.size()) };
                            SheetConditionalFormatting sheetCF = sheet.getSheetConditionalFormatting();

                            ConditionalFormattingRule rule1 = sheetCF
                                    .createConditionalFormattingRule(ComparisonOperator.EQUAL, "\"AA\"");
                            PatternFormatting fill1 = rule1.createPatternFormatting();
                            fill1.setFillBackgroundColor(IndexedColors.LIGHT_GREEN.index);
                            fill1.setFillPattern(PatternFormatting.SOLID_FOREGROUND);
                            ConditionalFormattingRule rule2 = sheetCF
                                    .createConditionalFormattingRule(ComparisonOperator.EQUAL, "\"BB\"");
                            PatternFormatting fill2 = rule2.createPatternFormatting();
                            fill2.setFillBackgroundColor(IndexedColors.PALE_BLUE.index);
                            fill2.setFillPattern(PatternFormatting.SOLID_FOREGROUND);
                            ConditionalFormattingRule rule3 = sheetCF
                                    .createConditionalFormattingRule(ComparisonOperator.EQUAL, "\"AB\"");
                            PatternFormatting fill3 = rule3.createPatternFormatting();
                            fill3.setFillBackgroundColor(IndexedColors.ROSE.index);
                            fill3.setFillPattern(PatternFormatting.SOLID_FOREGROUND);
                            sheetCF.addConditionalFormatting(regions, rule3, rule2);
                            sheetCF.addConditionalFormatting(regions, rule1);
                        }
                        rowNo = 0;
                        sheet = wb.createSheet();
                        wb.setSheetName(sheetNo++, reg.getChromosome());
                        prevChrom = reg.getChromosome();

                    } else {//pad regions with an empty line
                        rowNo++;
                    }
                    TreeMap<Integer, HashMap<String, String>> coordMap = new TreeMap();
                    /*coordmap - key is position, key of hashmap 
                     * is input filename and value call
                     */
                    HashMap<Integer, String> coordToId = new HashMap<>();
                    //coordinate to rs ID

                    try {
                        for (SnpFile f : bothFiles) {
                            updateProgress(prog++, total);
                            if (isCancelled()) {
                                return false;
                            }
                            List<SnpFile.SnpLine> lines = f.getSnpsInRegion(reg.getChromosome(),
                                    reg.getStartPos(), reg.getEndPos(), flanks);
                            for (SnpFile.SnpLine snpLine : lines) {
                                if (isCancelled()) {
                                    return false;
                                }
                                Integer coord = snpLine.getPosition();
                                if (!coordMap.containsKey(coord)) {
                                    coordMap.put(coord, new HashMap<String, String>());
                                }
                                String filename = f.inputFile.getName();
                                String rsId = snpLine.getId();
                                String call = snpLine.getCall();
                                coordMap.get(coord).put(filename, call);
                                coordToId.put(coord, rsId);
                            }
                        }
                        row = sheet.createRow(rowNo++);
                        Cell cell = row.createCell(0);
                        cell.setCellValue(reg.getCoordinateString());
                        row = sheet.createRow(rowNo++);
                        cell = row.createCell(0);
                        cell.setCellValue(reg.getIdLine());

                        int col = 0;
                        row = sheet.createRow(rowNo++);
                        cell = row.createCell(col++);
                        cell.setCellValue("Position");
                        cell = row.createCell(col++);
                        cell.setCellValue("rsID");
                        for (SnpFile f : bothFiles) {
                            updateProgress(prog++, total);
                            cell = row.createCell(col++);
                            if (f.getSampleName() != null && !f.getSampleName().isEmpty()) {
                                cell.setCellValue(f.getSampleName());
                            } else {
                                cell.setCellValue(f.inputFile.getName());
                            }
                        }
                        for (Entry current : coordMap.entrySet()) {
                            if (isCancelled()) {
                                return false;
                            }
                            col = 0;
                            Integer coord = (Integer) current.getKey();
                            row = sheet.createRow(rowNo++);
                            cell = row.createCell(col++);
                            cell.setCellValue(coord);
                            cell = row.createCell(col++);
                            cell.setCellValue(coordToId.get(coord));
                            HashMap<String, String> fileToCall = (HashMap<String, String>) current.getValue();
                            for (SnpFile f : bothFiles) {
                                cell = row.createCell(col++);
                                if (fileToCall.containsKey(f.inputFile.getName())) {
                                    cell.setCellValue(fileToCall.get(f.inputFile.getName()));
                                } else {
                                    cell.setCellValue("-");
                                }
                            }
                        }
                    } catch (Exception ex) {
                        return false;
                    }

                }
                CellRangeAddress[] regions = { new CellRangeAddress(0, rowNo, 2, 2 + bothFiles.size()) };
                SheetConditionalFormatting sheetCF = sheet.getSheetConditionalFormatting();

                ConditionalFormattingRule rule1 = sheetCF
                        .createConditionalFormattingRule(ComparisonOperator.EQUAL, "\"AA\"");
                PatternFormatting fill1 = rule1.createPatternFormatting();
                fill1.setFillBackgroundColor(IndexedColors.LIGHT_GREEN.index);
                fill1.setFillPattern(PatternFormatting.SOLID_FOREGROUND);
                ConditionalFormattingRule rule2 = sheetCF
                        .createConditionalFormattingRule(ComparisonOperator.EQUAL, "\"BB\"");
                PatternFormatting fill2 = rule2.createPatternFormatting();
                fill2.setFillBackgroundColor(IndexedColors.PALE_BLUE.index);
                fill2.setFillPattern(PatternFormatting.SOLID_FOREGROUND);
                ConditionalFormattingRule rule3 = sheetCF
                        .createConditionalFormattingRule(ComparisonOperator.EQUAL, "\"AB\"");
                PatternFormatting fill3 = rule3.createPatternFormatting();
                fill3.setFillBackgroundColor(IndexedColors.ROSE.index);
                fill3.setFillPattern(PatternFormatting.SOLID_FOREGROUND);
                sheetCF.addConditionalFormatting(regions, rule3, rule2);
                sheetCF.addConditionalFormatting(regions, rule1);
                wb.write(out);
                updateProgress(total, total);
                out.close();
            } catch (IOException | NumberFormatException ex) {
                ex.printStackTrace();
                return false;
            }
            return true;
        }
    };//end of task

    setProgressMode(true);
    progressBar.progressProperty().bind(writeTask.progressProperty());
    progressMessage.textProperty().bind(writeTask.messageProperty());
    writeTask.setOnSucceeded(new EventHandler<WorkerStateEvent>() {
        @Override
        public void handle(WorkerStateEvent e) {
            if (e.getSource().getValue() == true) {
                Dialogs.showInformationDialog(null,
                        "Saved regions written " + "to file " + "(" + regionFile.getName() + ")successfully",
                        "Regions Written", "SNP Viewer");
            } else {
                Dialogs.showErrorDialog(null, "Region write failed.", "Write Failed", "SNP Viewer");
            }
            setProgressMode(false);
            progressBar.progressProperty().unbind();
            progressBar.progressProperty().set(0);
            progressMessage.textProperty().unbind();
            progressMessage.setText("");
            progressTitle.setText("");

        }

    });
    writeTask.setOnFailed(new EventHandler<WorkerStateEvent>() {
        @Override
        public void handle(WorkerStateEvent e) {
            setProgressMode(false);
            progressBar.progressProperty().unbind();
            progressBar.progressProperty().set(0);
            progressMessage.textProperty().unbind();
            progressMessage.setText("");
            progressTitle.setText("Region write failed!");
            Dialogs.showErrorDialog(null, "Error writing region to file\n", "Region write error", "SNP Viewer",
                    e.getSource().getException());

        }

    });
    writeTask.setOnCancelled(new EventHandler<WorkerStateEvent>() {
        @Override
        public void handle(WorkerStateEvent e) {
            progressMessage.setText("Region write cancelled");
            progressTitle.setText("Cancelled");
            setProgressMode(false);
            progressBar.progressProperty().unbind();
            progressBar.progressProperty().set(0);
            Dialogs.showErrorDialog(null, "Error writing region to file\n", "Region write error", "SNP Viewer");
        }

    });
    cancelButton.setOnAction(new EventHandler<ActionEvent>() {
        @Override
        public void handle(ActionEvent actionEvent) {
            writeTask.cancel();

        }
    });
    progressTitle.setText("Writing regions to .xlsx file");
    new Thread(writeTask).start();
}

From source file:snpviewer.SnpViewer.java

public void writeRegionToFile(final String chromosome, final double start, final double end) {
    /* get coordinates of selection and report back
     * write SNPs in region to file//from ww w . jav  a2s.c  o  m
     */
    FileChooser fileChooser = new FileChooser();
    FileChooser.ExtensionFilter extFilter = new FileChooser.ExtensionFilter("Excel  (*.xlsx)", "*.xlsx");
    fileChooser.getExtensionFilters().add(extFilter);
    fileChooser.setTitle("Write region to Excel file (.xlsx)...");
    File rFile = fileChooser.showSaveDialog(mainWindow);
    if (rFile == null) {
        return;
    } else if (!rFile.getName().endsWith(".xlsx")) {
        rFile = new File(rFile.getAbsolutePath() + ".xlsx");
    }
    final File regionFile = rFile;
    final Task<Boolean> writeTask = new Task() {
        @Override
        protected Boolean call() throws Exception {
            try {

                updateProgress(-1, -1);
                ArrayList<SnpFile> bothFiles = new ArrayList<>();
                bothFiles.addAll(affFiles);
                bothFiles.addAll(unFiles);
                TreeMap<Integer, HashMap<String, String>> coordMap = new TreeMap();
                /*coordmap - key is position, key of hashmap 
                 * is input filename and value call
                 */
                HashMap<Integer, String> coordToId = new HashMap<>();
                double progress = 0;
                double total = bothFiles.size() * 5;
                try {
                    BufferedOutputStream out = new BufferedOutputStream(new FileOutputStream(regionFile));
                    Workbook wb = new XSSFWorkbook();
                    Sheet sheet = wb.createSheet();
                    int rowNo = 0;
                    Row row = sheet.createRow(rowNo++);
                    for (SnpFile f : bothFiles) {
                        if (isCancelled()) {
                            return false;
                        }
                        updateProgress(++progress, total);
                        updateMessage("Reading region in " + f.inputFile.getName());
                        List<SnpFile.SnpLine> lines = f.getSnpsInRegion(chromosome, (int) start, (int) end);
                        for (SnpFile.SnpLine snpLine : lines) {
                            if (isCancelled()) {
                                return false;
                            }
                            Integer coord = snpLine.getPosition();
                            if (!coordMap.containsKey(coord)) {
                                coordMap.put(coord, new HashMap<String, String>());
                            }
                            String filename = f.inputFile.getName();
                            String rsId = snpLine.getId();
                            String call = snpLine.getCall();
                            coordMap.get(coord).put(filename, call);
                            coordToId.put(coord, rsId);
                        }
                    }
                    Cell cell = row.createCell(0);
                    cell.setCellValue(
                            "chr" + chromosome + ":" + coordMap.firstKey() + "-" + coordMap.lastKey());
                    row = sheet.createRow(rowNo++);
                    cell = row.createCell(0);
                    cell.setCellValue(
                            coordToId.get(coordMap.firstKey()) + ";" + coordToId.get(coordMap.lastKey()));
                    row = sheet.createRow(rowNo++);
                    int colNo = 0;
                    cell = row.createCell(colNo++);
                    cell.setCellValue("Position");
                    cell = row.createCell(colNo++);
                    cell.setCellValue("rsID");
                    for (SnpFile f : bothFiles) {
                        cell = row.createCell(colNo++);
                        if (f.getSampleName() != null && f.getSampleName().length() > 0) {
                            cell.setCellValue(f.getSampleName());
                        } else {
                            cell.setCellValue(f.getInputFileName());
                        }
                    }
                    progress = coordMap.size();
                    total = 5 * coordMap.size();
                    updateMessage("Writing region to file...");
                    for (Entry current : coordMap.entrySet()) {
                        if (isCancelled()) {
                            return false;
                        }
                        progress += 4;
                        updateProgress(progress, total);
                        row = sheet.createRow(rowNo++);
                        colNo = 0;
                        Integer coord = (Integer) current.getKey();
                        cell = row.createCell(colNo++);
                        cell.setCellValue(coord);
                        String rsId = coordToId.get(coord);
                        cell = row.createCell(colNo++);
                        cell.setCellValue(rsId);
                        HashMap<String, String> fileToCall = (HashMap<String, String>) current.getValue();
                        for (SnpFile f : bothFiles) {
                            cell = row.createCell(colNo++);
                            if (fileToCall.containsKey(f.inputFile.getName())) {
                                cell.setCellValue(fileToCall.get(f.inputFile.getName()));
                            } else {
                                cell.setCellValue("-");
                            }
                        }
                    }
                    CellRangeAddress[] regions = { new CellRangeAddress(0, rowNo, 2, 2 + bothFiles.size()) };
                    SheetConditionalFormatting sheetCF = sheet.getSheetConditionalFormatting();

                    ConditionalFormattingRule rule1 = sheetCF
                            .createConditionalFormattingRule(ComparisonOperator.EQUAL, "\"AA\"");
                    PatternFormatting fill1 = rule1.createPatternFormatting();
                    fill1.setFillBackgroundColor(IndexedColors.LIGHT_GREEN.index);
                    fill1.setFillPattern(PatternFormatting.SOLID_FOREGROUND);
                    ConditionalFormattingRule rule2 = sheetCF
                            .createConditionalFormattingRule(ComparisonOperator.EQUAL, "\"BB\"");
                    PatternFormatting fill2 = rule2.createPatternFormatting();
                    fill2.setFillBackgroundColor(IndexedColors.PALE_BLUE.index);
                    fill2.setFillPattern(PatternFormatting.SOLID_FOREGROUND);
                    ConditionalFormattingRule rule3 = sheetCF
                            .createConditionalFormattingRule(ComparisonOperator.EQUAL, "\"AB\"");
                    PatternFormatting fill3 = rule3.createPatternFormatting();
                    fill3.setFillBackgroundColor(IndexedColors.ROSE.index);
                    fill3.setFillPattern(PatternFormatting.SOLID_FOREGROUND);
                    sheetCF.addConditionalFormatting(regions, rule3, rule2);
                    sheetCF.addConditionalFormatting(regions, rule1);
                    wb.write(out);
                    out.close();
                    return true;
                } catch (IOException ex) {
                    return false;
                }
            } catch (Exception ex) {
                return false;
            }
        }
    };//end of task

    setProgressMode(true);
    progressBar.progressProperty().bind(writeTask.progressProperty());
    progressMessage.textProperty().bind(writeTask.messageProperty());
    writeTask.setOnSucceeded(new EventHandler<WorkerStateEvent>() {
        @Override
        public void handle(WorkerStateEvent e) {
            if (e.getSource().getValue() == true) {
                Dialogs.showInformationDialog(null,
                        "Region written to file " + "(" + regionFile.getName() + ") successfully",
                        "Region Written", "SNP Viewer");
            } else {
                Dialogs.showErrorDialog(null, "Region write failed.", "Write Failed", "SNP Viewer");
            }
            setProgressMode(false);
            progressBar.progressProperty().unbind();
            progressBar.progressProperty().set(0);
            progressMessage.textProperty().unbind();
            progressMessage.setText("");
            progressTitle.setText("");

        }

    });
    writeTask.setOnFailed(new EventHandler<WorkerStateEvent>() {
        @Override
        public void handle(WorkerStateEvent e) {
            setProgressMode(false);
            progressBar.progressProperty().unbind();
            progressBar.progressProperty().set(0);
            progressMessage.textProperty().unbind();
            progressMessage.setText("");
            progressTitle.setText("Region write failed!");
            Dialogs.showErrorDialog(null, "Error writing region to file\n", "Region write error", "SNP Viewer",
                    e.getSource().getException());

        }

    });
    writeTask.setOnCancelled(new EventHandler<WorkerStateEvent>() {
        @Override
        public void handle(WorkerStateEvent e) {
            progressMessage.setText("Region write cancelled");
            progressTitle.setText("Cancelled");
            setProgressMode(false);
            progressBar.progressProperty().unbind();
            progressBar.progressProperty().set(0);
            Dialogs.showErrorDialog(null, "Error writing region to file\n", "Region write error", "SNP Viewer");
        }

    });
    cancelButton.setOnAction(new EventHandler<ActionEvent>() {
        @Override
        public void handle(ActionEvent actionEvent) {
            writeTask.cancel();

        }
    });
    progressTitle.setText("Writing region to .xlsx file");
    new Thread(writeTask).start();
}

From source file:ca.uhn.fhir.context.BaseRuntimeElementCompositeDefinition.java

@SuppressWarnings("unchecked")
private void scanCompositeElementForChildren(Set<String> elementNames,
        TreeMap<Integer, BaseRuntimeDeclaredChildDefinition> theOrderToElementDef,
        TreeMap<Integer, BaseRuntimeDeclaredChildDefinition> theOrderToExtensionDef) {
    int baseElementOrder = 0;

    for (ScannedField next : myScannedFields) {
        if (next.isFirstFieldInNewClass()) {
            baseElementOrder = theOrderToElementDef.isEmpty() ? 0
                    : theOrderToElementDef.lastEntry().getKey() + 1;
        }/*from  w  w  w  . j  a v a2  s  . c  om*/

        Class<?> declaringClass = next.getField().getDeclaringClass();

        Description descriptionAnnotation = ModelScanner.pullAnnotation(next.getField(), Description.class);

        TreeMap<Integer, BaseRuntimeDeclaredChildDefinition> orderMap = theOrderToElementDef;
        Extension extensionAttr = ModelScanner.pullAnnotation(next.getField(), Extension.class);
        if (extensionAttr != null) {
            orderMap = theOrderToExtensionDef;
        }

        Child childAnnotation = next.getChildAnnotation();
        Field nextField = next.getField();
        String elementName = childAnnotation.name();
        int order = childAnnotation.order();
        boolean childIsChoiceType = false;
        boolean orderIsReplaceParent = false;

        if (order == Child.REPLACE_PARENT) {

            if (extensionAttr != null) {

                for (Entry<Integer, BaseRuntimeDeclaredChildDefinition> nextEntry : orderMap.entrySet()) {
                    BaseRuntimeDeclaredChildDefinition nextDef = nextEntry.getValue();
                    if (nextDef instanceof RuntimeChildDeclaredExtensionDefinition) {
                        if (nextDef.getExtensionUrl().equals(extensionAttr.url())) {
                            orderIsReplaceParent = true;
                            order = nextEntry.getKey();
                            orderMap.remove(nextEntry.getKey());
                            elementNames.remove(elementName);
                            break;
                        }
                    }
                }
                if (order == Child.REPLACE_PARENT) {
                    throw new ConfigurationException("Field " + nextField.getName() + "' on target type "
                            + declaringClass.getSimpleName() + " has order() of REPLACE_PARENT ("
                            + Child.REPLACE_PARENT + ") but no parent element with extension URL "
                            + extensionAttr.url() + " could be found on type "
                            + nextField.getDeclaringClass().getSimpleName());
                }

            } else {

                for (Entry<Integer, BaseRuntimeDeclaredChildDefinition> nextEntry : orderMap.entrySet()) {
                    BaseRuntimeDeclaredChildDefinition nextDef = nextEntry.getValue();
                    if (elementName.equals(nextDef.getElementName())) {
                        orderIsReplaceParent = true;
                        order = nextEntry.getKey();
                        BaseRuntimeDeclaredChildDefinition existing = orderMap.remove(nextEntry.getKey());
                        elementNames.remove(elementName);

                        /*
                         * See #350 - If the original field (in the superclass) with the given name is a choice, then we need to make sure
                         * that the field which replaces is a choice even if it's only a choice of one type - this is because the
                         * element name when serialized still needs to reflect the datatype
                         */
                        if (existing instanceof RuntimeChildChoiceDefinition) {
                            childIsChoiceType = true;
                        }
                        break;
                    }
                }
                if (order == Child.REPLACE_PARENT) {
                    throw new ConfigurationException("Field " + nextField.getName() + "' on target type "
                            + declaringClass.getSimpleName() + " has order() of REPLACE_PARENT ("
                            + Child.REPLACE_PARENT + ") but no parent element with name " + elementName
                            + " could be found on type " + nextField.getDeclaringClass().getSimpleName());
                }

            }

        }

        if (order < 0 && order != Child.ORDER_UNKNOWN) {
            throw new ConfigurationException("Invalid order '" + order + "' on @Child for field '"
                    + nextField.getName() + "' on target type: " + declaringClass);
        }

        if (order != Child.ORDER_UNKNOWN && !orderIsReplaceParent) {
            order = order + baseElementOrder;
        }
        // int min = childAnnotation.min();
        // int max = childAnnotation.max();

        /*
         * Anything that's marked as unknown is given a new ID that is <0 so that it doesn't conflict with any given IDs and can be figured out later
         */
        if (order == Child.ORDER_UNKNOWN) {
            order = Integer.valueOf(0);
            while (orderMap.containsKey(order)) {
                order++;
            }
        }

        List<Class<? extends IBase>> choiceTypes = next.getChoiceTypes();

        if (orderMap.containsKey(order)) {
            throw new ConfigurationException("Detected duplicate field order '" + childAnnotation.order()
                    + "' for element named '" + elementName + "' in type '" + declaringClass.getCanonicalName()
                    + "' - Already had: " + orderMap.get(order).getElementName());
        }

        if (elementNames.contains(elementName)) {
            throw new ConfigurationException("Detected duplicate field name '" + elementName + "' in type '"
                    + declaringClass.getCanonicalName() + "'");
        }

        Class<?> nextElementType = next.getElementType();

        BaseRuntimeDeclaredChildDefinition def;
        if (childAnnotation.name().equals("extension")
                && IBaseExtension.class.isAssignableFrom(nextElementType)) {
            def = new RuntimeChildExtension(nextField, childAnnotation.name(), childAnnotation,
                    descriptionAnnotation);
        } else if (childAnnotation.name().equals("modifierExtension")
                && IBaseExtension.class.isAssignableFrom(nextElementType)) {
            def = new RuntimeChildExtension(nextField, childAnnotation.name(), childAnnotation,
                    descriptionAnnotation);
        } else if (BaseContainedDt.class.isAssignableFrom(nextElementType)
                || (childAnnotation.name().equals("contained")
                        && IBaseResource.class.isAssignableFrom(nextElementType))) {
            /*
             * Child is contained resources
             */
            def = new RuntimeChildContainedResources(nextField, childAnnotation, descriptionAnnotation,
                    elementName);
        } else if (IAnyResource.class.isAssignableFrom(nextElementType)
                || IResource.class.equals(nextElementType)) {
            /*
             * Child is a resource as a direct child, as in Bundle.entry.resource
             */
            def = new RuntimeChildDirectResource(nextField, childAnnotation, descriptionAnnotation,
                    elementName);
        } else {
            childIsChoiceType |= choiceTypes.size() > 1;
            if (childIsChoiceType && !BaseResourceReferenceDt.class.isAssignableFrom(nextElementType)
                    && !IBaseReference.class.isAssignableFrom(nextElementType)) {
                def = new RuntimeChildChoiceDefinition(nextField, elementName, childAnnotation,
                        descriptionAnnotation, choiceTypes);
            } else if (extensionAttr != null) {
                /*
                 * Child is an extension
                 */
                Class<? extends IBase> et = (Class<? extends IBase>) nextElementType;

                Object binder = null;
                if (BoundCodeDt.class.isAssignableFrom(nextElementType)
                        || IBoundCodeableConcept.class.isAssignableFrom(nextElementType)) {
                    binder = ModelScanner.getBoundCodeBinder(nextField);
                }

                def = new RuntimeChildDeclaredExtensionDefinition(nextField, childAnnotation,
                        descriptionAnnotation, extensionAttr, elementName, extensionAttr.url(), et, binder);

                if (IBaseEnumeration.class.isAssignableFrom(nextElementType)) {
                    ((RuntimeChildDeclaredExtensionDefinition) def).setEnumerationType(
                            ReflectionUtil.getGenericCollectionTypeOfFieldWithSecondOrderForList(nextField));
                }
            } else if (BaseResourceReferenceDt.class.isAssignableFrom(nextElementType)
                    || IBaseReference.class.isAssignableFrom(nextElementType)) {
                /*
                 * Child is a resource reference
                 */
                List<Class<? extends IBaseResource>> refTypesList = new ArrayList<Class<? extends IBaseResource>>();
                for (Class<? extends IElement> nextType : childAnnotation.type()) {
                    if (IBaseReference.class.isAssignableFrom(nextType)) {
                        refTypesList.add(myContext.getVersion().getVersion().isRi() ? IAnyResource.class
                                : IResource.class);
                        continue;
                    } else if (IBaseResource.class.isAssignableFrom(nextType) == false) {
                        throw new ConfigurationException("Field '" + nextField.getName() + "' in class '"
                                + nextField.getDeclaringClass().getCanonicalName() + "' is of type "
                                + BaseResourceReferenceDt.class + " but contains a non-resource type: "
                                + nextType.getCanonicalName());
                    }
                    refTypesList.add((Class<? extends IBaseResource>) nextType);
                }
                def = new RuntimeChildResourceDefinition(nextField, elementName, childAnnotation,
                        descriptionAnnotation, refTypesList);

            } else if (IResourceBlock.class.isAssignableFrom(nextElementType)
                    || IBaseBackboneElement.class.isAssignableFrom(nextElementType)
                    || IBaseDatatypeElement.class.isAssignableFrom(nextElementType)) {
                /*
                 * Child is a resource block (i.e. a sub-tag within a resource) TODO: do these have a better name according to HL7?
                 */

                Class<? extends IBase> blockDef = (Class<? extends IBase>) nextElementType;
                def = new RuntimeChildResourceBlockDefinition(myContext, nextField, childAnnotation,
                        descriptionAnnotation, elementName, blockDef);
            } else if (IDatatype.class.equals(nextElementType) || IElement.class.equals(nextElementType)
                    || "Type".equals(nextElementType.getSimpleName())
                    || IBaseDatatype.class.equals(nextElementType)) {

                def = new RuntimeChildAny(nextField, elementName, childAnnotation, descriptionAnnotation);
            } else if (IDatatype.class.isAssignableFrom(nextElementType)
                    || IPrimitiveType.class.isAssignableFrom(nextElementType)
                    || ICompositeType.class.isAssignableFrom(nextElementType)
                    || IBaseDatatype.class.isAssignableFrom(nextElementType)
                    || IBaseExtension.class.isAssignableFrom(nextElementType)) {
                Class<? extends IBase> nextDatatype = (Class<? extends IBase>) nextElementType;

                if (IPrimitiveType.class.isAssignableFrom(nextElementType)) {
                    if (nextElementType.equals(BoundCodeDt.class)) {
                        IValueSetEnumBinder<Enum<?>> binder = ModelScanner.getBoundCodeBinder(nextField);
                        Class<? extends Enum<?>> enumType = ModelScanner
                                .determineEnumTypeForBoundField(nextField);
                        def = new RuntimeChildPrimitiveBoundCodeDatatypeDefinition(nextField, elementName,
                                childAnnotation, descriptionAnnotation, nextDatatype, binder, enumType);
                    } else if (IBaseEnumeration.class.isAssignableFrom(nextElementType)) {
                        Class<? extends Enum<?>> binderType = ModelScanner
                                .determineEnumTypeForBoundField(nextField);
                        def = new RuntimeChildPrimitiveEnumerationDatatypeDefinition(nextField, elementName,
                                childAnnotation, descriptionAnnotation, nextDatatype, binderType);
                    } else {
                        def = new RuntimeChildPrimitiveDatatypeDefinition(nextField, elementName,
                                descriptionAnnotation, childAnnotation, nextDatatype);
                    }
                } else {
                    if (IBoundCodeableConcept.class.isAssignableFrom(nextElementType)) {
                        IValueSetEnumBinder<Enum<?>> binder = ModelScanner.getBoundCodeBinder(nextField);
                        Class<? extends Enum<?>> enumType = ModelScanner
                                .determineEnumTypeForBoundField(nextField);
                        def = new RuntimeChildCompositeBoundDatatypeDefinition(nextField, elementName,
                                childAnnotation, descriptionAnnotation, nextDatatype, binder, enumType);
                    } else if (BaseNarrativeDt.class.isAssignableFrom(nextElementType)
                            || INarrative.class.isAssignableFrom(nextElementType)) {
                        def = new RuntimeChildNarrativeDefinition(nextField, elementName, childAnnotation,
                                descriptionAnnotation, nextDatatype);
                    } else {
                        def = new RuntimeChildCompositeDatatypeDefinition(nextField, elementName,
                                childAnnotation, descriptionAnnotation, nextDatatype);
                    }
                }

            } else {
                throw new ConfigurationException(
                        "Field '" + elementName + "' in type '" + declaringClass.getCanonicalName()
                                + "' is not a valid child type: " + nextElementType);
            }

            Binding bindingAnnotation = ModelScanner.pullAnnotation(nextField, Binding.class);
            if (bindingAnnotation != null) {
                if (isNotBlank(bindingAnnotation.valueSet())) {
                    def.setBindingValueSet(bindingAnnotation.valueSet());
                }
            }

        }

        orderMap.put(order, def);
        elementNames.add(elementName);
    }
}

From source file:org.xwiki.contrib.mailarchive.timeline.internal.TimeLineGenerator.java

/**
 * {@inheritDoc}//from w  ww.  j ava2 s.  c  om
 * 
 * @see org.xwiki.contrib.mailarchive.timeline.ITimeLineGenerator#compute()
 */
@Override
public String compute(int maxItems) {
    try {
        config.reloadConfiguration();
    } catch (MailArchiveException e) {
        logger.error("Could not load mail archive configuration", e);
        return null;
    }
    Map<String, IMailingList> mailingLists = config.getMailingLists();
    Map<String, IType> types = config.getMailTypes();

    try {
        this.userStatsUrl = xwiki.getDocument(docResolver.resolve("MailArchive.ViewUserMessages"), context)
                .getURL("view", context);
    } catch (XWikiException e1) {
        logger.warn("Could not retrieve user stats url {}", ExceptionUtils.getRootCauseMessage(e1));
    }

    TreeMap<Long, TimeLineEvent> sortedEvents = new TreeMap<Long, TimeLineEvent>();

    // Set loading user in context (for rights)
    String loadingUser = config.getLoadingUser();
    context.setUserReference(docResolver.resolve(loadingUser));

    try {
        // Search topics
        String xwql = "select doc.fullName, topic.author, topic.subject, topic.topicid, topic.startdate, topic.lastupdatedate from Document doc, doc.object("
                + XWikiPersistence.CLASS_TOPICS + ") as topic order by topic.lastupdatedate desc";
        List<Object[]> result = queryManager.createQuery(xwql, Query.XWQL).setLimit(maxItems).execute();

        for (Object[] item : result) {
            XWikiDocument doc = null;
            try {
                String docurl = (String) item[0];
                String author = (String) item[1];
                String subject = (String) item[2];
                String topicId = (String) item[3];
                Date date = (Date) item[4];
                Date end = (Date) item[5];

                String action = "";

                // Retrieve associated emails
                TreeMap<Long, TopicEventBubble> emails = getTopicMails(topicId, subject);

                if (emails == null || emails.isEmpty()) {
                    // Invalid topic, not emails attached, do not show it
                    logger.warn("Invalid topic, no emails attached " + doc);
                } else {
                    if (date != null && end != null && date.equals(end)) {
                        // Add 10 min just to see the tape
                        end.setTime(end.getTime() + 600000);
                    }

                    doc = xwiki.getDocument(docResolver.resolve(docurl), context);
                    final List<String> tagsList = doc.getTagsList(context);
                    List<String> topicTypes = doc.getListValue(XWikiPersistence.CLASS_TOPICS, "type");

                    // Email type icon
                    List<String> icons = new ArrayList<String>();
                    for (String topicType : topicTypes) {
                        IType type = types.get(topicType);
                        if (type != null && !StringUtils.isEmpty(type.getIcon())) {
                            icons.add(xwiki.getSkinFile("icons/silk/" + type.getIcon() + ".png", context));
                            // http://localhost:8080/xwiki/skins/colibri/icons/silk/bell
                            // http://localhost:8080/xwiki/resources/icons/silk/bell.png

                        }
                    }

                    // Author and avatar
                    final IMAUser wikiUser = mailUtils.parseUser(author, config.isMatchLdap());
                    final String authorAvatar = getAuthorAvatar(wikiUser.getWikiProfile());

                    final TimeLineEvent timelineEvent = new TimeLineEvent();
                    TimeLineEvent additionalEvent = null;
                    timelineEvent.beginDate = date;
                    timelineEvent.title = subject;
                    timelineEvent.icons = icons;
                    timelineEvent.lists = doc.getListValue(XWikiPersistence.CLASS_TOPICS, "list");
                    timelineEvent.author = wikiUser.getDisplayName();
                    timelineEvent.authorAvatar = authorAvatar;
                    timelineEvent.extract = getExtract(topicId);

                    if (emails.size() == 1) {
                        logger.debug("Adding instant event for email '" + subject + "'");
                        // Unique email, we show a punctual email event
                        timelineEvent.url = emails.firstEntry().getValue().link;
                        timelineEvent.action = "New Email ";

                    } else {
                        // For email with specific type icon, and a duration, both a band and a point should be added (so 2 events)
                        // because no icon is displayed for duration events.
                        if (CollectionUtils.isNotEmpty(icons)) {
                            logger.debug(
                                    "Adding additional instant event to display type icon for first email in topic");
                            additionalEvent = new TimeLineEvent(timelineEvent);
                            additionalEvent.url = emails.firstEntry().getValue().link;
                            additionalEvent.action = "New Email ";
                        }

                        // Email thread, we show a topic event (a range)
                        logger.debug("Adding duration event for topic '" + subject + "'");
                        timelineEvent.endDate = end;
                        timelineEvent.url = doc.getURL("view", context);
                        timelineEvent.action = "New Topic ";
                        timelineEvent.messages = emails;
                    }

                    // Add the generated Event to the list
                    if (sortedEvents.containsKey(date.getTime())) {
                        // Avoid having more than 1 event at exactly the same time, because some timeline don't like it
                        date.setTime(date.getTime() + 1);
                    }
                    sortedEvents.put(date.getTime(), timelineEvent);
                    if (additionalEvent != null) {
                        sortedEvents.put(date.getTime() + 1, additionalEvent);
                    }
                }

            } catch (Throwable t) {
                logger.warn("Exception for " + doc, t);
            }

        }

    } catch (Throwable e) {
        logger.warn("could not compute timeline data", e);
    }

    return printEvents(sortedEvents);

}

From source file:org.xwiki.repository.internal.RepositoryManager.java

public DocumentReference importExtension(String extensionId, ExtensionRepository repository, Type type)
        throws QueryException, XWikiException, ResolveException {
    TreeMap<Version, String> versions = new TreeMap<Version, String>();

    Version lastVersion = getVersions(extensionId, repository, type, versions);

    if (lastVersion == null) {
        throw new ExtensionNotFoundException("Can't find any version for the extension [" + extensionId
                + "] on repository [" + repository + "]");
    } else if (versions.isEmpty()) {
        // If no valid version import the last version
        versions.put(lastVersion, extensionId);
    } else {/*w ww .  jav  a2  s.c om*/
        // Select the last valid version
        lastVersion = versions.lastKey();
    }

    Extension extension = repository.resolve(new ExtensionId(extensionId, lastVersion));

    // Get former ids versions
    Collection<ExtensionId> features = extension.getExtensionFeatures();

    for (ExtensionId feature : features) {
        try {
            getVersions(feature.getId(), repository, type, versions);
        } catch (ResolveException e) {
            // Ignore
        }
    }

    XWikiContext xcontext = this.xcontextProvider.get();

    boolean needSave = false;

    XWikiDocument document = getExistingExtensionDocumentById(extensionId);

    if (document == null) {
        // Create document
        document = xcontext.getWiki().getDocument(new DocumentReference(xcontext.getWikiId(),
                Arrays.asList("Extension", extension.getName()), "WebHome"), xcontext);

        for (int i = 1; !document.isNew(); ++i) {
            document = xcontext.getWiki().getDocument(new DocumentReference(xcontext.getWikiId(),
                    Arrays.asList("Extension", extension.getName() + ' ' + i), "WebHome"), xcontext);
        }

        document.readFromTemplate(
                this.currentResolver.resolve(XWikiRepositoryModel.EXTENSION_TEMPLATEREFERENCE), xcontext);

        needSave = true;
    }

    // Update document

    BaseObject extensionObject = document.getXObject(XWikiRepositoryModel.EXTENSION_CLASSREFERENCE);
    if (extensionObject == null) {
        extensionObject = document.newXObject(XWikiRepositoryModel.EXTENSION_CLASSREFERENCE, xcontext);
        needSave = true;
    }

    if (!StringUtils.equals(extensionId,
            getValue(extensionObject, XWikiRepositoryModel.PROP_EXTENSION_ID, (String) null))) {
        extensionObject.set(XWikiRepositoryModel.PROP_EXTENSION_ID, extensionId, xcontext);
        needSave = true;
    }

    // Update extension informations

    needSave |= updateExtension(extension, extensionObject, xcontext);

    // Proxy marker

    BaseObject extensionProxyObject = document.getXObject(XWikiRepositoryModel.EXTENSIONPROXY_CLASSREFERENCE);
    if (extensionProxyObject == null) {
        extensionProxyObject = document.newXObject(XWikiRepositoryModel.EXTENSIONPROXY_CLASSREFERENCE,
                xcontext);
        extensionProxyObject.setIntValue(XWikiRepositoryModel.PROP_PROXY_AUTOUPDATE, 1);
        needSave = true;
    }

    needSave |= update(extensionProxyObject, XWikiRepositoryModel.PROP_PROXY_REPOSITORYID,
            repository.getDescriptor().getId());
    needSave |= update(extensionProxyObject, XWikiRepositoryModel.PROP_PROXY_REPOSITORYTYPE,
            repository.getDescriptor().getType());
    needSave |= update(extensionProxyObject, XWikiRepositoryModel.PROP_PROXY_REPOSITORYURI,
            repository.getDescriptor().getURI().toString());

    // Remove unexisting versions

    Set<String> validVersions = new HashSet<String>();

    List<BaseObject> versionObjects = document
            .getXObjects(XWikiRepositoryModel.EXTENSIONVERSION_CLASSREFERENCE);
    if (versionObjects != null) {
        for (BaseObject versionObject : versionObjects) {
            if (versionObject != null) {
                String version = getValue(versionObject, XWikiRepositoryModel.PROP_VERSION_VERSION);

                if (StringUtils.isBlank(version) || (isVersionProxyingEnabled(document)
                        && !new DefaultVersion(version).equals(extension.getId().getVersion()))) {
                    // Empty version OR old versions should be proxied
                    document.removeXObject(versionObject);
                    needSave = true;
                } else {
                    if (!versions.containsKey(new DefaultVersion(version))) {
                        // The version does not exist on remote repository
                        if (!isVersionValid(document, versionObject, xcontext)) {
                            // The version is invalid, removing it to not make the whole extension invalid
                            document.removeXObject(versionObject);
                            needSave = true;
                        } else {
                            // The version is valid, lets keep it
                            validVersions.add(version);
                        }
                    } else {
                        // This version exist on remote repository
                        validVersions.add(version);
                    }
                }
            }
        }
    }
    List<BaseObject> dependencyObjects = document
            .getXObjects(XWikiRepositoryModel.EXTENSIONDEPENDENCY_CLASSREFERENCE);
    if (dependencyObjects != null) {
        for (BaseObject dependencyObject : dependencyObjects) {
            if (dependencyObject != null) {
                String version = getValue(dependencyObject,
                        XWikiRepositoryModel.PROP_DEPENDENCY_EXTENSIONVERSION);

                if (!validVersions.contains(version)) {
                    // The version is invalid, removing it to not make the whole extension invalid
                    document.removeXObject(dependencyObject);
                    needSave = true;
                }
            }
        }
    }

    // Update versions

    for (Map.Entry<Version, String> entry : versions.entrySet()) {
        Version version = entry.getKey();
        String id = entry.getValue();

        try {
            Extension versionExtension;
            if (version.equals(extension.getId().getVersion())) {
                versionExtension = extension;
            } else if (isVersionProxyingEnabled(document)) {
                continue;
            } else {
                versionExtension = repository.resolve(new ExtensionId(id, version));
            }

            // Update version related informations
            needSave |= updateExtensionVersion(document, versionExtension);
        } catch (Exception e) {
            this.logger.error("Failed to resolve extension with id [" + id + "] and version [" + version
                    + "] on repository [" + repository + "]", e);
        }
    }

    if (needSave) {
        document.setAuthorReference(xcontext.getUserReference());
        if (document.isNew()) {
            document.setContentAuthorReference(xcontext.getUserReference());
            document.setCreatorReference(xcontext.getUserReference());
        }

        xcontext.getWiki().saveDocument(document,
                "Imported extension [" + extensionId + "] from repository [" + repository.getDescriptor() + "]",
                true, xcontext);
    }

    return document.getDocumentReference();
}

From source file:fr.cirad.mgdb.exporting.individualoriented.PLinkExportHandler.java

@Override
public void exportData(OutputStream outputStream, String sModule, Collection<File> individualExportFiles,
        boolean fDeleteSampleExportFilesOnExit, ProgressIndicator progress, DBCursor markerCursor,
        Map<Comparable, Comparable> markerSynonyms, Map<String, InputStream> readyToExportFiles)
        throws Exception {
    File warningFile = File.createTempFile("export_warnings_", "");
    FileWriter warningFileWriter = new FileWriter(warningFile);

    ZipOutputStream zos = new ZipOutputStream(outputStream);

    if (readyToExportFiles != null)
        for (String readyToExportFile : readyToExportFiles.keySet()) {
            zos.putNextEntry(new ZipEntry(readyToExportFile));
            InputStream inputStream = readyToExportFiles.get(readyToExportFile);
            byte[] dataBlock = new byte[1024];
            int count = inputStream.read(dataBlock, 0, 1024);
            while (count != -1) {
                zos.write(dataBlock, 0, count);
                count = inputStream.read(dataBlock, 0, 1024);
            }/*from   w w  w.j  a va  2  s  .  com*/
        }

    MongoTemplate mongoTemplate = MongoTemplateManager.get(sModule);
    int markerCount = markerCursor.count();

    String exportName = sModule + "_" + markerCount + "variants_" + individualExportFiles.size()
            + "individuals";
    zos.putNextEntry(new ZipEntry(exportName + ".ped"));

    TreeMap<Integer, Comparable> problematicMarkerIndexToNameMap = new TreeMap<Integer, Comparable>();
    short nProgress = 0, nPreviousProgress = 0;
    int i = 0;
    for (File f : individualExportFiles) {
        BufferedReader in = new BufferedReader(new FileReader(f));
        try {
            String individualId, line = in.readLine(); // read sample id
            if (line != null) {
                individualId = line;
                String population = getIndividualPopulation(sModule, line);
                String individualInfo = (population == null ? "." : population) + " " + individualId;
                zos.write((individualInfo + " 0 0 0 " + getIndividualGenderCode(sModule, individualId))
                        .getBytes());
            } else
                throw new Exception("Unable to read first line of temp export file " + f.getName());

            int nMarkerIndex = 0;
            while ((line = in.readLine()) != null) {
                List<String> genotypes = MgdbDao.split(line, "|");
                HashMap<Object, Integer> genotypeCounts = new HashMap<Object, Integer>(); // will help us to keep track of missing genotypes
                int highestGenotypeCount = 0;
                String mostFrequentGenotype = null;
                for (String genotype : genotypes) {
                    if (genotype.length() == 0)
                        continue; /* skip missing genotypes */

                    int gtCount = 1 + MgdbDao.getCountForKey(genotypeCounts, genotype);
                    if (gtCount > highestGenotypeCount) {
                        highestGenotypeCount = gtCount;
                        mostFrequentGenotype = genotype;
                    }
                    genotypeCounts.put(genotype, gtCount);
                }

                if (genotypeCounts.size() > 1) {
                    warningFileWriter.write("- Dissimilar genotypes found for variant " + nMarkerIndex
                            + ", individual " + individualId + ". Exporting most frequent: "
                            + mostFrequentGenotype + "\n");
                    problematicMarkerIndexToNameMap.put(nMarkerIndex, "");
                }

                String[] alleles = mostFrequentGenotype == null ? new String[0]
                        : mostFrequentGenotype.split(" ");
                if (alleles.length > 2) {
                    warningFileWriter.write("- More than 2 alleles found for variant " + nMarkerIndex
                            + ", individual " + individualId + ". Exporting only the first 2 alleles.\n");
                    problematicMarkerIndexToNameMap.put(nMarkerIndex, "");
                }

                String all1 = alleles.length == 0 ? "0" : alleles[0];
                String all2 = alleles.length == 0 ? "0" : alleles[alleles.length == 1 ? 0 : 1];
                if (all1.length() != 1 || all2.length() != 1) {
                    warningFileWriter
                            .write("- SNP expected, but alleles are not coded on a single char for variant "
                                    + nMarkerIndex + ", individual " + individualId
                                    + ". Ignoring this genotype.\n");
                    problematicMarkerIndexToNameMap.put(nMarkerIndex, "");
                } else
                    zos.write((" " + all1 + " " + all2).getBytes());

                nMarkerIndex++;
            }
        } catch (Exception e) {
            LOG.error("Error exporting data", e);
            progress.setError("Error exporting data: " + e.getClass().getSimpleName()
                    + (e.getMessage() != null ? " - " + e.getMessage() : ""));
            return;
        } finally {
            in.close();
        }

        if (progress.hasAborted())
            return;

        nProgress = (short) (++i * 100 / individualExportFiles.size());
        if (nProgress > nPreviousProgress) {
            progress.setCurrentStepProgress(nProgress);
            nPreviousProgress = nProgress;
        }
        zos.write('\n');

        if (!f.delete()) {
            f.deleteOnExit();
            LOG.info("Unable to delete tmp export file " + f.getAbsolutePath());
        }
    }
    warningFileWriter.close();

    zos.putNextEntry(new ZipEntry(exportName + ".map"));

    int avgObjSize = (Integer) mongoTemplate
            .getCollection(mongoTemplate.getCollectionName(VariantRunData.class)).getStats().get("avgObjSize");
    int nChunkSize = nMaxChunkSizeInMb * 1024 * 1024 / avgObjSize;

    markerCursor.batchSize(nChunkSize);
    int nMarkerIndex = 0;
    while (markerCursor.hasNext()) {
        DBObject exportVariant = markerCursor.next();
        DBObject refPos = (DBObject) exportVariant.get(VariantData.FIELDNAME_REFERENCE_POSITION);
        Comparable markerId = (Comparable) exportVariant.get("_id");
        String chrom = (String) refPos.get(ReferencePosition.FIELDNAME_SEQUENCE);
        Long pos = ((Number) refPos.get(ReferencePosition.FIELDNAME_START_SITE)).longValue();

        if (chrom == null)
            LOG.warn("Chromosomal position not found for marker " + markerId);
        Comparable exportedId = markerSynonyms == null ? markerId : markerSynonyms.get(markerId);
        zos.write(((chrom == null ? "0" : chrom) + " " + exportedId + " " + 0 + " " + (pos == null ? 0 : pos)
                + LINE_SEPARATOR).getBytes());

        if (problematicMarkerIndexToNameMap.containsKey(nMarkerIndex)) { // we are going to need this marker's name for the warning file
            Comparable variantName = markerId;
            if (markerSynonyms != null) {
                Comparable syn = markerSynonyms.get(markerId);
                if (syn != null)
                    variantName = syn;
            }
            problematicMarkerIndexToNameMap.put(nMarkerIndex, variantName);
        }
        nMarkerIndex++;
    }

    if (warningFile.length() > 0) {
        zos.putNextEntry(new ZipEntry(exportName + "-REMARKS.txt"));
        int nWarningCount = 0;
        BufferedReader in = new BufferedReader(new FileReader(warningFile));
        String sLine;
        while ((sLine = in.readLine()) != null) {
            for (Integer aMarkerIndex : problematicMarkerIndexToNameMap.keySet())
                sLine = sLine.replaceAll("__" + aMarkerIndex + "__",
                        problematicMarkerIndexToNameMap.get(aMarkerIndex).toString());
            zos.write((sLine + "\n").getBytes());
            in.readLine();
            nWarningCount++;
        }
        LOG.info("Number of Warnings for export (" + exportName + "): " + nWarningCount);
        in.close();
    }
    warningFile.delete();

    zos.close();
    progress.setCurrentStepProgress((short) 100);
}

From source file:net.spfbl.spf.SPF.java

/**
 * Processa a consulta e retorna o resultado.
 *
 * @param query a expresso da consulta./*from  w w  w . ja va2s  .  com*/
 * @return o resultado do processamento.
 */
protected static String processSPF(InetAddress ipAddress, Client client, User user, String query,
        LinkedList<User> userList) {
    try {
        String result = "";
        if (query.length() == 0) {
            return "INVALID QUERY\n";
        } else {
            String origin;
            if (client == null) {
                origin = ipAddress.getHostAddress();
            } else if (client.hasEmail()) {
                origin = ipAddress.getHostAddress() + " " + client.getDomain() + " " + client.getEmail();
            } else {
                origin = ipAddress.getHostAddress() + " " + client.getDomain();
            }
            StringTokenizer tokenizer = new StringTokenizer(query, " ");
            String firstToken = tokenizer.nextToken();
            if (firstToken.equals("SPAM") && tokenizer.countTokens() == 1) {
                String ticket = tokenizer.nextToken();
                TreeSet<String> tokenSet = addComplainURLSafe(origin, ticket, null);
                if (tokenSet == null) {
                    result = "DUPLICATE COMPLAIN\n";
                } else {
                    String userEmail;
                    try {
                        userEmail = SPF.getClientURLSafe(ticket);
                    } catch (Exception ex) {
                        userEmail = client == null ? null : client.getEmail();
                    }
                    user = User.get(userEmail);
                    if (user != null) {
                        userList.add(user);
                    }
                    String recipient;
                    try {
                        recipient = SPF.getRecipientURLSafe(ticket);
                    } catch (ProcessException ex) {
                        recipient = null;
                    }
                    result = "OK " + tokenSet + (recipient == null ? "" : " >" + recipient) + "\n";
                }
            } else if (firstToken.equals("ABUSE") && tokenizer.hasMoreTokens()) {
                String token = tokenizer.nextToken();
                if (token.startsWith("In-Reply-To:") && tokenizer.countTokens() == 1) {
                    token = tokenizer.nextToken();
                    if (token.startsWith("From:")) {
                        int index = token.indexOf(':') + 1;
                        String recipient = token.substring(index);
                        User recipientUser = User.get(recipient);
                        if (recipientUser == null) {
                            // Se a consulta originar de destinatrio com postmaster cadastrado,
                            // considerar o prprio postmaster como usurio da consulta.
                            index = recipient.indexOf('@');
                            String postmaster = "postmaster" + recipient.substring(index);
                            User postmasterUser = User.get(postmaster);
                            if (postmasterUser != null) {
                                userList.add(user = postmasterUser);
                            }
                        } else {
                            userList.add(user = recipientUser);
                        }
                        index = query.indexOf(':') + 1;
                        String messageID = query.substring(index);
                        result = "INVALID ID\n";
                        index = messageID.indexOf('<');
                        if (index >= 0) {
                            messageID = messageID.substring(index + 1);
                            index = messageID.indexOf('>');
                            if (index > 0) {
                                messageID = messageID.substring(0, index);
                                result = user.blockByMessageID(messageID) + '\n';
                            }
                        }
                    } else {
                        result = "INVALID FROM\n";
                    }
                } else {
                    result = "INVALID COMMAND\n";
                }
            } else if (firstToken.equals("HOLDING") && tokenizer.countTokens() == 1) {
                String ticket = tokenizer.nextToken();
                result = getHoldStatus(client, ticket, userList) + '\n';
            } else if (firstToken.equals("LINK") && tokenizer.hasMoreTokens()) {
                String ticketSet = tokenizer.nextToken();
                TreeSet<String> linkSet = new TreeSet<String>();
                while (tokenizer.hasMoreTokens()) {
                    linkSet.add(tokenizer.nextToken());
                }
                StringTokenizer tokenizerTicket = new StringTokenizer(ticketSet, ";");
                String unblockURL = null;
                boolean blocked = false;
                Action action = null;
                while (tokenizerTicket.hasMoreTokens()) {
                    String ticket = tokenizerTicket.nextToken();
                    String userEmail;
                    try {
                        userEmail = SPF.getClientURLSafe(ticket);
                    } catch (Exception ex) {
                        userEmail = client == null ? null : client.getEmail();
                    }
                    if ((user = User.get(userEmail)) != null) {
                        userList.add(user);
                        long dateTicket = SPF.getDateTicket(ticket);
                        User.Query queryTicket = user.getQuery(dateTicket);
                        if (queryTicket != null) {
                            if (queryTicket.setLinkSet(linkSet)) {
                                SPF.setSpam(dateTicket, queryTicket.getTokenSet());
                                if (!queryTicket.isWhite() && queryTicket.blockSender(dateTicket)) {
                                    Server.logDebug(
                                            "new BLOCK '" + queryTicket.getBlockSender() + "' added by LINK.");
                                }
                                action = client == null ? Action.REJECT : client.getActionBLOCK();
                                unblockURL = queryTicket.getUnblockURL();
                                blocked = true;
                            } else if (queryTicket.isAnyLinkRED()) {
                                action = client == null ? Action.FLAG : client.getActionRED();
                            }
                            if (action == Action.HOLD) {
                                queryTicket.setResult("HOLD");
                            } else if (action == Action.FLAG) {
                                queryTicket.setResult("FLAG");
                            } else if (action == Action.REJECT) {
                                queryTicket.setResult("REJECT");
                            }
                            User.storeDB(dateTicket, queryTicket);
                        }
                    }
                }
                if (unblockURL != null) {
                    result = "BLOCKED " + unblockURL + "\n";
                } else if (blocked) {
                    result = "BLOCKED\n";
                } else if (action == Action.HOLD) {
                    result = "HOLD\n";
                } else if (action == Action.FLAG) {
                    result = "FLAG\n";
                } else if (action == Action.REJECT) {
                    result = "REJECT\n";
                } else {
                    result = "CLEAR\n";
                }
            } else if (firstToken.equals("MALWARE") && tokenizer.hasMoreTokens()) {
                String ticketSet = tokenizer.nextToken();
                StringBuilder nameBuilder = new StringBuilder();
                while (tokenizer.hasMoreTokens()) {
                    if (nameBuilder.length() > 0) {
                        nameBuilder.append(' ');
                    }
                    nameBuilder.append(tokenizer.nextToken());
                }
                StringBuilder resultBuilder = new StringBuilder();
                StringTokenizer ticketTokenizer = new StringTokenizer(ticketSet, ";");
                while (ticketTokenizer.hasMoreTokens()) {
                    String ticket = ticketTokenizer.nextToken();
                    TreeSet<String> tokenSet = addComplainURLSafe(origin, ticket, "MALWARE");
                    if (tokenSet == null) {
                        resultBuilder.append("DUPLICATE COMPLAIN\n");
                    } else {
                        // Processar reclamao.
                        String userEmail;
                        try {
                            userEmail = SPF.getClientURLSafe(ticket);
                        } catch (Exception ex) {
                            userEmail = client == null ? null : client.getEmail();
                        }
                        user = User.get(userEmail);
                        if (user != null) {
                            userList.add(user);
                            long dateTicket = getDateTicket(ticket);
                            User.Query userQuery = user.getQuery(dateTicket);
                            if (userQuery != null && userQuery.setMalware(nameBuilder.toString())) {
                                User.storeDB(dateTicket, userQuery);
                            }
                        }
                        String recipient;
                        try {
                            recipient = SPF.getRecipientURLSafe(ticket);
                        } catch (ProcessException ex) {
                            recipient = null;
                        }
                        // Bloquear automaticamente todos
                        // os tokens com reputao amarela ou vermelha.
                        // Processar reclamao.
                        for (String token : tokenSet) {
                            String block;
                            Status status = SPF.getStatus(token);
                            if (status == Status.RED && (block = Block.add(token)) != null) {
                                Server.logDebug(
                                        "new BLOCK '" + block + "' added by '" + recipient + ";MALWARE'.");
                                Peer.sendBlockToAll(block);
                            }
                            if (status != Status.GREEN && !Subnet.isValidIP(token)
                                    && (block = Block.addIfNotNull(user, token)) != null) {
                                Server.logDebug(
                                        "new BLOCK '" + block + "' added by '" + recipient + ";MALWARE'.");
                            }
                        }
                        resultBuilder.append("OK ");
                        resultBuilder.append(tokenSet);
                        resultBuilder.append(recipient == null ? "" : " >" + recipient);
                        resultBuilder.append("\n");
                    }
                }
                result = resultBuilder.toString();
            } else if (firstToken.equals("HEADER") && tokenizer.hasMoreTokens()) {
                String ticketSet = tokenizer.nextToken();
                String key = null;
                String from = null;
                String replyto = null;
                String messageID = null;
                String unsubscribe = null;
                String subject = null;
                while (tokenizer.hasMoreTokens()) {
                    String token = tokenizer.nextToken();
                    if (token.startsWith("From:")) {
                        key = "From";
                        int index = token.indexOf(':');
                        from = token.substring(index + 1);
                    } else if (token.startsWith("ReplyTo:") || token.startsWith("Reply-To:")) {
                        key = "Reply-To";
                        int index = token.indexOf(':');
                        replyto = token.substring(index + 1);
                    } else if (token.startsWith("Message-ID:")) {
                        key = "Message-ID";
                        int index = token.indexOf(':');
                        messageID = token.substring(index + 1);
                    } else if (token.startsWith("List-Unsubscribe:")) {
                        key = "List-Unsubscribe";
                        int index = token.indexOf(':');
                        unsubscribe = token.substring(index + 1);
                    } else if (token.startsWith("Subject:")) {
                        key = "Subject";
                        int index = token.indexOf(':');
                        subject = token.substring(index + 1);
                    } else if (key == null) {
                        from = null;
                        replyto = null;
                        unsubscribe = null;
                        subject = null;
                        break;
                    } else if (key.equals("From")) {
                        from += ' ' + token;
                    } else if (key.equals("Reply-To")) {
                        replyto += ' ' + token;
                    } else if (key.equals("Message-ID")) {
                        messageID += ' ' + token;
                    } else if (key.equals("List-Unsubscribe")) {
                        unsubscribe += ' ' + token;
                    } else if (key.equals("Subject")) {
                        subject += ' ' + token;
                    }
                }
                if ((from == null || from.length() == 0) && (replyto == null || replyto.length() == 0)
                        && (messageID == null || messageID.length() == 0)
                        && (unsubscribe == null || unsubscribe.length() == 0)
                        && (subject == null || subject.length() == 0)) {
                    result = "INVALID COMMAND\n";
                } else {
                    boolean whitelisted = false;
                    boolean blocklisted = false;
                    TreeSet<String> unblockURLSet = new TreeSet<String>();
                    StringTokenizer ticketRokenizer = new StringTokenizer(ticketSet, ";");
                    int n = ticketRokenizer.countTokens();
                    ArrayList<User.Query> queryList = new ArrayList<User.Query>(n);
                    while (ticketRokenizer.hasMoreTokens()) {
                        String ticket = ticketRokenizer.nextToken();
                        String userEmail;
                        try {
                            userEmail = SPF.getClientURLSafe(ticket);
                        } catch (Exception ex) {
                            userEmail = client == null ? null : client.getEmail();
                        }
                        if ((user = User.get(userEmail)) != null) {
                            userList.add(user);
                            long dateTicket = SPF.getDateTicket(ticket);
                            User.Query queryTicket = user.getQuery(dateTicket);
                            if (queryTicket != null) {
                                queryList.add(queryTicket);
                                String resultLocal = queryTicket.setHeader(from, replyto, subject, messageID,
                                        unsubscribe);
                                if ("WHITE".equals(resultLocal)) {
                                    whitelisted = true;
                                } else if ("BLOCK".equals(resultLocal)) {
                                    blocklisted = true;
                                    String url = queryTicket.getUnblockURL();
                                    if (url != null) {
                                        unblockURLSet.add(url);
                                    }
                                }
                                User.storeDB(dateTicket, queryTicket);
                            }
                        }
                    }
                    if (whitelisted) {
                        for (User.Query queryTicket : queryList) {
                            queryTicket.setResult("WHITE");
                        }
                        result = "WHITE\n";
                    } else if (blocklisted) {
                        for (User.Query queryTicket : queryList) {
                            queryTicket.setResult("BLOCK");
                        }
                        if (unblockURLSet.size() == 1) {
                            result = "BLOCKED " + unblockURLSet.first() + "\n";
                        } else {
                            result = "BLOCKED\n";
                        }
                    } else {
                        result = "CLEAR\n";
                    }
                }
            } else if (firstToken.equals("HAM") && tokenizer.countTokens() == 1) {
                String ticket = tokenizer.nextToken();
                TreeSet<String> tokenSet = deleteComplainURLSafe(origin, ticket);
                if (tokenSet == null) {
                    result = "ALREADY REMOVED\n";
                } else {
                    String recipient;
                    try {
                        recipient = SPF.getRecipientURLSafe(ticket);
                    } catch (ProcessException ex) {
                        recipient = null;
                    }
                    result = "OK " + tokenSet + (recipient == null ? "" : " >" + recipient) + "\n";
                }
            } else if (firstToken.equals("REFRESH") && tokenizer.countTokens() == 1) {
                String address = tokenizer.nextToken();
                try {
                    if (CacheSPF.refresh(address, true)) {
                        result = "UPDATED\n";
                    } else {
                        result = "NOT LOADED\n";
                    }
                } catch (ProcessException ex) {
                    result = ex.getMessage() + "\n";
                }
            } else if ((firstToken.equals("SPF") && tokenizer.countTokens() >= 4)
                    || tokenizer.countTokens() == 2 || tokenizer.countTokens() == 1
                    || (firstToken.equals("CHECK") && tokenizer.countTokens() == 4)
                    || (firstToken.equals("CHECK") && tokenizer.countTokens() == 3)
                    || (firstToken.equals("CHECK") && tokenizer.countTokens() == 2)) {
                try {
                    String ip;
                    String sender;
                    String helo;
                    String recipient;
                    String origem;
                    String fluxo;
                    if (firstToken.equals("SPF")) {
                        // Nova formatao de consulta.
                        ip = tokenizer.nextToken();
                        sender = tokenizer.nextToken();
                        while (!sender.endsWith("'") && tokenizer.hasMoreTokens()) {
                            sender += " " + tokenizer.nextToken();
                        }
                        helo = tokenizer.hasMoreTokens() ? tokenizer.nextToken() : "''";
                        recipient = tokenizer.hasMoreTokens() ? tokenizer.nextToken() : "''";
                        ip = ip.substring(1, ip.length() - 1);
                        sender = sender.substring(1, sender.length() - 1);
                        helo = helo.substring(1, helo.length() - 1);
                        if (recipient.equals("'")) {
                            recipient = tokenizer.hasMoreTokens() ? tokenizer.nextToken() : "";
                            if (recipient.endsWith("'")) {
                                recipient = recipient.substring(0, recipient.length() - 1);
                            }
                        } else {
                            recipient = recipient.substring(1, recipient.length() - 1);
                        }
                        if (sender.length() == 0) {
                            sender = null;
                        } else {
                            sender = sender.toLowerCase();
                        }
                        recipient = recipient.toLowerCase();
                        recipient = recipient.replace("\"", "");
                    } else if (firstToken.equals("CHECK") && tokenizer.countTokens() == 4) {
                        ip = tokenizer.nextToken().toLowerCase();
                        sender = tokenizer.nextToken().toLowerCase();
                        helo = tokenizer.nextToken();
                        recipient = tokenizer.nextToken().toLowerCase();
                        if (ip.startsWith("'") && ip.endsWith("'")) {
                            ip = ip.substring(1, ip.length() - 1);
                        }
                        if (sender.startsWith("'") && sender.endsWith("'")) {
                            sender = sender.substring(1, sender.length() - 1);
                        }
                        if (helo.startsWith("'") && helo.endsWith("'")) {
                            helo = helo.substring(1, helo.length() - 1);
                        }
                        if (recipient.startsWith("'") && recipient.endsWith("'")) {
                            recipient = recipient.substring(1, recipient.length() - 1);
                        }
                        if (ip.length() == 0) {
                            ip = null;
                        }
                        if (sender.length() == 0) {
                            sender = null;
                        }
                        if (!Domain.isHostname(helo)) {
                            helo = null;
                        }
                        if (recipient.length() == 0) {
                            recipient = null;
                        } else {
                            recipient = recipient.toLowerCase();
                        }
                    } else {
                        // Manter compatibilidade da verso antiga.
                        // Verso obsoleta.
                        if (firstToken.equals("CHECK")) {
                            ip = tokenizer.nextToken();
                        } else {
                            ip = firstToken;
                        }
                        if (tokenizer.countTokens() == 2) {
                            sender = tokenizer.nextToken().toLowerCase();
                            helo = tokenizer.nextToken();
                        } else {
                            sender = null;
                            helo = tokenizer.nextToken();
                        }
                        recipient = null;
                        if (ip.startsWith("'") && ip.endsWith("'")) {
                            ip = ip.substring(1, ip.length() - 1);
                        }
                        if (sender != null && sender.startsWith("'") && sender.endsWith("'")) {
                            sender = sender.substring(1, sender.length() - 1);
                            if (sender.length() == 0) {
                                sender = null;
                            }
                        }
                        if (helo.startsWith("'") && helo.endsWith("'")) {
                            helo = helo.substring(1, helo.length() - 1);
                        }
                    }
                    if (!Subnet.isValidIP(ip)) {
                        return "INVALID\n";
                    } else if (sender != null && !Domain.isEmail(sender)) {
                        return "INVALID\n";
                    } else if (recipient != null && !Domain.isEmail(recipient)) {
                        return "INVALID\n";
                    } else if (Subnet.isReservedIP(ip)) {
                        // Message from LAN.
                        return "LAN\n";
                    } else if (client != null && client.containsFull(ip)) {
                        // Message from LAN.
                        return "LAN\n";
                    } else {
                        TreeSet<String> tokenSet = new TreeSet<String>();
                        ip = Subnet.normalizeIP(ip);
                        tokenSet.add(ip);
                        if (Domain.isValidEmail(recipient)) {
                            // Se houver um remetente vlido,
                            // Adicionar no ticket para controle.
                            tokenSet.add('>' + recipient);
                        }
                        if (recipient != null) {
                            User recipientUser = User.get(recipient);
                            if (recipientUser == null) {
                                // Se a consulta originar de destinatrio com postmaster cadastrado,
                                // considerar o prprio postmaster como usurio da consulta.
                                int index = recipient.indexOf('@');
                                String postmaster = "postmaster" + recipient.substring(index);
                                User postmasterUser = User.get(postmaster);
                                if (postmasterUser != null) {
                                    user = postmasterUser;
                                }
                            } else {
                                user = recipientUser;
                            }
                        }
                        if (user != null) {
                            userList.add(user);
                            tokenSet.add(user.getEmail() + ':');
                        } else if (client != null && client.hasEmail()) {
                            tokenSet.add(client.getEmail() + ':');
                        }
                        // Passar a acompanhar todos os 
                        // HELO quando apontados para o IP para 
                        // uma nova forma de interpretar dados.
                        String hostname;
                        if (CacheHELO.match(ip, helo, false)) {
                            hostname = Domain.normalizeHostname(helo, true);
                        } else {
                            hostname = Reverse.getHostname(ip);
                            hostname = Domain.normalizeHostname(hostname, true);
                        }
                        if (hostname == null) {
                            Server.logDebug("no rDNS for " + ip + ".");
                        } else if (Domain.isOfficialTLD(hostname)) {
                            return "INVALID\n";
                        } else {
                            // Verificao de pilha dupla,
                            // para pontuao em ambas pilhas.
                            String ipv4 = CacheHELO.getUniqueIPv4(hostname);
                            String ipv6 = CacheHELO.getUniqueIPv6(hostname);
                            if (ip.equals(ipv6) && CacheHELO.match(ipv4, hostname, false)) {
                                // Equivalncia de pilha dupla se 
                                // IPv4 for nico para o hostname.
                                tokenSet.add(ipv4);
                            } else if (ip.equals(ipv4) && CacheHELO.match(ipv6, hostname, false)) {
                                // Equivalncia de pilha dupla se 
                                // IPv6 for nico para o hostname.
                                tokenSet.add(ipv6);
                            }
                        }
                        if (Generic.containsGenericSoft(hostname)) {
                            // Quando o reverso for 
                            // genrico, no consider-lo.
                            hostname = null;
                        } else if (hostname != null) {
                            tokenSet.add(hostname);
                        }
                        LinkedList<String> logList = null;
                        if (sender != null && firstToken.equals("CHECK")) {
                            int index = sender.lastIndexOf('@');
                            String domain = sender.substring(index + 1);
                            logList = new LinkedList<String>();
                            try {
                                CacheSPF.refresh(domain, false);
                            } catch (ProcessException ex) {
                                logList.add("Cannot refresh SPF registry: " + ex.getErrorMessage());
                                logList.add("Using cached SPF registry.");
                            }
                        }
                        SPF spf;
                        if (sender == null) {
                            spf = null;
                            result = "NONE";
                        } else if (Domain.isOfficialTLD(sender)) {
                            spf = null;
                            result = "NONE";
                        } else if (Generic.containsGeneric(sender)) {
                            spf = null;
                            result = "NONE";
                        } else if ((spf = CacheSPF.get(sender)) == null) {
                            result = "NONE";
                        } else if (spf.isInexistent()) {
                            result = "NONE";
                        } else {
                            result = spf.getResult(ip, sender, helo, logList);
                        }
                        String mx = Domain.extractHost(sender, true);
                        if (user != null && user.isLocal()) {
                            // Message from local user.
                            return "LAN\n";
                        } else if (recipient != null && result.equals("PASS")) {
                            if (recipient.endsWith(mx)) {
                                // Message from same domain.
                                return "LAN\n";
                            } else if (recipient.equals(Core.getAbuseEmail())
                                    && User.exists(sender, "postmaster" + mx)) {
                                // Message to abuse.
                                return "LAN\n";
                            }
                        }
                        if (result.equals("PASS") || (sender != null && Provider.containsHELO(ip, hostname))) {
                            // Quando fo PASS, significa que o domnio
                            // autorizou envio pelo IP, portanto o dono dele
                            //  responsavel pelas mensagens.
                            if (!Provider.containsExact(mx)) {
                                // No  um provedor ento
                                // o MX deve ser listado.
                                tokenSet.add(mx);
                                origem = mx;
                            } else if (Domain.isValidEmail(sender)) {
                                // Listar apenas o remetente se o
                                // hostname for um provedor de e-mail.
                                String userEmail = null;
                                String recipientEmail = null;
                                for (String token : tokenSet) {
                                    if (token.endsWith(":")) {
                                        userEmail = token;
                                    } else if (token.startsWith(">")) {
                                        recipientEmail = token;
                                    }
                                }
                                tokenSet.clear();
                                tokenSet.add(sender);
                                if (userEmail != null) {
                                    tokenSet.add(userEmail);
                                }
                                if (recipientEmail != null) {
                                    tokenSet.add(recipientEmail);
                                }
                                origem = sender;
                            } else {
                                origem = sender;
                            }
                            fluxo = origem + ">" + recipient;
                        } else if (hostname == null) {
                            origem = (sender == null ? "" : sender + '>') + ip;
                            fluxo = origem + ">" + recipient;
                        } else {
                            String dominio = Domain.extractDomain(hostname, true);
                            origem = (sender == null ? "" : sender + '>')
                                    + (dominio == null ? hostname : dominio.substring(1));
                            fluxo = origem + ">" + recipient;
                        }
                        Long recipientTrapTime = Trap.getTimeRecipient(client, user, recipient);
                        if (firstToken.equals("CHECK")) {
                            String results = "\nSPF resolution results:\n";
                            if (spf != null && spf.isInexistent()) {
                                results += "   NXDOMAIN\n";
                            } else if (logList == null || logList.isEmpty()) {
                                results += "   NONE\n";
                            } else {
                                for (String log : logList) {
                                    results += "   " + log + "\n";
                                }
                            }
                            String white;
                            String block;
                            if ((white = White.find(client, user, ip, sender, hostname, result,
                                    recipient)) != null) {
                                results += "\nFirst WHITE match: " + white + "\n";
                            } else if ((block = Block.find(client, user, ip, sender, hostname, result,
                                    recipient, false, true, true, false)) != null) {
                                results += "\nFirst BLOCK match: " + block + "\n";
                            }
                            TreeSet<String> graceSet = new TreeSet<String>();
                            if (Domain.isGraceTime(sender)) {
                                graceSet.add(Domain.extractDomain(sender, false));
                            }
                            if (Domain.isGraceTime(hostname)) {
                                graceSet.add(Domain.extractDomain(hostname, false));
                            }
                            if (!graceSet.isEmpty()) {
                                results += "\n";
                                results += "Domains in grace time:\n";
                                for (String grace : graceSet) {
                                    results += "   " + grace + "\n";
                                }
                            }
                            results += "\n";
                            results += "Considered identifiers and status:\n";
                            tokenSet = expandTokenSet(tokenSet);
                            TreeMap<String, Distribution> distributionMap = CacheDistribution.getMap(tokenSet);
                            int count = 0;
                            for (String token : tokenSet) {
                                if (!token.startsWith(">") && !token.endsWith(":")) {
                                    if (!Ignore.contains(token)) {
                                        float probability;
                                        Status status;
                                        if (distributionMap.containsKey(token)) {
                                            Distribution distribution = distributionMap.get(token);
                                            probability = distribution.getSpamProbability(token);
                                            status = distribution.getStatus(token);
                                        } else {
                                            probability = 0.0f;
                                            status = SPF.Status.GREEN;
                                        }
                                        results += "   " + token + " " + status.name() + " "
                                                + Core.DECIMAL_FORMAT.format(probability) + "\n";
                                        count++;
                                    }
                                }
                            }
                            if (count == 0) {
                                results += "   NONE\n";
                            }
                            results += "\n";
                            return results;
                        } else if (recipientTrapTime == null
                                && White.contains(client, user, ip, sender, hostname, result, recipient)) {
                            if (White.contains(client, user, ip, sender, hostname, result, null)) {
                                // Limpa da lista BLOCK um possvel falso positivo.
                                Block.clear(client, user, ip, sender, hostname, result, null);
                            }
                            // Calcula frequencia de consultas.
                            String url = Core.getURL();
                            String ticket = SPF.addQueryHam(client, user, ip, helo, hostname, sender, result,
                                    recipient, tokenSet, "WHITE");
                            return "WHITE " + (url == null ? ticket : url + ticket) + "\n";
                        } else if (Block.contains(client, user, ip, sender, hostname, result, recipient, true,
                                true, true, true)) {
                            Action action = client == null ? Action.REJECT : client.getActionBLOCK();
                            if (action == Action.REJECT) {
                                // Calcula frequencia de consultas.
                                long time = Server.getNewUniqueTime();
                                User.Query queryLocal = SPF.addQuerySpam(time, client, user, ip, helo, hostname,
                                        sender, result, recipient, tokenSet, "BLOCK");
                                action = client == null ? Action.FLAG : client.getActionRED();
                                if (action != Action.REJECT && queryLocal != null && queryLocal.needHeader()) {
                                    if (action == Action.FLAG) {
                                        queryLocal.setResult("FLAG");
                                        String url = Core.getURL();
                                        String ticket = SPF.createTicket(time, tokenSet);
                                        return "FLAG " + (url == null ? ticket : url + ticket) + "\n";
                                    } else if (action == Action.HOLD) {
                                        queryLocal.setResult("HOLD");
                                        String url = Core.getURL();
                                        String ticket = SPF.createTicket(time, tokenSet);
                                        return "HOLD " + (url == null ? ticket : url + ticket) + "\n";
                                    } else {
                                        return "ERROR: UNDEFINED ACTION\n";
                                    }
                                } else {
                                    String url = Core.getUnblockURL(client, user, ip, sender, hostname,
                                            recipient);
                                    if (url == null) {
                                        return "BLOCKED\n";
                                    } else {
                                        return "BLOCKED " + url + "\n";
                                    }
                                }
                            } else if (action == Action.FLAG) {
                                String url = Core.getURL();
                                String ticket = SPF.getTicket(client, user, ip, helo, hostname, sender, result,
                                        recipient, tokenSet, "FLAG");
                                return "FLAG " + (url == null ? ticket : url + ticket) + "\n";
                            } else if (action == Action.HOLD) {
                                String url = Core.getURL();
                                String ticket = SPF.getTicket(client, user, ip, helo, hostname, sender, result,
                                        recipient, tokenSet, "HOLD");
                                return "HOLD " + (url == null ? ticket : url + ticket) + "\n";
                            } else {
                                return "ERROR: UNDEFINED ACTION\n";
                            }
                        } else if (Generic.containsDynamicDomain(hostname)) {
                            // Bloquear automaticamente range de IP dinmico.
                            String cidr = Subnet
                                    .normalizeCIDR(SubnetIPv4.isValidIPv4(ip) ? ip + "/24" : ip + "/48");
                            if (Block.tryOverlap(cidr)) {
                                Server.logDebug(
                                        "new BLOCK '" + cidr + "' added by '" + hostname + ";DYNAMIC'.");
                            } else if (Block.tryAdd(ip)) {
                                Server.logDebug("new BLOCK '" + ip + "' added by '" + hostname + ";DYNAMIC'.");
                            }
                            SPF.addQuerySpam(client, user, ip, helo, hostname, sender, result, recipient,
                                    tokenSet, "INVALID");
                            return "INVALID\n";
                        } else if (spf != null && spf.isDefinitelyInexistent()) {
                            // Bloquear automaticamente IP com reputao vermelha.
                            if (SPF.isRed(ip)) {
                                if (Block.tryAdd(ip)) {
                                    Server.logDebug("new BLOCK '" + ip + "' added by '" + mx + ";NXDOMAIN'.");
                                }
                            }
                            Analise.processToday(ip);
                            // O domnio foi dado como inexistente inmeras vezes.
                            // Rejeitar e denunciar o host pois h abuso de tentativas.
                            SPF.addQuerySpam(client, user, ip, helo, hostname, sender, result, recipient,
                                    tokenSet, "NXDOMAIN");
                            return "NXDOMAIN\n";
                        } else if (spf != null && spf.isInexistent()) {
                            Analise.processToday(ip);
                            SPF.addQuery(client, user, ip, helo, hostname, sender, result, recipient, tokenSet,
                                    "NXDOMAIN");
                            return "NXDOMAIN\n";
                        } else if (result.equals("FAIL")) {
                            // Bloquear automaticamente IP com reputao vermelha.
                            if (SPF.isRed(ip)) {
                                if (Block.tryAdd(ip)) {
                                    Server.logDebug("new BLOCK '" + ip + "' added by '" + sender + ";FAIL'.");
                                }
                            }
                            Analise.processToday(ip);
                            SPF.addQuerySpam(client, user, ip, helo, hostname, sender, result, recipient,
                                    tokenSet, "FAIL");
                            // Retornar FAIL somente se no houver 
                            // liberao literal do remetente com FAIL.
                            return "FAIL\n";
                        } else if (sender != null && !Domain.isEmail(sender)) {
                            // Bloquear automaticamente IP com reputao vermelha.
                            if (SPF.isRed(ip)) {
                                if (Block.tryAdd(ip)) {
                                    Server.logDebug(
                                            "new BLOCK '" + ip + "' added by '" + sender + ";INVALID'.");
                                }
                            }
                            Analise.processToday(ip);
                            SPF.addQuerySpam(client, user, ip, helo, hostname, sender, result, recipient,
                                    tokenSet, "INVALID");
                            return "INVALID\n";
                        } else if (sender != null && Domain.isOfficialTLD(sender)) {
                            // Bloquear automaticamente IP com reputao vermelha.
                            if (SPF.isRed(ip)) {
                                if (Block.tryAdd(ip)) {
                                    Server.logDebug(
                                            "new BLOCK '" + ip + "' added by '" + sender + ";RESERVED'.");
                                }
                            }
                            Analise.processToday(ip);
                            SPF.addQuerySpam(client, user, ip, helo, hostname, sender, result, recipient,
                                    tokenSet, "INVALID");
                            return "INVALID\n";
                        } else if (sender == null && !CacheHELO.match(ip, hostname, false)) {
                            // Bloquear automaticamente IP com reputao ruim.
                            if (SPF.isRed(ip)) {
                                if (Block.tryAdd(ip)) {
                                    Server.logDebug("new BLOCK '" + ip + "' added by 'INVALID'.");
                                }
                            }
                            Analise.processToday(ip);
                            SPF.addQuerySpam(client, user, ip, helo, hostname, sender, result, recipient,
                                    tokenSet, "INVALID");
                            // HELO invlido sem remetente.
                            return "INVALID\n";
                        } else if (hostname == null && Core.isReverseRequired()) {
                            if (Block.tryAdd(ip)) {
                                Server.logDebug("new BLOCK '" + ip + "' added by 'NONE'.");
                            }
                            Analise.processToday(ip);
                            SPF.addQuerySpam(client, user, ip, helo, hostname, sender, result, recipient,
                                    tokenSet, "INVALID");
                            // Require a valid HELO or reverse.
                            return "INVALID\n";
                        } else if (recipient != null && !Domain.isValidEmail(recipient)) {
                            Analise.processToday(ip);
                            Analise.processToday(mx);
                            SPF.getTicket(client, user, ip, helo, hostname, sender, result, recipient, tokenSet,
                                    "INEXISTENT");
                            return "INEXISTENT\n";
                        } else if (recipientTrapTime != null) {
                            if (System.currentTimeMillis() > recipientTrapTime) {
                                // Spamtrap
                                for (String token : tokenSet) {
                                    String block;
                                    Status status = SPF.getStatus(token);
                                    if (status == Status.RED && (block = Block.add(token)) != null) {
                                        Server.logDebug("new BLOCK '" + block + "' added by '" + recipient
                                                + ";SPAMTRAP'.");
                                        Peer.sendBlockToAll(block);
                                    }
                                    if (status != Status.GREEN && !Subnet.isValidIP(token)
                                            && (block = Block.addIfNotNull(user, token)) != null) {
                                        Server.logDebug("new BLOCK '" + block + "' added by '" + recipient
                                                + ";SPAMTRAP'.");
                                    }
                                }
                                Analise.processToday(ip);
                                Analise.processToday(mx);
                                // Calcula frequencia de consultas.
                                SPF.addQuerySpam(client, user, ip, helo, hostname, sender, result, recipient,
                                        tokenSet, "TRAP");
                                return "SPAMTRAP\n";
                            } else {
                                // Inexistent
                                for (String token : tokenSet) {
                                    String block;
                                    Status status = SPF.getStatus(token);
                                    if (status == Status.RED && (block = Block.add(token)) != null) {
                                        Server.logDebug("new BLOCK '" + block + "' added by '" + recipient
                                                + ";INEXISTENT'.");
                                        Peer.sendBlockToAll(block);
                                    }
                                    if (status != Status.GREEN && !Subnet.isValidIP(token)
                                            && (block = Block.addIfNotNull(user, token)) != null) {
                                        Server.logDebug("new BLOCK '" + block + "' added by '" + recipient
                                                + ";INEXISTENT'.");
                                    }
                                }
                                Analise.processToday(ip);
                                Analise.processToday(mx);
                                SPF.getTicket(client, user, ip, helo, hostname, sender, result, recipient,
                                        tokenSet, "INEXISTENT");
                                return "INEXISTENT\n";
                            }
                        } else if (Defer.count(fluxo) > Core.getFloodMaxRetry()) {
                            Analise.processToday(ip);
                            Analise.processToday(mx);
                            // A origem atingiu o limite de atraso 
                            // para liberao do destinatrio.
                            long time = System.currentTimeMillis();
                            Defer.end(fluxo);
                            Server.logDefer(time, fluxo, "DEFER FLOOD");
                            SPF.addQuerySpam(client, user, ip, helo, hostname, sender, result, recipient,
                                    tokenSet, "REJECT");
                            return "BLOCKED\n";
                        } else if (!result.equals("PASS") && !CacheHELO.match(ip, hostname, false)) {
                            // Bloquear automaticamente IP com reputao amarela.
                            if (SPF.isRed(ip)) {
                                if (Block.tryAdd(ip)) {
                                    Server.logDebug(
                                            "new BLOCK '" + ip + "' added by '" + recipient + ";INVALID'.");
                                }
                            }
                            Analise.processToday(ip);
                            SPF.addQuerySpam(client, user, ip, helo, hostname, sender, result, recipient,
                                    tokenSet, "INVALID");
                            return "INVALID\n";
                        } else if (recipient != null && recipient.startsWith("postmaster@")) {
                            String url = Core.getURL();
                            String ticket = SPF.getTicket(client, user, ip, helo, hostname, sender, result,
                                    recipient, tokenSet, "ACCEPT");
                            return result + " "
                                    + (url == null ? ticket : url + URLEncoder.encode(ticket, "UTF-8")) + "\n";
                        } else if (result.equals("PASS")
                                && SPF.isGood(Provider.containsExact(mx) ? sender : mx)) {
                            // O remetente  vlido e tem excelente reputao,
                            // ainda que o provedor dele esteja com reputao ruim.
                            String url = Core.getURL();
                            String ticket = SPF.addQueryHam(client, user, ip, helo, hostname, sender, result,
                                    recipient, tokenSet, "ACCEPT");
                            return "PASS " + (url == null ? ticket : url + URLEncoder.encode(ticket, "UTF-8"))
                                    + "\n";
                        } else if (SPF.hasRed(tokenSet) || Analise.isCusterRED(ip, sender, hostname)) {
                            Analise.processToday(ip);
                            Analise.processToday(mx);
                            Action action = client == null ? Action.REJECT : client.getActionRED();
                            if (action == Action.REJECT) {
                                // Calcula frequencia de consultas.
                                SPF.addQuerySpam(client, user, ip, helo, hostname, sender, result, recipient,
                                        tokenSet, "REJECT");
                                return "BLOCKED\n";
                            } else if (action == Action.DEFER) {
                                if (Defer.defer(fluxo, Core.getDeferTimeRED())) {
                                    String url = Core.getReleaseURL(fluxo);
                                    SPF.addQuery(client, user, ip, helo, hostname, sender, result, recipient,
                                            tokenSet, "LISTED");
                                    if (url == null || Defer.count(fluxo) > 1) {
                                        return "LISTED\n";
                                    } else if (result.equals("PASS")
                                            && enviarLiberacao(url, sender, recipient)) {
                                        // Envio da liberao por e-mail se 
                                        // houver validao do remetente por PASS.
                                        return "LISTED\n";
                                    } else {
                                        return "LISTED " + url + "\n";
                                    }
                                } else {
                                    // Calcula frequencia de consultas.
                                    SPF.addQuerySpam(client, user, ip, helo, hostname, sender, result,
                                            recipient, tokenSet, "REJECT");
                                    return "BLOCKED\n";
                                }
                            } else if (action == Action.FLAG) {
                                String url = Core.getURL();
                                String ticket = SPF.getTicket(client, user, ip, helo, hostname, sender, result,
                                        recipient, tokenSet, "FLAG");
                                return "FLAG " + (url == null ? ticket : url + ticket) + "\n";
                            } else if (action == Action.HOLD) {
                                String url = Core.getURL();
                                String ticket = SPF.getTicket(client, user, ip, helo, hostname, sender, result,
                                        recipient, tokenSet, "HOLD");
                                return "HOLD " + (url == null ? ticket : url + ticket) + "\n";
                            } else {
                                return "ERROR: UNDEFINED ACTION\n";
                            }
                        } else if (Domain.isGraceTime(sender) || Domain.isGraceTime(hostname)) {
                            Server.logTrace("domain in grace time.");
                            for (String token : tokenSet) {
                                String block;
                                Status status = SPF.getStatus(token);
                                if (status == Status.RED && (block = Block.add(token)) != null) {
                                    Server.logDebug("new BLOCK '" + block + "' added by '" + status + "'.");
                                    Peer.sendBlockToAll(block);
                                }
                                if (status != Status.GREEN && !Subnet.isValidIP(token)
                                        && (block = Block.addIfNotNull(user, token)) != null) {
                                    Server.logDebug("new BLOCK '" + block + "' added by '" + status + "'.");
                                }
                            }
                            Analise.processToday(ip);
                            Analise.processToday(mx);
                            Action action = client == null ? Action.REJECT : client.getActionGRACE();
                            if (action == Action.REJECT) {
                                // Calcula frequencia de consultas.
                                SPF.addQuerySpam(client, user, ip, helo, hostname, sender, result, recipient,
                                        tokenSet, "REJECT");
                                return "BLOCKED\n";
                            } else if (action == Action.DEFER) {
                                if (Defer.defer(fluxo, Core.getDeferTimeRED())) {
                                    String url = Core.getReleaseURL(fluxo);
                                    SPF.addQuery(client, user, ip, helo, hostname, sender, result, recipient,
                                            tokenSet, "LISTED");
                                    if (url == null || Defer.count(fluxo) > 1) {
                                        return "LISTED\n";
                                    } else if (result.equals("PASS")
                                            && enviarLiberacao(url, sender, recipient)) {
                                        // Envio da liberao por e-mail se 
                                        // houver validao do remetente por PASS.
                                        return "LISTED\n";
                                    } else {
                                        return "LISTED " + url + "\n";
                                    }
                                } else {
                                    // Calcula frequencia de consultas.
                                    SPF.addQuerySpam(client, user, ip, helo, hostname, sender, result,
                                            recipient, tokenSet, "REJECT");
                                    return "BLOCKED\n";
                                }
                            } else if (action == Action.FLAG) {
                                String url = Core.getURL();
                                String ticket = SPF.getTicket(client, user, ip, helo, hostname, sender, result,
                                        recipient, tokenSet, "FLAG");
                                return "FLAG " + (url == null ? ticket : url + ticket) + "\n";
                            } else if (action == Action.HOLD) {
                                String url = Core.getURL();
                                String ticket = SPF.getTicket(client, user, ip, helo, hostname, sender, result,
                                        recipient, tokenSet, "HOLD");
                                return "HOLD " + (url == null ? ticket : url + ticket) + "\n";
                            } else {
                                return "ERROR: UNDEFINED ACTION\n";
                            }
                        } else if (SPF.hasYellow(tokenSet) && Defer.defer(fluxo, Core.getDeferTimeYELLOW())) {
                            Analise.processToday(ip);
                            Analise.processToday(mx);
                            Action action = client == null ? Action.DEFER : client.getActionYELLOW();
                            if (action == Action.DEFER) {
                                // Pelo menos um identificador do conjunto est em greylisting com atrazo de 10min.
                                SPF.addQuery(client, user, ip, helo, hostname, sender, result, recipient,
                                        tokenSet, "GREYLIST");
                                return "GREYLIST\n";
                            } else if (action == Action.HOLD) {
                                String url = Core.getURL();
                                String ticket = SPF.getTicket(client, user, ip, helo, hostname, sender, result,
                                        recipient, tokenSet, "HOLD");
                                return "HOLD " + (url == null ? ticket : url + ticket) + "\n";
                            } else {
                                return "ERROR: UNDEFINED ACTION\n";
                            }
                        } else if (SPF.isFlood(tokenSet) && !Provider.containsHELO(ip, hostname)
                                && Defer.defer(origem, Core.getDeferTimeFLOOD())) {
                            Analise.processToday(ip);
                            Analise.processToday(mx);
                            // Pelo menos um identificador est com frequncia superior ao permitido.
                            Server.logDebug("FLOOD " + tokenSet);
                            SPF.addQuery(client, user, ip, helo, hostname, sender, result, recipient, tokenSet,
                                    "GREYLIST");
                            return "GREYLIST\n";
                        } else if (result.equals("SOFTFAIL") && !Provider.containsHELO(ip, hostname)
                                && Defer.defer(fluxo, Core.getDeferTimeSOFTFAIL())) {
                            Analise.processToday(ip);
                            Analise.processToday(mx);
                            // SOFTFAIL com atrazo de 1min.
                            SPF.addQuery(client, user, ip, helo, hostname, sender, result, recipient, tokenSet,
                                    "GREYLIST");
                            return "GREYLIST\n";
                        } else {
                            Analise.processToday(ip);
                            Analise.processToday(mx);
                            // Calcula frequencia de consultas.
                            String url = Core.getURL();
                            String ticket = SPF.addQueryHam(client, user, ip, helo, hostname, sender, result,
                                    recipient, tokenSet, "ACCEPT");
                            return result + " "
                                    + (url == null ? ticket : url + URLEncoder.encode(ticket, "UTF-8")) + "\n";
                        }
                    }
                } catch (ProcessException ex) {
                    if (ex.isErrorMessage("HOST NOT FOUND")) {
                        return "NXDOMAIN\n";
                    } else {
                        throw ex;
                    }
                }
            } else {
                return "INVALID QUERY\n";
            }
        }
        return result;
    } catch (ProcessException ex) {
        Server.logError(ex);
        return ex.getMessage() + "\n";
    } catch (Exception ex) {
        Server.logError(ex);
        return "ERROR: FATAL\n";
    }
}

From source file:com.zimbra.cs.account.ProvUtil.java

private void dumpAttrs(Map<String, Object> attrsIn, Set<String> specificAttrs) throws ServiceException {
    TreeMap<String, Object> attrs = new TreeMap<String, Object>(attrsIn);

    Map<String, Set<String>> specificAttrValues = null;

    if (specificAttrs != null) {
        specificAttrValues = new HashMap<String, Set<String>>();
        for (String specificAttr : specificAttrs) {
            int colonAt = specificAttr.indexOf("=");
            String attrName = null;
            String attrValue = null;
            if (colonAt == -1) {
                attrName = specificAttr;
            } else {
                attrName = specificAttr.substring(0, colonAt);
                attrValue = specificAttr.substring(colonAt + 1);
                if (attrValue.length() < 1) {
                    throw ServiceException.INVALID_REQUEST("missing value for " + specificAttr, null);
                }/*from w  w  w  .ja v a 2 s . co  m*/
            }

            attrName = attrName.toLowerCase();
            Set<String> values = specificAttrValues.get(attrName);
            if (values == null) { // haven't seen the attr yet
                values = new HashSet<String>();
            }
            if (attrValue != null) {
                values.add(attrValue);
            }
            specificAttrValues.put(attrName, values);
        }
    }

    AttributeManager attrMgr = AttributeManager.getInstance();

    SimpleDateFormat dateFmt = new SimpleDateFormat("yyyyMMddHHmmss");
    String timestamp = dateFmt.format(new Date());

    for (Map.Entry<String, Object> entry : attrs.entrySet()) {
        String name = entry.getKey();

        boolean isBinary = needsBinaryIO(attrMgr, name);

        Set<String> specificValues = null;
        if (specificAttrValues != null) {
            specificValues = specificAttrValues.get(name.toLowerCase());
        }
        if (specificAttrValues == null || specificAttrValues.keySet().contains(name.toLowerCase())) {

            Object value = entry.getValue();

            if (value instanceof String[]) {
                String sv[] = (String[]) value;
                for (int i = 0; i < sv.length; i++) {
                    String aSv = sv[i];
                    // don't print permission denied attr
                    if (this.forceDisplayAttrValue || aSv.length() > 0 && (specificValues == null
                            || specificValues.isEmpty() || specificValues.contains(aSv))) {
                        printAttr(name, aSv, i, isBinary, timestamp);
                    }
                }
            } else if (value instanceof String) {
                // don't print permission denied attr
                if (this.forceDisplayAttrValue || ((String) value).length() > 0 && (specificValues == null
                        || specificValues.isEmpty() || specificValues.contains(value))) {
                    printAttr(name, (String) value, null, isBinary, timestamp);
                }
            }
        }
    }

    // force display empty value attribute
    if (this.forceDisplayAttrValue) {
        for (String attr : specificAttrs) {
            if (!attrs.containsKey(attr)) {
                AttributeInfo ai = attrMgr.getAttributeInfo(attr);
                if (ai != null) {
                    printAttr(attr, "", null, false, timestamp);
                }
            }
        }
    }

}

From source file:com.lp.server.fertigung.ejbfac.FertigungFacBean.java

private TreeMap<String, Object[]> add2TreeMap(TreeMap<String, Object[]> tm, String key, Object[] zeile) {

    if (tm.containsKey(key)) {
        Object[] zeileVorhanden = tm.get(key);
        BigDecimal bdMenge = (BigDecimal) zeileVorhanden[StuecklisteReportFac.REPORT_STUECKLISTE_LOSEAKTUALISIERT_KORREKTUR_AUSGABEMENGE];
        bdMenge = bdMenge.add(/*from w ww.  j a v  a2s . c  o  m*/
                (BigDecimal) zeile[StuecklisteReportFac.REPORT_STUECKLISTE_LOSEAKTUALISIERT_KORREKTUR_AUSGABEMENGE]);
        zeileVorhanden[StuecklisteReportFac.REPORT_STUECKLISTE_LOSEAKTUALISIERT_KORREKTUR_AUSGABEMENGE] = bdMenge;

        BigDecimal bdSollMenge = (BigDecimal) zeileVorhanden[StuecklisteReportFac.REPORT_STUECKLISTE_LOSEAKTUALISIERT_KORREKTUR_SOLLMENGE];
        bdSollMenge = bdSollMenge.add(
                (BigDecimal) zeile[StuecklisteReportFac.REPORT_STUECKLISTE_LOSEAKTUALISIERT_KORREKTUR_SOLLMENGE]);
        zeileVorhanden[StuecklisteReportFac.REPORT_STUECKLISTE_LOSEAKTUALISIERT_KORREKTUR_SOLLMENGE] = bdSollMenge;
        if (bdMenge.doubleValue() == 0 && bdSollMenge.doubleValue() == 0) {
            tm.remove(key);
        } else {
            tm.put(key, zeileVorhanden);
        }

    } else {
        tm.put(key, zeile);
    }

    return tm;
}