Example usage for java.util LinkedHashMap isEmpty

List of usage examples for java.util LinkedHashMap isEmpty

Introduction

In this page you can find the example usage for java.util LinkedHashMap isEmpty.

Prototype

boolean isEmpty();

Source Link

Document

Returns true if this map contains no key-value mappings.

Usage

From source file:com.compomics.pride_asa_pipeline.core.logic.enzyme.EnzymePredictor.java

public LinkedHashMap<Enzyme, Integer> getEnzymeCounts(List<String> peptideSequences) {
    LOGGER.info("Counting enzyme occurences");
    bestGuess = enzymeFactory.getEnzyme("Trypsin");
    HashMap<Character, Integer> AAbeforeMap = new HashMap<>();
    for (String sequence : peptideSequences) {
        char CharAAbeforeCleavage = sequence.charAt(sequence.length() - 1);
        int counter = 1;
        if (AAbeforeMap.containsKey(CharAAbeforeCleavage)) {
            counter = counter + (AAbeforeMap.get(CharAAbeforeCleavage));
        }// w  ww .  ja  va  2 s.c o m
        AAbeforeMap.put(CharAAbeforeCleavage, counter);
    }
    LinkedHashMap<Enzyme, Integer> enzymeHits = new LinkedHashMap<>();
    ArrayList<Enzyme> enzymes = enzymeFactory.getEnzymes();
    int totalEnzymeCounter = 0;
    for (Enzyme anEnzyme : enzymes) {
        int enzymeCounter = 0;
        for (char AA : anEnzyme.getAminoAcidBefore()) {
            if (AAbeforeMap.get(AA) != null) {
                enzymeCounter = enzymeCounter + AAbeforeMap.get(AA);
            }
        }
        if (enzymeCounter != 0) {
            LOGGER.debug("Checking " + anEnzyme.getName() + " - " + enzymeCounter);
            enzymeHits.put(anEnzyme, enzymeCounter);
        }
        totalEnzymeCounter += enzymeCounter;
    }
    //Sort on occurences 
    //Deal with special cases : 
    //default
    if (enzymeHits.isEmpty()) {
        enzymeHits.put(enzymeFactory.getEnzyme("Trypsin"), 9999);
    }
    return enzymeHits;
}

From source file:jp.or.openid.eiwg.scim.operation.Operation.java

/**
 * /*  ww  w . j  a  v a2  s.  c om*/
 *
 * @param context
 * @param request
 * @param targetId
 * @param attributes
 * @param filter
 * @param sortBy
 * @param sortOrder
 * @param startIndex
 * @param count
 */
public ArrayList<LinkedHashMap<String, Object>> searchUserInfo(ServletContext context,
        HttpServletRequest request, String targetId, String attributes, String filter, String sortBy,
        String sortOrder, String startIndex, String count) {
    ArrayList<LinkedHashMap<String, Object>> result = null;

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

    // ?
    setError(0, null, null);

    // ??
    if (attributes != null && !attributes.isEmpty()) {
        // 
        String[] tempList = attributes.split(",");
        for (int i = 0; i < tempList.length; i++) {
            String attributeName = tempList[i].trim();
            // ???????
            LinkedHashMap<String, Object> attributeSchema = SCIMUtil.getUserAttributeInfo(context,
                    attributeName, true);
            if (attributeSchema != null && !attributeSchema.isEmpty()) {
                returnAttributeNameSet.add(attributeName);
            } else {
                // ???????
                String message = String.format(MessageConstants.ERROR_INVALID_ATTRIBUTES, attributeName);
                setError(HttpServletResponse.SC_BAD_REQUEST, null, message);
                return result;
            }
        }
    }

    // ????????
    // (sortBy)?
    // (sortOrder)?
    // ?(startIndex)?
    // 1??(count)?
    //
    // ()

    // 
    result = new ArrayList<LinkedHashMap<String, Object>>();

    // ?
    @SuppressWarnings("unchecked")
    ArrayList<LinkedHashMap<String, Object>> users = (ArrayList<LinkedHashMap<String, Object>>) context
            .getAttribute("Users");
    if (users != null && !users.isEmpty()) {
        Iterator<LinkedHashMap<String, Object>> usersIt = users.iterator();
        while (usersIt.hasNext()) {
            boolean isMatched = false;
            LinkedHashMap<String, Object> userInfo = usersIt.next();

            // id???????
            if (targetId != null && !targetId.isEmpty()) {
                Object id = SCIMUtil.getAttribute(userInfo, "id");
                if (id != null && id instanceof String) {
                    // id????
                    if (targetId.equals(id.toString())) {
                        if (filter != null && !filter.isEmpty()) {
                            // ????
                            boolean matched = false;
                            try {
                                matched = SCIMUtil.checkUserSimpleFilter(context, userInfo, filter);
                            } catch (SCIMUtilException e) {
                                result = null;
                                setError(e.getCode(), e.getType(), e.getMessage());
                                break;
                            }

                            if (matched) {
                                isMatched = true;
                            }
                        } else {
                            isMatched = true;
                        }
                    }
                }
            } else {
                if (filter != null && !filter.isEmpty()) {
                    // ????
                    boolean matched = false;
                    try {
                        matched = SCIMUtil.checkUserSimpleFilter(context, userInfo, filter);
                    } catch (SCIMUtilException e) {
                        result = null;
                        setError(e.getCode(), e.getType(), e.getMessage());
                        break;
                    }

                    if (matched) {
                        isMatched = true;
                    }
                } else {
                    isMatched = true;
                }
            }

            if (isMatched) {
                // ??
                LinkedHashMap<String, Object> resultInfo = new LinkedHashMap<String, Object>();
                Iterator<String> attributeIt = userInfo.keySet().iterator();
                while (attributeIt.hasNext()) {
                    // ???
                    String attributeName = attributeIt.next();

                    // ?
                    LinkedHashMap<String, Object> attributeSchema = SCIMUtil.getUserAttributeInfo(context,
                            attributeName, true);
                    Object returned = attributeSchema.get("returned");

                    if (returned != null && returned.toString().equalsIgnoreCase("never")) {
                        continue;
                    }

                    // ?
                    Object attributeValue = userInfo.get(attributeName);

                    resultInfo.put(attributeName, attributeValue);
                }

                result.add(resultInfo);
            }
        }
    }

    return result;
}

From source file:jp.or.openid.eiwg.scim.operation.Operation.java

/**
 * ?//ww w  .j a v a 2s .  co m
 *
 * @param context
 * @param request
 * @param attributes
 * @param requestJson
 */
public LinkedHashMap<String, Object> createUserInfo(ServletContext context, HttpServletRequest request,
        String attributes, String requestJson) {
    LinkedHashMap<String, Object> result = null;

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

    // ?
    setError(0, null, null);

    // ??
    if (attributes != null && !attributes.isEmpty()) {
        // 
        String[] tempList = attributes.split(",");
        for (int i = 0; i < tempList.length; i++) {
            String attributeName = tempList[i].trim();
            // ???????
            LinkedHashMap<String, Object> attributeSchema = SCIMUtil.getUserAttributeInfo(context,
                    attributeName, true);
            if (attributeSchema != null && !attributeSchema.isEmpty()) {
                returnAttributeNameSet.add(attributeName);
            } else {
                // ???????
                String message = String.format(MessageConstants.ERROR_INVALID_ATTRIBUTES, attributeName);
                setError(HttpServletResponse.SC_BAD_REQUEST, null, message);
                return result;
            }
        }
    }

    // ?
    if (requestJson == null || requestJson.isEmpty()) {
        // 
        setError(HttpServletResponse.SC_BAD_REQUEST, null, MessageConstants.ERROR_INVALID_REQUEST);
        return result;
    }

    // (JSON)?
    ObjectMapper mapper = new ObjectMapper();
    LinkedHashMap<String, Object> requestObject = null;
    try {
        requestObject = mapper.readValue(requestJson, new TypeReference<LinkedHashMap<String, Object>>() {
        });
    } catch (JsonParseException e) {
        String datailMessage = e.getMessage();
        datailMessage = datailMessage.substring(0, datailMessage.indexOf('\n'));
        setError(HttpServletResponse.SC_BAD_REQUEST, null,
                MessageConstants.ERROR_INVALID_REQUEST + "(" + datailMessage + ")");
        return result;
    } catch (JsonMappingException e) {
        String datailMessage = e.getMessage();
        datailMessage = datailMessage.substring(0, datailMessage.indexOf('\n'));
        setError(HttpServletResponse.SC_BAD_REQUEST, null,
                MessageConstants.ERROR_INVALID_REQUEST + "(" + datailMessage + ")");
        return result;
    } catch (IOException e) {
        setError(HttpServletResponse.SC_INTERNAL_SERVER_ERROR, null, MessageConstants.ERROR_UNKNOWN);
        return result;
    }

    // ?
    if (requestObject != null && !requestObject.isEmpty()) {
        Iterator<String> attributeIt = requestObject.keySet().iterator();
        while (attributeIt.hasNext()) {
            // ???
            String attributeName = attributeIt.next();
            // ?
            LinkedHashMap<String, Object> attributeSchema = SCIMUtil.getUserAttributeInfo(context,
                    attributeName, true);
            if (attributeSchema != null) {
                // ????
                Object mutability = attributeSchema.get("mutability");
                if (mutability != null && mutability.toString().equalsIgnoreCase("readOnly")) {
                    // readOnly 
                    String message = String.format(MessageConstants.ERROR_READONLY_ATTRIBUTE, attributeName);
                    setError(HttpServletResponse.SC_BAD_REQUEST, null, message);
                    return result;
                }

                // ??
                // ()
            } else {
                // ????
                String message = String.format(MessageConstants.ERROR_UNKNOWN_ATTRIBUTE, attributeName);
                setError(HttpServletResponse.SC_BAD_REQUEST, null, message);
                return result;
            }
        }
    } else {
        // 
        setError(HttpServletResponse.SC_BAD_REQUEST, null, MessageConstants.ERROR_INVALID_REQUEST);
        return result;
    }

    // ?
    // ()

    LinkedHashMap<String, Object> newUserInfo = new LinkedHashMap<String, Object>();

    // id?
    UUID uuid = UUID.randomUUID();
    newUserInfo.put("id", uuid.toString());

    Iterator<String> attributeIt = requestObject.keySet().iterator();
    while (attributeIt.hasNext()) {
        // ???
        String attributeName = attributeIt.next();
        // ?
        Object attributeValue = requestObject.get(attributeName);

        newUserInfo.put(attributeName, attributeValue);
    }

    // meta?
    LinkedHashMap<String, Object> metaValues = new LinkedHashMap<String, Object>();
    // meta.resourceType 
    metaValues.put("resourceType", "User");
    // meta.created 
    SimpleDateFormat xsdDateTime = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.S'Z'");
    xsdDateTime.setTimeZone(TimeZone.getTimeZone("UTC"));
    metaValues.put("created", xsdDateTime.format(new Date()));
    // meta.location 
    String location = request.getScheme() + "://" + request.getServerName();
    int serverPort = request.getServerPort();
    if (serverPort != 80 && serverPort != 443) {
        location += ":" + Integer.toString(serverPort);
    }
    location += request.getContextPath();
    location += "/scim/Users/" + uuid.toString();
    metaValues.put("location", location);
    newUserInfo.put("meta", metaValues);

    // (??)
    @SuppressWarnings("unchecked")
    ArrayList<LinkedHashMap<String, Object>> users = (ArrayList<LinkedHashMap<String, Object>>) context
            .getAttribute("Users");
    if (users == null) {
        users = new ArrayList<LinkedHashMap<String, Object>>();
    }
    users.add(newUserInfo);
    context.setAttribute("Users", users);

    // ??
    result = new LinkedHashMap<String, Object>();
    attributeIt = newUserInfo.keySet().iterator();
    while (attributeIt.hasNext()) {
        // ???
        String attributeName = attributeIt.next();

        // ?
        LinkedHashMap<String, Object> attributeSchema = SCIMUtil.getUserAttributeInfo(context, attributeName,
                true);
        Object returned = attributeSchema.get("returned");

        if (returned != null && returned.toString().equalsIgnoreCase("never")) {
            continue;
        }

        // ?
        Object attributeValue = newUserInfo.get(attributeName);

        result.put(attributeName, attributeValue);
    }

    return result;
}

From source file:jp.or.openid.eiwg.scim.operation.Operation.java

/**
 * /*from  w  ww  .  j  ava2  s .c o m*/
 *
 * @param context
 * @param request
 * @param attributes
 * @param requestJson
 */
public LinkedHashMap<String, Object> updateUserInfo(ServletContext context, HttpServletRequest request,
        String targetId, String attributes, String requestJson) {
    LinkedHashMap<String, Object> result = null;

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

    // ?
    setError(0, null, null);

    // ??
    if (attributes != null && !attributes.isEmpty()) {
        // 
        String[] tempList = attributes.split(",");
        for (int i = 0; i < tempList.length; i++) {
            String attributeName = tempList[i].trim();
            // ???????
            LinkedHashMap<String, Object> attributeSchema = SCIMUtil.getUserAttributeInfo(context,
                    attributeName, true);
            if (attributeSchema != null && !attributeSchema.isEmpty()) {
                returnAttributeNameSet.add(attributeName);
            } else {
                // ???????
                String message = String.format(MessageConstants.ERROR_INVALID_ATTRIBUTES, attributeName);
                setError(HttpServletResponse.SC_BAD_REQUEST, null, message);
                return result;
            }
        }
    }

    // id?
    LinkedHashMap<String, Object> targetInfo = null;
    @SuppressWarnings("unchecked")
    ArrayList<LinkedHashMap<String, Object>> users = (ArrayList<LinkedHashMap<String, Object>>) context
            .getAttribute("Users");
    Iterator<LinkedHashMap<String, Object>> usersIt = null;
    if (users != null && !users.isEmpty()) {
        usersIt = users.iterator();
        while (usersIt.hasNext()) {
            LinkedHashMap<String, Object> userInfo = usersIt.next();
            Object id = SCIMUtil.getAttribute(userInfo, "id");
            if (id != null && id instanceof String) {
                if (targetId.equals(id.toString())) {
                    targetInfo = userInfo;
                    break;
                }
            }
        }
    }

    if (targetInfo == null) {
        setError(HttpServletResponse.SC_NOT_FOUND, null, MessageConstants.ERROR_NOT_FOUND);
        return result;
    }

    // ?
    if (requestJson == null || requestJson.isEmpty()) {
        // 
        setError(HttpServletResponse.SC_BAD_REQUEST, null, MessageConstants.ERROR_INVALID_REQUEST);
        return result;
    }

    // (JSON)?
    ObjectMapper mapper = new ObjectMapper();
    LinkedHashMap<String, Object> requestObject = null;
    try {
        requestObject = mapper.readValue(requestJson, new TypeReference<LinkedHashMap<String, Object>>() {
        });
    } catch (JsonParseException e) {
        String datailMessage = e.getMessage();
        datailMessage = datailMessage.substring(0, datailMessage.indexOf('\n'));
        setError(HttpServletResponse.SC_BAD_REQUEST, null,
                MessageConstants.ERROR_INVALID_REQUEST + "(" + datailMessage + ")");
        return result;
    } catch (JsonMappingException e) {
        String datailMessage = e.getMessage();
        datailMessage = datailMessage.substring(0, datailMessage.indexOf('\n'));
        setError(HttpServletResponse.SC_BAD_REQUEST, null,
                MessageConstants.ERROR_INVALID_REQUEST + "(" + datailMessage + ")");
        return result;
    } catch (IOException e) {
        setError(HttpServletResponse.SC_INTERNAL_SERVER_ERROR, null, MessageConstants.ERROR_UNKNOWN);
        return result;
    }

    // ?
    if (requestObject != null && !requestObject.isEmpty()) {
        Iterator<String> attributeIt = requestObject.keySet().iterator();
        while (attributeIt.hasNext()) {
            // ???
            String attributeName = attributeIt.next();
            // ?
            LinkedHashMap<String, Object> attributeSchema = SCIMUtil.getUserAttributeInfo(context,
                    attributeName, false);
            if (attributeSchema != null) {
                // ????
                Object mutability = attributeSchema.get("mutability");
                if (mutability != null && mutability.toString().equalsIgnoreCase("readOnly")) {
                    // readOnly 
                    String message = String.format(MessageConstants.ERROR_READONLY_ATTRIBUTE, attributeName);
                    setError(HttpServletResponse.SC_BAD_REQUEST, null, message);
                    return result;
                }

                // ??
                // ()
            } else {
                if (!attributeName.equalsIgnoreCase("schemas") && !attributeName.equalsIgnoreCase("id")
                        && !attributeName.equalsIgnoreCase("meta")) {
                    // ????
                    String message = String.format(MessageConstants.ERROR_UNKNOWN_ATTRIBUTE, attributeName);
                    setError(HttpServletResponse.SC_BAD_REQUEST, null, message);
                    return result;
                }
            }
        }
    } else {
        // 
        setError(HttpServletResponse.SC_BAD_REQUEST, null, MessageConstants.ERROR_INVALID_REQUEST);
        return result;
    }

    // ?
    // ()

    LinkedHashMap<String, Object> updateUserInfo = new LinkedHashMap<String, Object>();

    // 
    updateUserInfo.put("id", targetId);

    Iterator<String> attributeIt = requestObject.keySet().iterator();
    while (attributeIt.hasNext()) {
        // ???
        String attributeName = attributeIt.next();
        // ?
        Object attributeValue = requestObject.get(attributeName);

        updateUserInfo.put(attributeName, attributeValue);
    }

    // meta
    Object metaObject = targetInfo.get("meta");
    @SuppressWarnings("unchecked")
    LinkedHashMap<String, Object> metaValues = (LinkedHashMap<String, Object>) metaObject;
    // meta.lastModified 
    SimpleDateFormat xsdDateTime = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.S'Z'");
    xsdDateTime.setTimeZone(TimeZone.getTimeZone("UTC"));
    metaValues.put("lastModified", xsdDateTime.format(new Date()));

    updateUserInfo.put("meta", metaValues);

    // (??)
    usersIt.remove();
    users.add(updateUserInfo);
    context.setAttribute("Users", users);

    // ??
    result = new LinkedHashMap<String, Object>();
    attributeIt = updateUserInfo.keySet().iterator();
    while (attributeIt.hasNext()) {
        // ???
        String attributeName = attributeIt.next();

        // ?
        LinkedHashMap<String, Object> attributeSchema = SCIMUtil.getUserAttributeInfo(context, attributeName,
                true);
        Object returned = attributeSchema.get("returned");

        if (returned != null && returned.toString().equalsIgnoreCase("never")) {
            continue;
        }

        // ?
        Object attributeValue = updateUserInfo.get(attributeName);

        result.put(attributeName, attributeValue);
    }

    return result;
}

From source file:com.streamsets.pipeline.stage.processor.hive.HiveMetadataProcessor.java

@Override
protected void process(Record record, BatchMaker batchMaker) throws StageException {
    ELVars variables = getContext().createELVars();
    RecordEL.setRecordInContext(variables, record);
    TimeEL.setCalendarInContext(variables, Calendar.getInstance());
    TimeNowEL.setTimeNowInContext(variables, new Date());

    // Calculate record time for this particular record and persist it in the variables
    Date timeBasis = elEvals.timeDriverElEval.eval(variables, timeDriver, Date.class);
    Calendar calendar = Calendar.getInstance(timeZone);
    calendar.setTime(timeBasis);/* w ww .  j  a v  a  2 s .  c o  m*/
    TimeEL.setCalendarInContext(variables, calendar);

    String dbName = HiveMetastoreUtil.resolveEL(elEvals.dbNameELEval, variables, databaseEL);
    String tableName = HiveMetastoreUtil.resolveEL(elEvals.tableNameELEval, variables, tableEL);
    String targetPath;
    String avroSchema;
    String partitionStr = "";
    LinkedHashMap<String, String> partitionValMap;

    if (dbName.isEmpty()) {
        dbName = DEFAULT_DB;
    }
    try {
        // Validate Database and Table names
        if (!HiveMetastoreUtil.validateObjectName(dbName)) {
            throw new HiveStageCheckedException(Errors.HIVE_METADATA_03, "database name", dbName);
        }
        if (!HiveMetastoreUtil.validateObjectName(tableName)) {
            throw new HiveStageCheckedException(Errors.HIVE_METADATA_03, "table name", tableName);
        }

        partitionValMap = getPartitionValuesFromRecord(variables);

        if (partitioned) {
            partitionStr = externalTable
                    ? HiveMetastoreUtil.resolveEL(elEvals.partitionPathTemplateELEval, variables,
                            partitionPathTemplate)
                    : HiveMetastoreUtil.generatePartitionPath(partitionValMap);
            if (!partitionStr.startsWith("/"))
                partitionStr = "/" + partitionStr;
        }
        // First, find out if this record has all necessary data to process
        validateNames(dbName, tableName);
        String qualifiedName = HiveMetastoreUtil.getQualifiedTableName(dbName, tableName);
        LOG.trace("Generated table {} for record {}", qualifiedName, record.getHeader().getSourceId());

        if (externalTable) {
            // External table have location in the resolved EL
            targetPath = HiveMetastoreUtil.resolveEL(elEvals.tablePathTemplateELEval, variables,
                    tablePathTemplate);
        } else {
            // Internal table will be the database location + table name
            String databaseLocation;
            try {
                databaseLocation = databaseCache.get(dbName);
            } catch (ExecutionException e) {
                throw new HiveStageCheckedException(com.streamsets.pipeline.stage.lib.hive.Errors.HIVE_23,
                        e.getMessage());
            }
            targetPath = String.format("%s/%s", databaseLocation, tableName);
        }

        if (targetPath.isEmpty()) {
            throw new HiveStageCheckedException(Errors.HIVE_METADATA_02, targetPath);
        }

        // Obtain the record structure from current record
        LinkedHashMap<String, HiveTypeInfo> recordStructure = HiveMetastoreUtil.convertRecordToHMSType(record,
                elEvals.scaleEL, elEvals.precisionEL, elEvals.commentEL, decimalDefaultsConfig.scaleExpression,
                decimalDefaultsConfig.precisionExpression, commentExpression, variables);

        if (recordStructure.isEmpty()) { // If record has no data to process, No-op
            return;
        }

        TBLPropertiesInfoCacheSupport.TBLPropertiesInfo tblPropertiesInfo = HiveMetastoreUtil
                .getCacheInfo(cache, HMSCacheType.TBLPROPERTIES_INFO, qualifiedName, queryExecutor);

        if (tblPropertiesInfo != null) {
            HiveMetastoreUtil.validateTblPropertiesInfo(dataFormat, tblPropertiesInfo, tableName);

            if (tblPropertiesInfo.isExternal() != externalTable) {
                throw new HiveStageCheckedException(com.streamsets.pipeline.stage.lib.hive.Errors.HIVE_23,
                        "EXTERNAL", externalTable, tblPropertiesInfo.isExternal());
            }
        }

        TypeInfoCacheSupport.TypeInfo tableCache = HiveMetastoreUtil.getCacheInfo(cache, HMSCacheType.TYPE_INFO,
                qualifiedName, queryExecutor);

        if (tableCache != null) {
            //Checks number and name of partitions.
            HiveMetastoreUtil.validatePartitionInformation(tableCache, partitionValMap, qualifiedName);
            //Checks the type of partitions.
            Map<String, HiveTypeInfo> cachedPartitionTypeInfoMap = tableCache.getPartitionTypeInfo();
            for (Map.Entry<String, HiveTypeInfo> cachedPartitionTypeInfo : cachedPartitionTypeInfoMap
                    .entrySet()) {
                String partitionName = cachedPartitionTypeInfo.getKey();
                HiveTypeInfo expectedTypeInfo = cachedPartitionTypeInfo.getValue();
                HiveTypeInfo actualTypeInfo = partitionTypeInfo.get(partitionName);
                if (!expectedTypeInfo.equals(actualTypeInfo)) {
                    throw new HiveStageCheckedException(com.streamsets.pipeline.stage.lib.hive.Errors.HIVE_28,
                            partitionName, qualifiedName, expectedTypeInfo.toString(),
                            actualTypeInfo.toString());
                }
            }
            // Validate that the columns from record itself does not clash with partition columns
            for (String columnName : recordStructure.keySet()) {
                if (cachedPartitionTypeInfoMap.containsKey(columnName)) {
                    throw new HiveStageCheckedException(com.streamsets.pipeline.stage.lib.hive.Errors.HIVE_40,
                            columnName);
                }
            }
        }

        AvroSchemaInfoCacheSupport.AvroSchemaInfo schemaCache = HiveMetastoreUtil.getCacheInfo(cache,
                HMSCacheType.AVRO_SCHEMA_INFO, qualifiedName, queryExecutor);

        // True if there was a schema drift (including detection of new table)
        boolean schemaDrift = false;

        // Build final structure of how the table should look like
        LinkedHashMap<String, HiveTypeInfo> finalStructure;
        if (tableCache != null) {
            // Table already exists in Hive - so it's columns will be preserved and in their original order
            finalStructure = new LinkedHashMap<>();
            finalStructure.putAll(tableCache.getColumnTypeInfo());

            // If there is any diff (any new columns), we will append them at the end of the table
            LinkedHashMap<String, HiveTypeInfo> columnDiff = tableCache.getDiff(recordStructure);
            if (!columnDiff.isEmpty()) {
                LOG.trace("Detected drift for table {} - new columns: {}", qualifiedName,
                        StringUtils.join(columnDiff.keySet(), ","));
                schemaDrift = true;
                finalStructure.putAll(columnDiff);
            }
        } else {
            LOG.trace("{} is a new table", qualifiedName);
            // This table doesn't exists yet, so we'll use record own structure as the final table's structure
            schemaDrift = true;
            finalStructure = recordStructure;
        }

        // Generate schema only if the table do not exist or it's schema is changed.
        if (schemaDrift) {
            avroSchema = HiveMetastoreUtil.generateAvroSchema(finalStructure, qualifiedName);
            LOG.trace("Schema Drift. Generated new Avro schema for table {}: {}", qualifiedName, avroSchema);

            // Add custom metadata attributes if they are specified
            Map<String, String> metadataHeaderAttributeMap = new LinkedHashMap();
            if (metadataHeadersToAddExist) {
                metadataHeaderAttributeMap = generateResolvedHeaderAttributeMap(metadataHeaderAttributeConfigs,
                        variables);
            }

            handleSchemaChange(dbName, tableName, recordStructure, targetPath, avroSchema, batchMaker,
                    qualifiedName, tableCache, schemaCache, metadataHeaderAttributeMap);
        } else {
            if (schemaCache == null) { // Table exists in Hive, but this is cold start so the cache is null
                avroSchema = HiveMetastoreUtil.generateAvroSchema(finalStructure, qualifiedName);
                LOG.trace("Cold Start. Generated new Avro schema for table {}: {}", qualifiedName, avroSchema);
                updateAvroCache(schemaCache, avroSchema, qualifiedName);
            } else // No schema change, table already exists in Hive, and we have avro schema in cache.
                avroSchema = schemaCache.getSchema();
        }

        if (partitioned) {
            PartitionInfoCacheSupport.PartitionInfo pCache = HiveMetastoreUtil.getCacheInfo(cache,
                    HMSCacheType.PARTITION_VALUE_INFO, qualifiedName, queryExecutor);

            PartitionInfoCacheSupport.PartitionValues partitionValues = new PartitionInfoCacheSupport.PartitionValues(
                    partitionValMap);

            // If the partition information exist (thus this is not a cold start)
            if (pCache != null) {
                // If we detected drift, we need to persist that information and "roll" all partitions next time
                // we will see them.
                if (schemaDrift) {
                    pCache.setAllPartitionsToBeRolled();
                }

                // If we performed drift for the table and this is the firs time we see this partition, we need to
                // set the roll flag anyway.
                if (pCache.shouldRoll(partitionValues)) {
                    schemaDrift = true;
                }
            }

            // Append partition path to target path as all paths from now should be with the partition info
            targetPath += partitionStr;

            Map<PartitionInfoCacheSupport.PartitionValues, String> diff = detectNewPartition(partitionValues,
                    pCache, targetPath);

            // Send new partition metadata if new partition is detected.
            if (diff != null) {
                // Add custom metadata attributes if they are specified
                Map<String, String> partitionMetadataHeaderAttributeMap = new LinkedHashMap<>();
                if (metadataHeadersToAddExist) {
                    partitionMetadataHeaderAttributeMap = generateResolvedHeaderAttributeMap(
                            metadataHeaderAttributeConfigs, variables);
                }
                handleNewPartition(partitionValMap, pCache, dbName, tableName, targetPath, batchMaker,
                        qualifiedName, diff, partitionMetadataHeaderAttributeMap);
            }
        }

        // Send record to HDFS target.
        if (dataFormat == HMPDataFormat.PARQUET) {
            targetPath = targetPath + TEMP_AVRO_DIR_NAME;
        }

        changeRecordFieldToLowerCase(record);
        updateRecordForHDFS(record, schemaDrift, avroSchema, targetPath);
        batchMaker.addRecord(record, hdfsLane);
    } catch (HiveStageCheckedException error) {
        LOG.error("Error happened when processing record", error);
        LOG.trace("Record that caused the error: {}", record.toString());
        errorRecordHandler.onError(new OnRecordErrorException(record, error.getErrorCode(), error.getParams()));
    }
}

From source file:com.aliyun.odps.mapred.bridge.LOTGenerator.java

private DataSink genTableSink(LogicalOperatorTree.Builder tree, TableInfo tableInfo, String parentId,
        boolean overwrite) {
    LogicalOperator.Builder builder = LogicalOperator.newBuilder();

    DataSink.Builder db = DataSink.newBuilder();

    TableSink.Builder tw = TableSink.newBuilder();
    tw.setProject(tableInfo.getProjectName() == null ? project : tableInfo.getProjectName());
    tw.setTable(tableInfo.getTableName());
    tw.setIsOverwrite(overwrite);/* w ww .java2s .c o m*/

    LinkedHashMap<String, String> partSpec = tableInfo.getPartSpec();
    if (!partSpec.isEmpty()) {
        PartitionSpec.Builder pb = PartitionSpec.newBuilder();
        for (Map.Entry<String, String> e : partSpec.entrySet()) {
            PartitionSpec.Items.Builder ib = PartitionSpec.Items.newBuilder();
            ib.setKey(e.getKey());
            Constant.Builder cb = Constant.newBuilder();
            cb.setString(e.getValue());
            ib.setValue(cb.build());
            pb.addItems(ib.build());
        }
        tw.setPartition(pb.build());
    }

    db.setTableSink(tw.build());

    db.setParentId(parentId);
    db.setId("DataSink_" + opId++);

    DataSink dataSink = db.build();
    builder.setDataSink(dataSink);

    tree.addOperators(builder.build());

    return dataSink;
}

From source file:ma.glasnost.orika.metadata.ScoringClassMapBuilder.java

/**
 * Gets all of the property expressions for a given type, including all nested properties.
 * If the type of a property is not immutable and has any nested properties, it will not
 * be included. (Note that the 'class' property is explicitly excluded.)
 * //from  w w w  .  j  a v a  2s  .co m
 * @param type the type for which to gather properties
 * @return the map of nested properties keyed by expression name
 */
protected Map<String, Property> getPropertyExpressions(Type<?> type) {

    PropertyResolverStrategy propertyResolver = getPropertyResolver();

    Map<String, Property> properties = new HashMap<String, Property>();
    LinkedHashMap<String, Property> toProcess = new LinkedHashMap<String, Property>(
            propertyResolver.getProperties(type));

    if (type.isMap() || type.isList() || type.isArray()) {
        Property selfReferenceProperty = new Property.Builder().name("").getter("").setter(" = %s")
                .type(TypeFactory.valueOf(type)).build((PropertyResolver) propertyResolver);
        toProcess.put("", selfReferenceProperty);
    }

    while (!toProcess.isEmpty()) {

        Entry<String, Property> entry = toProcess.entrySet().iterator().next();
        if (!entry.getKey().equals("class")) {
            Property owningProperty = entry.getValue();
            Type<?> propertyType = owningProperty.getType();
            if (!ClassUtil.isImmutable(propertyType)) {
                Map<String, Property> props = propertyResolver.getProperties(propertyType);
                if (propertyType.isMap()) {
                    Map<String, Property> valueProperties = getPropertyExpressions(
                            propertyType.getNestedType(1));
                    for (Entry<String, Property> prop : valueProperties.entrySet()) {
                        Property elementProp = new NestedElementProperty(entry.getValue(), prop.getValue(),
                                propertyResolver);
                        String key = entry.getKey() + PropertyResolver.ELEMENT_PROPERT_PREFIX + prop.getKey()
                                + PropertyResolver.ELEMENT_PROPERT_SUFFIX;
                        toProcess.put(key, elementProp);
                    }
                } else if (propertyType.isList()) {
                    Map<String, Property> valueProperties = getPropertyExpressions(
                            propertyType.getNestedType(0));
                    for (Entry<String, Property> prop : valueProperties.entrySet()) {
                        Property elementProp = new NestedElementProperty(owningProperty, prop.getValue(),
                                propertyResolver);
                        String key = entry.getKey() + PropertyResolver.ELEMENT_PROPERT_PREFIX
                                + prop.getValue().getExpression() + PropertyResolver.ELEMENT_PROPERT_SUFFIX;
                        toProcess.put(key, elementProp);
                    }
                } else if (propertyType.isArray()) {
                    Map<String, Property> valueProperties = getPropertyExpressions(
                            propertyType.getComponentType());
                    for (Entry<String, Property> prop : valueProperties.entrySet()) {
                        Property elementProp = new NestedElementProperty(entry.getValue(), prop.getValue(),
                                propertyResolver);
                        String key = entry.getKey() + PropertyResolver.ELEMENT_PROPERT_PREFIX + prop.getKey()
                                + PropertyResolver.ELEMENT_PROPERT_SUFFIX;
                        toProcess.put(key, elementProp);
                    }
                } else if (!props.isEmpty()) {
                    for (Entry<String, Property> property : props.entrySet()) {
                        if (!property.getKey().equals("class")) {
                            String expression = entry.getKey() + "." + property.getKey();
                            toProcess.put(expression, resolveProperty(type, expression));
                        }
                    }
                } else {
                    properties.put(entry.getKey(), resolveProperty(type, entry.getKey()));
                }
            } else {
                properties.put(entry.getKey(), resolveProperty(type, entry.getKey()));
            }
        }
        toProcess.remove(entry.getKey());
    }
    return properties;
}

From source file:com.glaf.core.service.impl.MxTablePageServiceImpl.java

@SuppressWarnings({ "unchecked", "rawtypes" })
public List<Object> getTableList(String tableName, String idColumn, Map<String, String> selectColumns,
        int begin, int pageSize, List<QueryCondition> conditions,
        LinkedHashMap<String, Boolean> orderByColumns) {
    SqlExecutor sqlExecutor = QueryUtils.getMyBatisAndConditionSql(conditions);
    StringBuilder buffer = new StringBuilder();
    buffer.append(" select ");
    if (selectColumns != null && !selectColumns.isEmpty()) {
        Set<Entry<String, String>> entrySet = selectColumns.entrySet();
        for (Entry<String, String> entry : entrySet) {
            String columnName = entry.getKey();
            String columnLabel = entry.getValue();
            if (columnName != null && columnLabel != null) {
                buffer.append(columnName).append(" as ").append(columnLabel);
                buffer.append(" , ");
            }/*from   ww  w .  j  a  va  2  s . com*/
        }
        buffer.delete(buffer.length() - 2, buffer.length());
        buffer.append(" from ").append(tableName);
    } else {
        buffer.append(" * from ").append(tableName);
    }

    buffer.append(" where 1=1 ");

    String sql = buffer.toString() + sqlExecutor.getSql();

    if (orderByColumns != null && !orderByColumns.isEmpty()) {
        buffer.delete(0, buffer.length());
        buffer.append(" order by ");
        Set<Entry<String, Boolean>> entrySet = orderByColumns.entrySet();
        for (Entry<String, Boolean> entry : entrySet) {
            String columnName = entry.getKey();
            Boolean ordinal = entry.getValue();
            if (columnName != null && ordinal != null) {
                buffer.append(columnName);
                if (ordinal.booleanValue()) {
                    buffer.append(" asc");
                } else {
                    buffer.append(" desc");
                }
                buffer.append(" ,");
            }
        }
        if (buffer.toString().endsWith(",")) {
            buffer.delete(buffer.length() - 2, buffer.length());
        }
    }

    sql = sql + buffer.toString();

    Map<String, Object> params = new java.util.HashMap<String, Object>();
    if (sqlExecutor.getParameter() instanceof Map) {
        params.putAll((Map) sqlExecutor.getParameter());
    }

    logger.debug("sql:\n" + sql);
    logger.debug("params:" + params);

    params.put("queryString", sql);

    if (begin < 0) {
        begin = 0;
    }
    if (pageSize <= 0) {
        pageSize = Paging.DEFAULT_PAGE_SIZE;
    }
    RowBounds rowBounds = new RowBounds(begin, pageSize);
    List<Object> rows = sqlSession.selectList("getSqlQueryList", params, rowBounds);
    return rows;
}

From source file:eionet.cr.staging.exp.ExportRunner.java

/**
 *
 * @param rs/*from  w ww  .ja v a2s.  c  om*/
 * @throws SQLException
 * @throws DAOException
 */
private void processTestRow(ResultSet rs) throws SQLException, DAOException {

    LinkedHashMap<String, String> rowMap = new LinkedHashMap<String, String>();
    for (Entry<String, ObjectProperty> entry : queryConf.getColumnMappings().entrySet()) {

        ObjectProperty property = entry.getValue();
        String colName = entry.getKey();
        String colValue = rs.getString(colName);

        String valueTemplate = property.getValueTemplate();
        String propertyValue = valueTemplate == null ? colValue
                : StringUtils.replace(valueTemplate, "<value>", colValue);
        recordMissingConcepts(property, colValue, propertyValue);

        rowMap.put(colName, colValue);
    }

    if (!rowMap.isEmpty()) {
        testResults.add(rowMap);
    }
}

From source file:com.fizzed.rocker.compiler.JavaGenerator.java

private void createSourceTemplate(TemplateModel model, Writer w) throws GeneratorException, IOException {
    if (model.getOptions().getPostProcessing() != null) {
        // allow post-processors to transform the model
        try {//from   w ww.j  a  v a 2 s .c om
            model = postProcess(model);
        } catch (PostProcessorException ppe) {
            throw new GeneratorException("Error during post-processing of model.", ppe);
        }
    }

    // Used to register any withstatements we encounter, so we can generate all dynamic consumers at the end.
    final WithStatementConsumerGenerator withStatementConsumerGenerator = new WithStatementConsumerGenerator();

    // simple increment to help create unique var names
    int varCounter = -1;

    if (model.getPackageName() != null && !model.getPackageName().equals("")) {
        w.append("package ").append(model.getPackageName()).append(";").append(CRLF);
    }

    // imports regardless of template
    w.append(CRLF);
    w.append("import ").append(java.io.IOException.class.getName()).append(";").append(CRLF);
    w.append("import ").append(com.fizzed.rocker.ForIterator.class.getName()).append(";").append(CRLF);
    w.append("import ").append(com.fizzed.rocker.RenderingException.class.getName()).append(";").append(CRLF);
    w.append("import ").append(com.fizzed.rocker.RockerContent.class.getName()).append(";").append(CRLF);
    w.append("import ").append(com.fizzed.rocker.RockerOutput.class.getName()).append(";").append(CRLF);
    w.append("import ").append(com.fizzed.rocker.runtime.DefaultRockerTemplate.class.getName()).append(";")
            .append(CRLF);
    w.append("import ").append(com.fizzed.rocker.runtime.PlainTextUnloadedClassLoader.class.getName())
            .append(";").append(CRLF);

    // template imports
    if (model.getImports().size() > 0) {
        for (JavaImport i : model.getImports()) {
            w.append("// import ").append(sourceRef(i)).append(CRLF);
            w.append("import ").append(i.getStatement()).append(";").append(CRLF);
        }
    }

    w.append(CRLF);
    w.append("/*").append(CRLF);
    w.append(" * Auto generated code to render template ").append(model.getPackageName().replace('.', '/'))
            .append("/").append(model.getTemplateName()).append(CRLF);
    w.append(" * Do not edit this file. Changes will eventually be overwritten by Rocker parser!").append(CRLF);
    w.append(" */").append(CRLF);

    int indent = 0;

    // MODEL CLASS

    // class definition
    tab(w, indent).append("public class ").append(model.getName()).append(" extends ")
            .append(model.getOptions().getExtendsModelClass()).append(" {").append(CRLF);

    indent++;

    w.append(CRLF);

    // static info about this template
    tab(w, indent).append("static public final ").append(ContentType.class.getCanonicalName())
            .append(" CONTENT_TYPE = ").append(ContentType.class.getCanonicalName()).append(".")
            .append(model.getContentType().toString()).append(";").append(CRLF);
    tab(w, indent).append("static public final String TEMPLATE_NAME = \"").append(model.getTemplateName())
            .append("\";").append(CRLF);
    tab(w, indent).append("static public final String TEMPLATE_PACKAGE_NAME = \"")
            .append(model.getPackageName()).append("\";").append(CRLF);
    tab(w, indent).append("static public final String HEADER_HASH = \"").append(model.createHeaderHash() + "")
            .append("\";").append(CRLF);

    // Don't include MODIFIED_AT header when optimized compiler is used since this implicitly disables hot reloading anyhow
    if (!model.getOptions().getOptimize()) {
        tab(w, indent).append("static public final long MODIFIED_AT = ").append(model.getModifiedAt() + "")
                .append("L;").append(CRLF);
    }

    tab(w, indent).append("static public final String[] ARGUMENT_NAMES = {");
    StringBuilder argNameList = new StringBuilder();
    for (Argument arg : model.getArgumentsWithoutRockerBody()) {
        if (argNameList.length() > 0) {
            argNameList.append(",");
        }
        argNameList.append(" \"").append(arg.getExternalName()).append("\"");
    }
    w.append(argNameList).append(" };").append(CRLF);

    // model arguments as members of model class
    appendArgumentMembers(model, w, "private", false, indent);

    // model setters & getters with builder-style pattern
    // special case for the RockerBody argument which sorta "hides" its getter/setter
    if (model.getArguments().size() > 0) {
        for (Argument arg : model.getArguments()) {
            // setter
            w.append(CRLF);
            tab(w, indent).append("public ").append(model.getName()).append(" ").append(arg.getExternalName())
                    .append("(" + arg.getExternalType()).append(" ").append(arg.getName()).append(") {")
                    .append(CRLF);
            tab(w, indent + 1).append("this.").append(arg.getName()).append(" = ").append(arg.getName())
                    .append(";").append(CRLF);
            tab(w, indent + 1).append("return this;").append(CRLF);
            tab(w, indent).append("}").append(CRLF);

            // getter
            w.append(CRLF);
            tab(w, indent).append("public ").append(arg.getExternalType()).append(" ")
                    .append(arg.getExternalName()).append("() {").append(CRLF);
            tab(w, indent + 1).append("return this.").append(arg.getName()).append(";").append(CRLF);
            tab(w, indent).append("}").append(CRLF);
        }
    }

    w.append(CRLF);

    //
    // model "template" static builder
    //
    tab(w, indent).append("static public ").append(model.getName()).append(" template(");

    if (model.getArguments().size() > 0) {
        int i = 0;
        // RockerBody is NOT included (it is passed via a closure block in other templates)
        // so we only care about the other arguments
        for (Argument arg : model.getArgumentsWithoutRockerBody()) {
            if (i != 0) {
                w.append(", ");
            }
            w.append(arg.getType()).append(" ").append(arg.getName());
            i++;
        }
    }
    w.append(") {").append(CRLF);

    tab(w, indent + 1).append("return new ").append(model.getName()).append("()");
    if (model.getArguments().size() > 0) {
        int i = 0;
        for (Argument arg : model.getArgumentsWithoutRockerBody()) {
            w.append(CRLF);
            tab(w, indent + 2).append(".").append(arg.getName()).append("(").append(arg.getName()).append(")");
            i++;
        }
    }
    w.append(";").append(CRLF);
    tab(w, indent).append("}").append(CRLF);

    //
    // render of model
    //
    w.append(CRLF);

    tab(w, indent).append("@Override").append(CRLF);
    tab(w, indent).append("protected DefaultRockerTemplate buildTemplate() throws RenderingException {")
            .append(CRLF);

    if (model.getOptions().getOptimize()) {
        // model "template" static builder (not reloading support, fastest performance)
        tab(w, indent + 1).append("// optimized for performance (via rocker.optimize flag; no auto reloading)")
                .append(CRLF);
        tab(w, indent + 1).append("return new Template(this);").append(CRLF);
        //tab(w, indent+1).append("return template.__render(context);").append(CRLF);
    } else {
        tab(w, indent + 1).append(
                "// optimized for convenience (runtime auto reloading enabled if rocker.reloading=true)")
                .append(CRLF);
        // use bootstrap to create underlying template
        tab(w, indent + 1).append("return ").append(RockerRuntime.class.getCanonicalName())
                .append(".getInstance().getBootstrap().template(this.getClass(), this);").append(CRLF);
        //tab(w, indent+1).append("return template.__render(context);").append(CRLF);
    }

    tab(w, indent).append("}").append(CRLF);

    //
    // TEMPLATE CLASS
    //

    w.append(CRLF);

    // class definition
    tab(w, indent).append("static public class Template extends ").append(model.getOptions().getExtendsClass());

    w.append(" {").append(CRLF);

    indent++;

    // plain text -> map of chunks of text (Java only supports 2-byte length of string constant)
    LinkedHashMap<String, LinkedHashMap<String, String>> plainTextMap = model
            .createPlainTextMap(PLAIN_TEXT_CHUNK_LENGTH);

    if (!plainTextMap.isEmpty()) {

        w.append(CRLF);

        for (String plainText : plainTextMap.keySet()) {

            // include static text as comments in source (limit to 500)
            tab(w, indent).append("// ")
                    .append(StringUtils.abbreviate(RockerUtil.ESCAPE_JAVA.translate(plainText), 500))
                    .append(CRLF);
            for (Map.Entry<String, String> chunk : plainTextMap.get(plainText).entrySet()) {

                if (this.plainTextStrategy == PlainTextStrategy.STATIC_STRINGS) {
                    tab(w, indent).append("static private final String ").append(chunk.getKey()).append(" = \"")
                            .append(StringEscapeUtils.escapeJava(chunk.getValue())).append("\";").append(CRLF);
                } else if (this.plainTextStrategy == PlainTextStrategy.STATIC_BYTE_ARRAYS_VIA_UNLOADED_CLASS) {
                    tab(w, indent).append("static private final byte[] ").append(chunk.getKey()).append(";")
                            .append(CRLF);
                }

            }
        }

        // generate the static initializer
        if (this.plainTextStrategy == PlainTextStrategy.STATIC_BYTE_ARRAYS_VIA_UNLOADED_CLASS) {

            w.append(CRLF);

            tab(w, indent).append("static {").append(CRLF);

            String loaderClassName = unqualifiedClassName(PlainTextUnloadedClassLoader.class);
            tab(w, indent + 1).append(loaderClassName).append(" loader = ").append(loaderClassName)
                    .append(".tryLoad(").append(model.getName()).append(".class.getClassLoader(), ")
                    .append(model.getName()).append(".class.getName()").append(" + \"$PlainText\", \"")
                    .append(model.getOptions().getTargetCharset()).append("\");").append(CRLF);

            for (String plainText : plainTextMap.keySet()) {

                for (Map.Entry<String, String> chunk : plainTextMap.get(plainText).entrySet()) {

                    if (this.plainTextStrategy == PlainTextStrategy.STATIC_BYTE_ARRAYS_VIA_UNLOADED_CLASS) {
                        tab(w, indent + 1).append(chunk.getKey()).append(" = loader.tryGet(\"")
                                .append(chunk.getKey()).append("\");").append(CRLF);
                    }

                }

            }

            tab(w, indent).append("}").append(CRLF);
        }

    }

    // arguments as members of template class
    appendArgumentMembers(model, w, "protected", true, indent);

    w.append(CRLF);

    // constructor
    tab(w, indent).append("public Template(").append(model.getName()).append(" model) {").append(CRLF);

    tab(w, indent + 1).append("super(model);").append(CRLF);
    tab(w, indent + 1).append("__internal.setCharset(\"").append(model.getOptions().getTargetCharset())
            .append("\");").append(CRLF);
    tab(w, indent + 1).append("__internal.setContentType(CONTENT_TYPE);").append(CRLF);
    tab(w, indent + 1).append("__internal.setTemplateName(TEMPLATE_NAME);").append(CRLF);
    tab(w, indent + 1).append("__internal.setTemplatePackageName(TEMPLATE_PACKAGE_NAME);").append(CRLF);

    // each model argument passed along as well
    for (Argument arg : model.getArguments()) {
        tab(w, indent + 1).append("this.").append(arg.getName()).append(" = model.")
                .append(arg.getExternalName()).append("();").append(CRLF);
    }

    tab(w, indent).append("}").append(CRLF);

    w.append(CRLF);

    tab(w, indent).append("@Override").append(CRLF);
    tab(w, indent).append("protected void __doRender() throws IOException, RenderingException {").append(CRLF);

    // build rendering code
    int depth = 1;
    Deque<String> blockEnd = new ArrayDeque<>();

    for (TemplateUnit unit : model.getUnits()) {
        if (unit instanceof Comment) {
            continue;
        }

        // something like
        // IfBeginBlock
        // __internal.aboutToExecutePosInSourceTemplate(5, 10);
        appendCommentAndSourcePositionUpdate(w, depth + indent, unit);

        if (unit instanceof PlainText) {
            PlainText plain = (PlainText) unit;

            LinkedHashMap<String, String> chunks = plainTextMap.get(plain.getText());

            for (String chunkName : chunks.keySet()) {

                tab(w, depth + indent).append("__internal.writeValue(").append(chunkName).append(");")
                        .append(CRLF);

            }

        } else if (unit instanceof ValueExpression) {
            ValueExpression value = (ValueExpression) unit;
            tab(w, depth + indent).append("__internal.renderValue(").append(value.getExpression()).append(", ")
                    .append("" + value.isNullSafe()).append(");").append(CRLF);
        } else if (unit instanceof NullTernaryExpression) {
            NullTernaryExpression nullTernary = (NullTernaryExpression) unit;
            tab(w, depth + indent).append("{").append(CRLF);
            tab(w, depth + indent + 1).append("final Object __v = ").append(nullTernary.getLeftExpression())
                    .append(";").append(CRLF);
            tab(w, depth + indent + 1).append("if (__v != null) { __internal.renderValue(__v, false); }")
                    .append(CRLF);
            if (nullTernary.getRightExpression() != null) {
                tab(w, depth + indent + 1).append("else {__internal.renderValue(")
                        .append(nullTernary.getRightExpression()).append(", true); }").append(CRLF);
            }
            tab(w, depth + indent).append("}").append(CRLF);
        } else if (unit instanceof ValueClosureBegin) {

            ValueClosureBegin closure = (ValueClosureBegin) unit;
            tab(w, depth + indent).append("__internal.renderValue(").append(closure.getExpression())
                    .append(".__body(");

            // Java 1.8+ use lambda
            if (isJava8Plus(model)) {
                w.append("() -> {").append(CRLF);

                depth++;

                blockEnd.push("}), false);");
            }
            // Java 1.7- uses anonymous inner class
            else {
                w.append("new ").append(unqualifiedClassName(RockerContent.class)).append("() {").append(CRLF);

                depth++;

                blockEnd.push("}), false);");

                tab(w, depth + indent).append("@Override").append(CRLF);

                tab(w, depth + indent).append("public void render() throws IOException, RenderingException {")
                        .append(CRLF);

                depth++;

                blockEnd.push("}");
            }
        } else if (unit instanceof ValueClosureEnd) {
            // Java 1.8+ use lambda
            if (isJava8Plus(model)) {
                depth--;

                tab(w, depth + indent).append(blockEnd.pop()).append(" // value closure end ")
                        .append(sourceRef(unit)).append(CRLF);
            }
            // Java 1.7- uses anonymous inner class
            else {
                depth--;

                tab(w, depth + indent).append(blockEnd.pop()).append(CRLF);

                depth--;

                tab(w, depth + indent).append(blockEnd.pop()).append(" // value closure end ")
                        .append(sourceRef(unit)).append(CRLF);
            }
        } else if (unit instanceof ContentClosureBegin) {

            ContentClosureBegin closure = (ContentClosureBegin) unit;
            tab(w, depth + indent).append("RockerContent ").append(closure.getIdentifier()).append(" = ");

            // Java 1.8+ use lambda
            if (isJava8Plus(model)) {
                w.append("() -> {").append(CRLF);

                depth++;

                blockEnd.push("};");
            }
            // Java 1.7- uses anonymous inner class
            else {
                w.append("new ").append(unqualifiedClassName(com.fizzed.rocker.RockerContent.class))
                        .append("() {").append(CRLF);

                depth++;

                blockEnd.push("};");

                tab(w, depth + indent).append("@Override").append(CRLF);

                tab(w, depth + indent).append("public void render() throws IOException, RenderingException {")
                        .append(CRLF);

                depth++;

                blockEnd.push("}");
            }
        } else if (unit instanceof ContentClosureEnd) {
            // Java 1.8+ use lambda
            if (isJava8Plus(model)) {
                depth--;

                tab(w, depth + indent).append(blockEnd.pop()).append(" // content closure end ")
                        .append(sourceRef(unit)).append(CRLF);
            }
            // Java 1.7- uses anonymous inner class
            else {
                depth--;

                tab(w, depth + indent).append(blockEnd.pop()).append(CRLF);

                depth--;

                tab(w, depth + indent).append(blockEnd.pop()).append(" // content closure end ")
                        .append(sourceRef(unit)).append(CRLF);
            }
        } else if (unit instanceof IfBlockBegin) {
            IfBlockBegin block = (IfBlockBegin) unit;

            tab(w, depth + indent).append("if ").append(block.getExpression()).append(" {").append(CRLF);

            blockEnd.push("}");
            depth++;
        } else if (unit instanceof IfBlockElseIf) {
            final IfBlockElseIf block = (IfBlockElseIf) unit;

            depth--;

            // This keeps else-if nicely formatted in generated code.
            tab(w, depth + indent).append("} else if ").append(block.getExpression()).append(" {").append(CRLF);

            depth++;
        } else if (unit instanceof IfBlockElse) {
            depth--;

            tab(w, depth + indent).append("} else {").append(" // else ").append(sourceRef(unit)).append(CRLF);

            depth++;
        } else if (unit instanceof IfBlockEnd) {
            depth--;

            tab(w, depth + indent).append(blockEnd.pop()).append(" // if end ").append(sourceRef(unit))
                    .append(CRLF);

        } else if (unit instanceof WithBlockBegin) {
            WithBlockBegin block = (WithBlockBegin) unit;
            WithStatement stmt = block.getStatement();

            String statementConsumerName = withStatementConsumerGenerator.register(stmt);

            final List<WithStatement.VariableWithExpression> variables = stmt.getVariables();

            if (isJava8Plus(model)) {
                tab(w, depth + indent)
                        .append(variables.size() == 1 ? qualifiedClassName(WithBlock.class)
                                : WithStatementConsumerGenerator.WITH_BLOCKS_GENERATED_CLASS_NAME)
                        .append(".with(");
                // All expressions
                for (int i = 0; i < variables.size(); i++) {
                    final WithStatement.VariableWithExpression var = variables.get(i);
                    if (i > 0) {
                        w.append(", ");
                    }
                    w.append(var.getValueExpression());
                }
                w.append(", ").append(stmt.isNullSafe() + "").append(", (");
                for (int i = 0; i < variables.size(); i++) {
                    final WithStatement.VariableWithExpression var = variables.get(i);
                    if (i > 0) {
                        w.append(", ");
                    }
                    w.append(var.getVariable().getName());
                }
                w.append(") -> {").append(CRLF);

                depth++;

                blockEnd.push("});");

            } else {
                tab(w, depth + indent)
                        // Note for standard 1 variable with block we use the predefined consumers.
                        // Otherwise we fallback to the generated ones.
                        .append(variables.size() == 1 ? qualifiedClassName(WithBlock.class)
                                : WithStatementConsumerGenerator.WITH_BLOCKS_GENERATED_CLASS_NAME)
                        .append(".with(");

                // All expressions
                for (int i = 0; i < variables.size(); i++) {
                    final WithStatement.VariableWithExpression var = variables.get(i);
                    if (i > 0) {
                        w.append(", ");
                    }
                    w.append(var.getValueExpression());
                }
                w.append(", ").append(stmt.isNullSafe() + "").append(", (new ").append(statementConsumerName)
                        .append('<');

                // Types for the .with(..)
                for (int i = 0; i < variables.size(); i++) {
                    final JavaVariable variable = variables.get(i).getVariable();
                    if (i > 0) {
                        w.append(", ");
                    }
                    w.append(variable.getType());
                }
                w.append(">() {").append(CRLF);
                tab(w, depth + indent + 1).append("@Override public void accept(");
                for (int i = 0; i < variables.size(); i++) {
                    final JavaVariable variable = variables.get(i).getVariable();
                    if (i > 0) {
                        w.append(", ");
                    }
                    w.append("final ").append(variable.toString());
                }
                w.append(") throws IOException {").append(CRLF);

                depth++;

                blockEnd.push("}}));");
            }
        } else if (unit instanceof WithBlockElse) {
            depth--;

            if (isJava8Plus(model)) {
                tab(w, depth + indent).append("}, () -> {").append(CRLF);
            } else {
                tab(w, depth + indent).append("}}), (new ")
                        .append(qualifiedClassName(WithBlock.Consumer0.class)).append("() { ")
                        .append("@Override public void accept() throws IOException {").append(CRLF);
            }
            depth++;
        } else if (unit instanceof WithBlockEnd) {
            depth--;

            tab(w, depth + indent).append(blockEnd.pop()).append(" // with end ").append(sourceRef(unit))
                    .append(CRLF);
        } else if (unit instanceof ForBlockBegin) {
            ForBlockBegin block = (ForBlockBegin) unit;
            ForStatement stmt = block.getStatement();

            // break support via try and catch mechanism (works across lambdas!)
            tab(w, depth + indent).append("try {").append(CRLF);

            depth++;

            if (stmt.getForm() == ForStatement.Form.GENERAL) {
                // print out raw statement including parentheses
                tab(w, depth + indent).append("for ").append(block.getExpression()).append(" {").append(CRLF);

                blockEnd.push("}");
            } else if (stmt.getForm() == ForStatement.Form.ENHANCED) {
                // Java 1.8+ (use lambdas)
                if (stmt.hasAnyUntypedArguments() && isJava8Plus(model)) {

                    // build list of lambda vars
                    String localVars = "";
                    for (JavaVariable arg : stmt.getArguments()) {
                        if (localVars.length() != 0) {
                            localVars += ",";
                        }
                        localVars += arg.getName();
                    }

                    tab(w, depth + indent).append(Java8Iterator.class.getName()).append(".forEach(")
                            .append(stmt.getValueExpression()).append(", (").append(localVars).append(") -> {")
                            .append(CRLF);

                    blockEnd.push("});");
                } else {

                    // is the first argument a "ForIterator" ?
                    boolean forIterator = isForIteratorType(stmt.getArguments().get(0).getType());
                    int collectionCount = (forIterator ? 2 : 1);
                    int mapCount = (forIterator ? 3 : 2);

                    // type and value we are going to iterate thru
                    String iterateeType = null;
                    String valueExpression = null;
                    if (stmt.getArguments().size() == collectionCount) {
                        iterateeType = stmt.getArguments().get(collectionCount - 1).getTypeAsNonPrimitiveType();
                        valueExpression = stmt.getValueExpression();
                    } else if (stmt.getArguments().size() == mapCount) {
                        iterateeType = "java.util.Map.Entry<"
                                + stmt.getArguments().get(mapCount - 2).getTypeAsNonPrimitiveType() + ","
                                + stmt.getArguments().get(mapCount - 1).getTypeAsNonPrimitiveType() + ">";
                        valueExpression = stmt.getValueExpression() + ".entrySet()";
                    }

                    // create unique variable name for iterator
                    String forIteratorVarName = "__forIterator" + (++varCounter);

                    // ForIterator for collection and make it final to assure nested anonymous
                    // blocks can access it as well.
                    tab(w, depth + indent).append("final ")
                            .append(com.fizzed.rocker.runtime.CollectionForIterator.class.getName()).append("<")
                            .append(iterateeType).append(">").append(" ").append(forIteratorVarName)
                            .append(" = new ")
                            .append(com.fizzed.rocker.runtime.CollectionForIterator.class.getName()).append("<")
                            .append(iterateeType).append(">").append("(").append(valueExpression).append(");")
                            .append(CRLF);

                    // for loop same regardless of map vs. collection
                    tab(w, depth + indent).append("while (").append(forIteratorVarName).append(".hasNext()) {")
                            .append(CRLF);

                    // if forIterator request assign to local var and make it final to assure nested anonymous
                    // blocks can access it as well.
                    if (forIterator) {
                        tab(w, depth + indent + 1).append("final ")
                                .append(com.fizzed.rocker.ForIterator.class.getName()).append(" ")
                                .append(stmt.getArguments().get(0).getName()).append(" = ")
                                .append(forIteratorVarName).append(";").append(CRLF);
                    }

                    if (stmt.getArguments().size() == collectionCount) {
                        // assign item to local var and make it final to assure nested anonymous
                        // blocks can access it as well.
                        tab(w, depth + indent + 1).append("final ")
                                .append(stmt.getArguments().get(collectionCount - 1).toString()).append(" = ")
                                .append(forIteratorVarName).append(".next();").append(CRLF);
                    } else if (stmt.getArguments().size() == mapCount) {
                        // create unique variable name for iterator
                        String entryVarName = "__entry" + (++varCounter);

                        // assign map entry to local var
                        tab(w, depth + indent + 1).append("final ").append(iterateeType).append(" ")
                                .append(entryVarName).append(" = ").append(forIteratorVarName)
                                .append(".next();").append(CRLF);

                        // assign entry to local values  make it final to assure nested anonymous
                        // blocks can access it as well.
                        tab(w, depth + indent + 1).append("final ")
                                .append(stmt.getArguments().get(mapCount - 2).toString()).append(" = ")
                                .append(entryVarName).append(".getKey();").append(CRLF);

                        tab(w, depth + indent + 1).append("final ")
                                .append(stmt.getArguments().get(mapCount - 1).toString()).append(" = ")
                                .append(entryVarName).append(".getValue();").append(CRLF);
                    } else {
                        throw new GeneratorException("Unsupported number of arguments for for loop");
                    }

                    blockEnd.push("}");
                }
            }

            depth++;

            // continue support via try and catch mechanism (works across lambdas!)
            tab(w, depth + indent).append("try {").append(CRLF);

            depth++;

        } else if (unit instanceof ForBlockEnd) {
            depth--;

            // continue support via try and catch mechanism (works across lambdas!)
            tab(w, depth + indent).append("} catch (").append(ContinueException.class.getCanonicalName())
                    .append(" e) {").append(CRLF);

            tab(w, depth + indent + 1).append("// support for continuing for loops").append(CRLF);

            tab(w, depth + indent).append("}").append(CRLF);

            depth--;

            tab(w, depth + indent).append(blockEnd.pop()).append(" // for end ").append(sourceRef(unit))
                    .append(CRLF);

            depth--;

            // break support via try and catch mechanism (works across lambdas!)
            tab(w, depth + indent).append("} catch (").append(BreakException.class.getCanonicalName())
                    .append(" e) {").append(CRLF);

            tab(w, depth + indent + 1).append("// support for breaking for loops").append(CRLF);

            tab(w, depth + indent).append("}").append(CRLF);
        } else if (unit instanceof BreakStatement) {
            tab(w, depth + indent).append("__internal.throwBreakException();").append(CRLF);
        } else if (unit instanceof ContinueStatement) {
            tab(w, depth + indent).append("__internal.throwContinueException();").append(CRLF);
        }
        //log.info(" src (@ {}): [{}]", unit.getSourceRef(), unit.getSourceRef().getConsoleFriendlyText());
    }

    // end of render()
    tab(w, indent).append("}").append(CRLF);

    indent--;

    // end of template class
    tab(w, indent).append("}").append(CRLF);

    // Generate class with all gathered consumer interfaces for all withblocks
    withStatementConsumerGenerator.generate(this, w);

    if (this.plainTextStrategy == PlainTextStrategy.STATIC_BYTE_ARRAYS_VIA_UNLOADED_CLASS
            && !plainTextMap.isEmpty()) {

        w.append(CRLF);

        tab(w, indent).append("private static class PlainText {").append(CRLF);
        w.append(CRLF);

        for (String plainText : plainTextMap.keySet()) {

            for (Map.Entry<String, String> chunk : plainTextMap.get(plainText).entrySet()) {

                tab(w, indent + 1).append("static private final String ").append(chunk.getKey()).append(" = \"")
                        .append(StringEscapeUtils.escapeJava(chunk.getValue())).append("\";").append(CRLF);

            }

        }

        w.append(CRLF);
        tab(w, indent).append("}").append(CRLF);
    }

    w.append(CRLF);
    w.append("}").append(CRLF);
}