Example usage for com.google.gson JsonObject has

List of usage examples for com.google.gson JsonObject has

Introduction

In this page you can find the example usage for com.google.gson JsonObject has.

Prototype

public boolean has(String memberName) 

Source Link

Document

Convenience method to check if a member with the specified name is present in this object.

Usage

From source file:com.gst.infrastructure.core.serialization.JsonParserHelper.java

License:Apache License

private Locale extractLocaleValue(final JsonObject object) {
    Locale clientApplicationLocale = null;
    String locale = null;/*from w  w  w.ja va 2 s.  c  om*/
    if (object.has("locale") && object.get("locale").isJsonPrimitive()) {
        final JsonPrimitive primitive = object.get("locale").getAsJsonPrimitive();
        locale = primitive.getAsString();
        clientApplicationLocale = localeFromString(locale);
    }
    return clientApplicationLocale;
}

From source file:com.gst.infrastructure.dataqueries.service.ReadWriteNonCoreDataServiceImpl.java

License:Apache License

private void parseDatatableColumnObjectForCreate(final JsonObject column, StringBuilder sqlBuilder,
        final StringBuilder constrainBuilder, final String dataTableNameAlias,
        final Map<String, Long> codeMappings, final boolean isConstraintApproach) {

    String name = (column.has("name")) ? column.get("name").getAsString() : null;
    final String type = (column.has("type")) ? column.get("type").getAsString().toLowerCase() : null;
    final Integer length = (column.has("length")) ? column.get("length").getAsInt() : null;
    final Boolean mandatory = (column.has("mandatory")) ? column.get("mandatory").getAsBoolean() : false;
    final String code = (column.has("code")) ? column.get("code").getAsString() : null;

    if (StringUtils.isNotBlank(code)) {
        if (isConstraintApproach) {
            codeMappings.put(dataTableNameAlias + "_" + name,
                    this.codeReadPlatformService.retriveCode(code).getCodeId());
            constrainBuilder.append(", CONSTRAINT `fk_").append(dataTableNameAlias).append("_").append(name)
                    .append("` ").append("FOREIGN KEY (`" + name + "`) ").append("REFERENCES `")
                    .append(CODE_VALUES_TABLE).append("` (`id`)");
        } else {/* w w  w.  ja  v a  2s.  c o m*/
            name = datatableColumnNameToCodeValueName(name, code);
        }
    }

    final String mysqlType = apiTypeToMySQL.get(type);
    sqlBuilder = sqlBuilder.append("`" + name + "` " + mysqlType);

    if (type != null) {
        if (type.equalsIgnoreCase("String")) {
            sqlBuilder = sqlBuilder.append("(" + length + ")");
        } else if (type.equalsIgnoreCase("Decimal")) {
            sqlBuilder = sqlBuilder.append("(19,6)");
        } else if (type.equalsIgnoreCase("Dropdown")) {
            sqlBuilder = sqlBuilder.append("(11)");
        }
    }

    if (mandatory) {
        sqlBuilder = sqlBuilder.append(" NOT NULL");
    } else {
        sqlBuilder = sqlBuilder.append(" DEFAULT NULL");
    }

    sqlBuilder = sqlBuilder.append(", ");
}

From source file:com.gst.infrastructure.dataqueries.service.ReadWriteNonCoreDataServiceImpl.java

License:Apache License

private void parseDatatableColumnForUpdate(final JsonObject column,
        final Map<String, ResultsetColumnHeaderData> mapColumnNameDefinition, StringBuilder sqlBuilder,
        final String datatableName, final StringBuilder constrainBuilder, final Map<String, Long> codeMappings,
        final List<String> removeMappings, final boolean isConstraintApproach) {

    String name = (column.has("name")) ? column.get("name").getAsString() : null;
    final String lengthStr = (column.has("length")) ? column.get("length").getAsString() : null;
    Integer length = (StringUtils.isNotBlank(lengthStr)) ? Integer.parseInt(lengthStr) : null;
    String newName = (column.has("newName")) ? column.get("newName").getAsString() : name;
    final Boolean mandatory = (column.has("mandatory")) ? column.get("mandatory").getAsBoolean() : false;
    final String after = (column.has("after")) ? column.get("after").getAsString() : null;
    final String code = (column.has("code")) ? column.get("code").getAsString() : null;
    final String newCode = (column.has("newCode")) ? column.get("newCode").getAsString() : null;
    final String dataTableNameAlias = datatableName.toLowerCase().replaceAll("\\s", "_");
    if (isConstraintApproach) {
        if (StringUtils.isBlank(newName)) {
            newName = name;//from  w  w  w . java 2 s. c o  m
        }
        if (!StringUtils.equalsIgnoreCase(code, newCode) || !StringUtils.equalsIgnoreCase(name, newName)) {
            if (StringUtils.equalsIgnoreCase(code, newCode)) {
                final int codeId = getCodeIdForColumn(dataTableNameAlias, name);
                if (codeId > 0) {
                    removeMappings.add(dataTableNameAlias + "_" + name);
                    constrainBuilder.append(", DROP FOREIGN KEY `fk_").append(dataTableNameAlias).append("_")
                            .append(name).append("` ");
                    codeMappings.put(dataTableNameAlias + "_" + newName, (long) codeId);
                    constrainBuilder.append(",ADD CONSTRAINT  `fk_").append(dataTableNameAlias).append("_")
                            .append(newName).append("` ").append("FOREIGN KEY (`" + newName + "`) ")
                            .append("REFERENCES `").append(CODE_VALUES_TABLE).append("` (`id`)");
                }

            } else {
                if (code != null) {
                    removeMappings.add(dataTableNameAlias + "_" + name);
                    if (newCode == null || !StringUtils.equalsIgnoreCase(name, newName)) {
                        constrainBuilder.append(", DROP FOREIGN KEY `fk_").append(dataTableNameAlias)
                                .append("_").append(name).append("` ");
                    }
                }
                if (newCode != null) {
                    codeMappings.put(dataTableNameAlias + "_" + newName,
                            this.codeReadPlatformService.retriveCode(newCode).getCodeId());
                    if (code == null || !StringUtils.equalsIgnoreCase(name, newName)) {
                        constrainBuilder.append(",ADD CONSTRAINT  `fk_").append(dataTableNameAlias).append("_")
                                .append(newName).append("` ").append("FOREIGN KEY (`" + newName + "`) ")
                                .append("REFERENCES `").append(CODE_VALUES_TABLE).append("` (`id`)");
                    }
                }
            }
        }
    } else {
        if (StringUtils.isNotBlank(code)) {
            name = datatableColumnNameToCodeValueName(name, code);
            if (StringUtils.isNotBlank(newCode)) {
                newName = datatableColumnNameToCodeValueName(newName, newCode);
            } else {
                newName = datatableColumnNameToCodeValueName(newName, code);
            }
        }
    }
    if (!mapColumnNameDefinition.containsKey(name)) {
        throw new PlatformDataIntegrityException("error.msg.datatable.column.missing.update.parse",
                "Column " + name + " does not exist.", name);
    }
    final String type = mapColumnNameDefinition.get(name).getColumnType();
    if (length == null && type.toLowerCase().equals("varchar")) {
        length = mapColumnNameDefinition.get(name).getColumnLength().intValue();
    }

    sqlBuilder = sqlBuilder.append(", CHANGE `" + name + "` `" + newName + "` " + type);
    if (length != null && length > 0) {
        if (type.toLowerCase().equals("decimal")) {
            sqlBuilder.append("(19,6)");
        } else if (type.toLowerCase().equals("varchar")) {
            sqlBuilder.append("(" + length + ")");
        }
    }

    if (mandatory) {
        sqlBuilder = sqlBuilder.append(" NOT NULL");
    } else {
        sqlBuilder = sqlBuilder.append(" DEFAULT NULL");
    }

    if (after != null) {
        sqlBuilder = sqlBuilder.append(" AFTER `" + after + "`");
    }
}

From source file:com.gst.infrastructure.dataqueries.service.ReadWriteNonCoreDataServiceImpl.java

License:Apache License

private void parseDatatableColumnForAdd(final JsonObject column, StringBuilder sqlBuilder,
        final String dataTableNameAlias, final StringBuilder constrainBuilder,
        final Map<String, Long> codeMappings, final boolean isConstraintApproach) {

    String name = (column.has("name")) ? column.get("name").getAsString() : null;
    final String type = (column.has("type")) ? column.get("type").getAsString().toLowerCase() : null;
    final Integer length = (column.has("length")) ? column.get("length").getAsInt() : null;
    final Boolean mandatory = (column.has("mandatory")) ? column.get("mandatory").getAsBoolean() : false;
    final String after = (column.has("after")) ? column.get("after").getAsString() : null;
    final String code = (column.has("code")) ? column.get("code").getAsString() : null;

    if (StringUtils.isNotBlank(code)) {
        if (isConstraintApproach) {
            codeMappings.put(dataTableNameAlias + "_" + name,
                    this.codeReadPlatformService.retriveCode(code).getCodeId());
            constrainBuilder.append(",ADD CONSTRAINT  `fk_").append(dataTableNameAlias).append("_").append(name)
                    .append("` ").append("FOREIGN KEY (`" + name + "`) ").append("REFERENCES `")
                    .append(CODE_VALUES_TABLE).append("` (`id`)");
        } else {//w w w.j  av  a  2s .  c  o  m
            name = datatableColumnNameToCodeValueName(name, code);
        }
    }

    final String mysqlType = apiTypeToMySQL.get(type);
    sqlBuilder = sqlBuilder.append(", ADD `" + name + "` " + mysqlType);

    if (type != null) {
        if (type.equalsIgnoreCase("String") && length != null) {
            sqlBuilder = sqlBuilder.append("(" + length + ")");
        } else if (type.equalsIgnoreCase("Decimal")) {
            sqlBuilder = sqlBuilder.append("(19,6)");
        } else if (type.equalsIgnoreCase("Dropdown")) {
            sqlBuilder = sqlBuilder.append("(11)");
        }
    }

    if (mandatory) {
        sqlBuilder = sqlBuilder.append(" NOT NULL");
    } else {
        sqlBuilder = sqlBuilder.append(" DEFAULT NULL");
    }

    if (after != null) {
        sqlBuilder = sqlBuilder.append(" AFTER `" + after + "`");
    }
}

From source file:com.gst.infrastructure.dataqueries.service.ReadWriteNonCoreDataServiceImpl.java

License:Apache License

private void parseDatatableColumnForDrop(final JsonObject column, StringBuilder sqlBuilder,
        final String datatableName, final StringBuilder constrainBuilder, final List<String> codeMappings) {
    final String datatableAlias = datatableName.toLowerCase().replaceAll("\\s", "_");
    final String name = (column.has("name")) ? column.get("name").getAsString() : null;
    sqlBuilder = sqlBuilder.append(", DROP COLUMN `" + name + "`");
    final StringBuilder findFKSql = new StringBuilder();
    findFKSql.append("SELECT count(*)").append("FROM information_schema.TABLE_CONSTRAINTS i")
            .append(" WHERE i.CONSTRAINT_TYPE = 'FOREIGN KEY'").append(" AND i.TABLE_SCHEMA = DATABASE()")
            .append(" AND i.TABLE_NAME = '").append(datatableName).append("' AND i.CONSTRAINT_NAME = 'fk_")
            .append(datatableAlias).append("_").append(name).append("' ");
    @SuppressWarnings("deprecation")
    final int count = this.jdbcTemplate.queryForInt(findFKSql.toString());
    if (count > 0) {
        codeMappings.add(datatableAlias + "_" + name);
        constrainBuilder.append(", DROP FOREIGN KEY `fk_").append(datatableAlias).append("_").append(name)
                .append("` ");
    }//from w  w w . ja  v a 2  s  .co m
}

From source file:com.gst.infrastructure.dataqueries.service.ReadWriteNonCoreDataServiceImpl.java

License:Apache License

/**
 * Update data table, set column value to empty string where current value
 * is NULL. Run update SQL only if the "mandatory" property is set to true
 * /* w ww  .jav a2  s. co  m*/
 * @param datatableName
 *            Name of data table
 * @param column
 *            JSON encoded array of column properties
 * @see https://mifosforge.jira.com/browse/MIFOSX-1145
 **/
private void removeNullValuesFromStringColumn(final String datatableName, final JsonObject column,
        final Map<String, ResultsetColumnHeaderData> mapColumnNameDefinition) {
    final Boolean mandatory = (column.has("mandatory")) ? column.get("mandatory").getAsBoolean() : false;
    final String name = (column.has("name")) ? column.get("name").getAsString() : "";
    final String type = (mapColumnNameDefinition.containsKey(name))
            ? mapColumnNameDefinition.get(name).getColumnType()
            : "";

    if (StringUtils.isNotEmpty(type)) {
        if (mandatory && stringDataTypes.contains(type.toLowerCase())) {
            StringBuilder sqlBuilder = new StringBuilder();
            sqlBuilder.append(
                    "UPDATE `" + datatableName + "` SET `" + name + "` = '' WHERE `" + name + "` IS NULL");

            this.jdbcTemplate.update(sqlBuilder.toString());
        }
    }
}

From source file:com.gst.infrastructure.dataqueries.service.ReportWritePlatformServiceImpl.java

License:Apache License

private Set<ReportParameterUsage> assembleSetOfReportParameterUsages(final Report report,
        final JsonCommand command) {

    Set<ReportParameterUsage> reportParameterUsages = null;

    if (command.parameterExists("reportParameters")) {
        final JsonArray reportParametersArray = command.arrayOfParameterNamed("reportParameters");
        if (reportParametersArray != null) {

            reportParameterUsages = new HashSet<>();

            for (int i = 0; i < reportParametersArray.size(); i++) {

                final JsonObject jsonObject = reportParametersArray.get(i).getAsJsonObject();

                Long id = null;/*  w ww. j  av  a2  s .  com*/
                ReportParameterUsage reportParameterUsageItem = null;
                ReportParameter reportParameter = null;
                String reportParameterName = null;

                if (jsonObject.has("id")) {
                    final String idStr = jsonObject.get("id").getAsString();
                    if (StringUtils.isNotBlank(idStr)) {
                        id = Long.parseLong(idStr);
                    }
                }

                if (id != null) {
                    // existing report parameter usage
                    reportParameterUsageItem = this.reportParameterUsageRepository.findOne(id);
                    if (reportParameterUsageItem == null) {
                        throw new ReportParameterNotFoundException(id);
                    }

                    // check parameter
                    if (jsonObject.has("parameterId")) {
                        final Long parameterId = jsonObject.get("parameterId").getAsLong();
                        reportParameter = this.reportParameterRepository.findOne(parameterId);
                        if (reportParameter == null
                                || !reportParameterUsageItem.hasParameterIdOf(parameterId)) {
                            //
                            throw new ReportParameterNotFoundException(parameterId);
                        }
                    }

                    if (jsonObject.has("reportParameterName")) {
                        reportParameterName = jsonObject.get("reportParameterName").getAsString();
                        reportParameterUsageItem.updateParameterName(reportParameterName);
                    }
                } else {
                    // new report parameter usage
                    if (jsonObject.has("parameterId")) {
                        final Long parameterId = jsonObject.get("parameterId").getAsLong();
                        reportParameter = this.reportParameterRepository.findOne(parameterId);
                        if (reportParameter == null) {
                            throw new ReportParameterNotFoundException(parameterId);
                        }
                    } else {
                        throw new PlatformDataIntegrityException(
                                "error.msg.parameter.id.mandatory.in.report.parameter",
                                "parameterId column is mandatory in Report Parameter Entry");
                    }

                    if (jsonObject.has("reportParameterName")) {
                        reportParameterName = jsonObject.get("reportParameterName").getAsString();
                    }

                    reportParameterUsageItem = new ReportParameterUsage(report, reportParameter,
                            reportParameterName);
                }

                reportParameterUsages.add(reportParameterUsageItem);
            }
        }
    }

    return reportParameterUsages;
}

From source file:com.gst.organisation.holiday.data.HolidayDataValidator.java

License:Apache License

public void validateForCreate(final String json) {

    if (StringUtils.isBlank(json)) {
        throw new InvalidJsonException();
    }/*  ww w  . j  ava  2  s  . c  om*/

    final Type typeOfMap = new TypeToken<Map<String, Object>>() {
    }.getType();
    this.fromApiJsonHelper.checkForUnsupportedParameters(typeOfMap, json,
            HolidayApiConstants.HOLIDAY_CREATE_OR_UPDATE_REQUEST_DATA_PARAMETERS);
    final JsonElement element = this.fromApiJsonHelper.parse(json);

    final List<ApiParameterError> dataValidationErrors = new ArrayList<>();

    final DataValidatorBuilder baseDataValidator = new DataValidatorBuilder(dataValidationErrors)
            .resource(HolidayApiConstants.HOLIDAY_RESOURCE_NAME);

    final String name = this.fromApiJsonHelper.extractStringNamed(HolidayApiConstants.nameParamName, element);
    baseDataValidator.reset().parameter(HolidayApiConstants.nameParamName).value(name).notNull()
            .notExceedingLengthOf(100);

    final LocalDate fromDate = this.fromApiJsonHelper
            .extractLocalDateNamed(HolidayApiConstants.fromDateParamName, element);
    baseDataValidator.reset().parameter(HolidayApiConstants.fromDateParamName).value(fromDate).notNull();

    final LocalDate toDate = this.fromApiJsonHelper.extractLocalDateNamed(HolidayApiConstants.toDateParamName,
            element);
    baseDataValidator.reset().parameter(HolidayApiConstants.toDateParamName).value(toDate).notNull();

    final LocalDate repaymentsRescheduledTo = this.fromApiJsonHelper
            .extractLocalDateNamed(HolidayApiConstants.repaymentsRescheduledToParamName, element);
    baseDataValidator.reset().parameter(HolidayApiConstants.repaymentsRescheduledToParamName)
            .value(repaymentsRescheduledTo).notNull();

    Set<Long> offices = null;
    final JsonObject topLevelJsonElement = element.getAsJsonObject();

    if (topLevelJsonElement.has(HolidayApiConstants.officesParamName)
            && topLevelJsonElement.get(HolidayApiConstants.officesParamName).isJsonArray()) {

        final JsonArray array = topLevelJsonElement.get(HolidayApiConstants.officesParamName).getAsJsonArray();
        if (array.size() > 0) {
            offices = new HashSet<>(array.size());
            for (int i = 0; i < array.size(); i++) {
                final JsonObject officeElement = array.get(i).getAsJsonObject();
                final Long officeId = this.fromApiJsonHelper
                        .extractLongNamed(HolidayApiConstants.officeIdParamName, officeElement);
                baseDataValidator.reset().parameter(HolidayApiConstants.officesParamName).value(officeId)
                        .notNull();
                offices.add(officeId);
            }
        }
    }
    baseDataValidator.reset().parameter(HolidayApiConstants.officesParamName).value(offices).notNull();
    throwExceptionIfValidationWarningsExist(dataValidationErrors);
}

From source file:com.gst.organisation.holiday.data.HolidayDataValidator.java

License:Apache License

public void validateForUpdate(final String json) {

    if (StringUtils.isBlank(json)) {
        throw new InvalidJsonException();
    }//from  w w  w  .j av a 2  s  . c  om

    final Type typeOfMap = new TypeToken<Map<String, Object>>() {
    }.getType();
    this.fromApiJsonHelper.checkForUnsupportedParameters(typeOfMap, json,
            HolidayApiConstants.HOLIDAY_CREATE_OR_UPDATE_REQUEST_DATA_PARAMETERS);
    final JsonElement element = this.fromApiJsonHelper.parse(json);

    final List<ApiParameterError> dataValidationErrors = new ArrayList<>();

    final DataValidatorBuilder baseDataValidator = new DataValidatorBuilder(dataValidationErrors)
            .resource(HolidayApiConstants.HOLIDAY_RESOURCE_NAME);

    if (this.fromApiJsonHelper.parameterExists(HolidayApiConstants.nameParamName, element)) {
        final String name = this.fromApiJsonHelper.extractStringNamed(HolidayApiConstants.nameParamName,
                element);
        baseDataValidator.reset().parameter(HolidayApiConstants.nameParamName).value(name).notNull()
                .notExceedingLengthOf(100);
    }

    if (this.fromApiJsonHelper.parameterExists(HolidayApiConstants.fromDateParamName, element)) {
        final LocalDate fromDate = this.fromApiJsonHelper
                .extractLocalDateNamed(HolidayApiConstants.fromDateParamName, element);
        baseDataValidator.reset().parameter(HolidayApiConstants.fromDateParamName).value(fromDate).notNull();
    }

    if (this.fromApiJsonHelper.parameterExists(HolidayApiConstants.toDateParamName, element)) {
        final LocalDate toDate = this.fromApiJsonHelper
                .extractLocalDateNamed(HolidayApiConstants.toDateParamName, element);
        baseDataValidator.reset().parameter(HolidayApiConstants.toDateParamName).value(toDate).notNull();
    }

    if (this.fromApiJsonHelper.parameterExists(HolidayApiConstants.repaymentsRescheduledToParamName, element)) {
        final LocalDate repaymentsRescheduledTo = this.fromApiJsonHelper
                .extractLocalDateNamed(HolidayApiConstants.repaymentsRescheduledToParamName, element);
        baseDataValidator.reset().parameter(HolidayApiConstants.repaymentsRescheduledToParamName)
                .value(repaymentsRescheduledTo).notNull();
    }

    Set<Long> offices = null;
    final JsonObject topLevelJsonElement = element.getAsJsonObject();
    if (this.fromApiJsonHelper.parameterExists(HolidayApiConstants.officesParamName, element)) {
        if (topLevelJsonElement.has(HolidayApiConstants.officesParamName)
                && topLevelJsonElement.get(HolidayApiConstants.officesParamName).isJsonArray()) {

            final JsonArray array = topLevelJsonElement.get(HolidayApiConstants.officesParamName)
                    .getAsJsonArray();
            if (array.size() > 0) {
                offices = new HashSet<>(array.size());
                for (int i = 0; i < array.size(); i++) {
                    final JsonObject officeElement = array.get(i).getAsJsonObject();
                    final Long officeId = this.fromApiJsonHelper
                            .extractLongNamed(HolidayApiConstants.officeIdParamName, officeElement);
                    baseDataValidator.reset().parameter(HolidayApiConstants.officesParamName).value(officeId)
                            .notNull();
                    offices.add(officeId);
                }
            }
        }
        baseDataValidator.reset().parameter(HolidayApiConstants.officesParamName).value(offices).notNull();
        throwExceptionIfValidationWarningsExist(dataValidationErrors);
    }
}

From source file:com.gst.organisation.holiday.service.HolidayWritePlatformServiceJpaRepositoryImpl.java

License:Apache License

private Set<Office> getSelectedOffices(final JsonCommand command) {
    Set<Office> offices = null;
    final JsonObject topLevelJsonElement = this.fromApiJsonHelper.parse(command.json()).getAsJsonObject();
    if (topLevelJsonElement.has(HolidayApiConstants.officesParamName)
            && topLevelJsonElement.get(HolidayApiConstants.officesParamName).isJsonArray()) {

        final JsonArray array = topLevelJsonElement.get(HolidayApiConstants.officesParamName).getAsJsonArray();
        offices = new HashSet<>(array.size());
        for (int i = 0; i < array.size(); i++) {
            final JsonObject officeElement = array.get(i).getAsJsonObject();
            final Long officeId = this.fromApiJsonHelper.extractLongNamed(HolidayApiConstants.officeIdParamName,
                    officeElement);/*from w  ww.j a v  a  2s.c  o  m*/
            final Office office = this.officeRepositoryWrapper.findOneWithNotFoundDetection(officeId);
            offices.add(office);
        }
    }
    return offices;
}