public ContentConstraintBean buildConstraint()

in SdmxStructureParser/src/main/java/org/sdmxsource/sdmx/structureparser/builder/constraint/ConstraintBuilderImpl.java [52:156]


    public ContentConstraintBean buildConstraint(DataReaderEngine dre,
                                                 StructureReferenceBean attachment,
                                                 DataSourceBean dsAttachement,
                                                 boolean indexAttributes, boolean indexDataset,
                                                 boolean indexReportingPeriod, boolean indexTimeSeries,
                                                 boolean definingDataPresent, MaintainableRefBean refParams) {

        dre.reset();
        //TODO Should it check if there is more then one dataset in the datasource
        if (!dre.moveNextDataset()) {
            throw new SdmxSemmanticException("Can not index time series for registered datasource, the data retrieved from the datasource does not contain a dataset");
        }

        if (!indexAttributes && !indexDataset && !indexReportingPeriod && !indexTimeSeries) {
            return null;
        }

        DatasetHeaderBean header = dre.getCurrentDatasetHeaderBean();
        if (indexTimeSeries && !header.isTimeSeries()) {
            throw new SdmxSemmanticException("Can not index time series for registered datasource, the data retrieved from the datasource is not time series");
        }

        //Create mutable Maintainable
        ContentConstraintMutableBean mutableBean = new ContentConstraintMutableBeanImpl();
        mutableBean.setAgencyId(refParams.getAgencyId());
        mutableBean.setId(refParams.getMaintainableId());
        mutableBean.setVersion(refParams.getVersion());
        mutableBean.addName("en", "Generated Constraint");
        mutableBean.addDescription("en", "Constraint built from dataset");
        mutableBean.setIsDefiningActualDataPresent(true);
        mutableBean.setConstraintAttachment(buildAttachement(attachment, dsAttachement));

        ConstraintDataKeySetMutableBean dataKeySet = null;

        Map<String, Set<String>> cubeRegionMap = new HashMap<String, Set<String>>();
        Map<String, Set<String>> attributeMap = new HashMap<String, Set<String>>();
        Date reportFrom = null;
        Date reportTo = null;
        Set<String> processedDates = new HashSet<String>();

        while (dre.moveNextKeyable()) {
            Keyable key = dre.getCurrentKey();
            if (key.isSeries()) {
                //TODO Check if Cross Sectional and put out exception if it is
                //1. If indexing the time series store the time Series Key on the Constraint
                if (indexTimeSeries) {
                    if (dataKeySet == null) {
                        dataKeySet = new ConstraintDataKeySetMutableBeanImpl();
                        mutableBean.setIncludedSeriesKeys(dataKeySet);
                    }
                    ConstrainedDataKeyMutableBean dataKey = new ConstrainedDataKeyMutableBeanImpl();
                    dataKey.setKeyValues(key.getKey());
                    dataKeySet.addConstrainedDataKey(dataKey);
                }
                //2. If indexing the dataset, store the individual code values
                if (indexDataset) {
                    storeKeyValuesOnMap(key.getKey(), cubeRegionMap);
                }
                //3. If indexing attributes, store the individual code values for each attribute
                if (indexAttributes) {
                    storeKeyValuesOnMap(key.getAttributes(), attributeMap);
                }
            }
            if (indexAttributes || indexReportingPeriod) {
                while (dre.moveNextObservation()) {
                    Observation obs = dre.getCurrentObservation();
                    //If indexing the dates, determine the data start and end dates from the obs dates
                    //To save time, do not process the same date twice
                    if (indexReportingPeriod) {
                        if (!processedDates.contains(obs.getObsTime())) {
                            Date obsDate = obs.getObsAsTimeDate();
                            if (reportFrom == null || reportFrom.getTime() > obsDate.getTime()) {
                                reportFrom = obsDate;
                            }
                            if (reportTo == null || reportTo.getTime() < obsDate.getTime()) {
                                reportTo = obsDate;
                            }
                            processedDates.add(obs.getObsTime());
                        }
                    }
                    if (indexAttributes) {
                        storeKeyValuesOnMap(obs.getAttributes(), attributeMap);
                    }
                }
            }
        }
        if (indexAttributes || indexDataset) {
            CubeRegionMutableBean cubeRegionMutableBean = new CubeRegionMutableBeanImpl();
            mutableBean.setIncludedCubeRegion(cubeRegionMutableBean);
            if (indexAttributes) {
                createKeyValues(attributeMap, cubeRegionMutableBean.getAttributeValues());
            }
            if (indexDataset) {
                createKeyValues(cubeRegionMap, cubeRegionMutableBean.getKeyValues());
            }
        }
        if (indexReportingPeriod && reportFrom != null && reportTo != null) {
            ReferencePeriodMutableBean refPeriodMutable = new ReferencePeriodMutableBeanImpl();
            refPeriodMutable.setEndTime(reportTo);
            refPeriodMutable.setStartTime(reportFrom);
            mutableBean.setReferencePeriod(refPeriodMutable);
        }

        return mutableBean.getImmutableInstance();
    }