private List<FileData> recursiveShallowExport(List<FileData> files, CTLSchemaDto parent) throws Exception { files.add(this.shallowExport(parent)); ObjectNode object = new ObjectMapper().readValue(parent.getBody(), ObjectNode.class); ArrayNode dependencies = (ArrayNode) object.get(DEPENDENCIES); if (dependencies != null) { for (JsonNode node : dependencies) { ObjectNode dependency = (ObjectNode) node; String fqn = dependency.get(FQN).getTextValue(); Integer version = dependency.get(VERSION).getIntValue(); CTLSchemaDto child = this.findAnyCtlSchemaByFqnAndVerAndTenantIdAndApplicationId( fqn, version, parent.getMetaInfo().getTenantId(), parent.getMetaInfo().getApplicationId()); Validate.notNull(child, MessageFormat.format("The dependency [{0}] was not found!", fqn)); this.recursiveShallowExport(files, child); } } return files; } }
CtlSchemaFormDto toCtlSchemaForm(CTLSchemaDto ctlSchema, ConverterType converterType) throws KaaAdminServiceException { try { CtlSchemaFormDto ctlSchemaForm = new CtlSchemaFormDto(ctlSchema); SchemaFormAvroConverter converter = getCtlSchemaConverterForScope( ctlSchemaForm.getMetaInfo().getTenantId(), ctlSchemaForm.getMetaInfo().getApplicationId(), converterType); RecordField form = converter.createSchemaFormFromSchema(ctlSchema.getBody()); ctlSchemaForm.setSchema(form); List<Integer> availableVersions = controlService.getAllCtlSchemaVersionsByFqnTenantIdAndApplicationId( ctlSchema.getMetaInfo().getFqn(), ctlSchema.getMetaInfo().getTenantId(), ctlSchema.getMetaInfo().getApplicationId()); availableVersions = availableVersions == null ? Collections.<Integer>emptyList() : availableVersions; Collections.sort(availableVersions); ctlSchemaForm.getMetaInfo().setVersions(availableVersions); return ctlSchemaForm; } catch (Exception cause) { throw Utils.handleException(cause); } }
/** * Create new instance of CTL schema meta info. * * @param dto data transfer object that used for creating new instance, * it hold id, full qualified name, tenant id and application id * of new instance */ public CtlSchemaMetaInfo(CtlSchemaMetaInfoDto dto) { this.id = ModelUtils.getLongId(dto.getId()); this.fqn = dto.getFqn(); Long tenantId = getLongId(dto.getTenantId()); this.tenant = tenantId != null ? new Tenant(tenantId) : null; Long appId = getLongId(dto.getApplicationId()); this.application = appId != null ? new Application(appId) : null; }
@Override public CTLSchemaDto getCtlSchemaById(String schemaId) throws KaaAdminServiceException { this.checkAuthority(KaaAuthorityDto.values()); try { this.checkCtlSchemaId(schemaId); CTLSchemaDto schemaFound = controlService.getCtlSchemaById(schemaId); Utils.checkNotNull(schemaFound); checkCtlSchemaReadScope( schemaFound.getMetaInfo().getTenantId(), schemaFound.getMetaInfo().getApplicationId()); return schemaFound; } catch (Exception cause) { throw Utils.handleException(cause); } }
@Override public CtlSchemaFormDto getLatestCtlSchemaForm(String metaInfoId) throws KaaAdminServiceException { checkAuthority(KaaAuthorityDto.values()); try { this.checkCtlSchemaMetaInfoId(metaInfoId); CTLSchemaDto ctlSchema = controlService.getLatestCtlSchemaByMetaInfoId(metaInfoId); Utils.checkNotNull(ctlSchema); checkCtlSchemaReadScope( ctlSchema.getMetaInfo().getTenantId(), ctlSchema.getMetaInfo().getApplicationId()); return toCtlSchemaForm(ctlSchema, ConverterType.FORM_AVRO_CONVERTER); } catch (Exception cause) { throw Utils.handleException(cause); } }
@Override public CtlSchemaMetaInfoDto updateCtlSchemaMetaInfoScope(CtlSchemaMetaInfoDto ctlSchemaMetaInfo) { validateObject(ctlSchemaMetaInfo, "Incorrect ctl schema meta info object"); LOG.debug("Update ctl schema meta info scope with id [{}]", ctlSchemaMetaInfo.getId()); CtlSchemaMetaInfo schemaMetaInfo = ctlSchemaMetaInfoDao.findById(ctlSchemaMetaInfo.getId()); if (schemaMetaInfo != null) { synchronized (this) { ctlSchemaMetaInfoDao.lockRequest(lockOptions).setScope(true).lock(schemaMetaInfo); if (checkScopeUpdate(ctlSchemaMetaInfo, schemaMetaInfo.toDto())) { List<CtlSchemaMetaInfo> others = ctlSchemaMetaInfoDao.findOthersByFqnAndTenantId( ctlSchemaMetaInfo.getFqn(), ctlSchemaMetaInfo.getTenantId(), ctlSchemaMetaInfo .getId()); if (others != null && !others.isEmpty()) { throw new DatabaseProcessingException("Can't update scope of the common type due to " + "an FQN conflict."); } schemaMetaInfo = ctlSchemaMetaInfoDao.updateScope( new CtlSchemaMetaInfo(ctlSchemaMetaInfo)); } return DaoUtil.getDto(schemaMetaInfo); } } else { throw new DatabaseProcessingException("Can't find common type by id."); } }
@Override public String prepareCtlSchemaExport(String ctlSchemaId, CTLSchemaExportMethod method) throws KaaAdminServiceException { checkAuthority(KaaAuthorityDto.values()); try { CTLSchemaDto schemaFound = controlService.getCtlSchemaById(ctlSchemaId); Utils.checkNotNull(schemaFound); checkCtlSchemaReadScope( schemaFound.getMetaInfo().getTenantId(), schemaFound.getMetaInfo().getApplicationId()); CtlSchemaExportKey key = new CtlSchemaExportKey(ctlSchemaId, method); return Base64.encodeObject(key, Base64.URL_SAFE); } catch (Exception ex) { throw Utils.handleException(ex); } }
@Test public void testFindSiblingsFqns() { List<CtlSchemaMetaInfoDto> siblingSchemas = ctlService.findSiblingsByFqnTenantIdAndApplicationId(appSchema.getMetaInfo().getFqn(), appSchema.getMetaInfo().getTenantId(), appSchema.getMetaInfo().getApplicationId()); Assert.assertNotNull(siblingSchemas); Assert.assertEquals(1, siblingSchemas.size()); Assert.assertEquals(app2Schema.getMetaInfo(), siblingSchemas.get(0)); }
@Override public CtlSchemaFormDto getCtlSchemaFormByMetaInfoIdAndVer(String metaInfoId, int version) throws KaaAdminServiceException { this.checkAuthority(KaaAuthorityDto.values()); try { this.checkCtlSchemaMetaInfoId(metaInfoId); this.checkCtlSchemaVersion(version); CTLSchemaDto schemaFound = controlService.getCtlSchemaByMetaInfoIdAndVer( metaInfoId, version); Utils.checkNotNull(schemaFound); checkCtlSchemaReadScope( schemaFound.getMetaInfo().getTenantId(), schemaFound.getMetaInfo().getApplicationId()); return toCtlSchemaForm(schemaFound, ConverterType.FORM_AVRO_CONVERTER); } catch (Exception cause) { throw Utils.handleException(cause); } }
@Test public void testFindCTLSchemaByFqnAndVerAndTenantIdAndApplicationId() { CtlSchemaMetaInfoDto metaInfo = firstSchema.getMetaInfo(); CTLSchemaDto found = ctlService.findCtlSchemaByFqnAndVerAndTenantIdAndApplicationId(metaInfo.getFqn(), firstSchema.getVersion(), metaInfo.getTenantId(), metaInfo.getApplicationId()); Assert.assertEquals(firstSchema, found); }
@Override public FileData exportCtlSchema(String fqn, int version, String applicationId, CTLSchemaExportMethod method) throws KaaAdminServiceException { try { this.checkCtlSchemaFqn(fqn); this.checkCtlSchemaVersion(version); String tenantId = getCurrentUser().getTenantId(); CTLSchemaDto schemaFound = controlService.getCtlSchemaByFqnVersionTenantIdAndApplicationId( fqn, version, tenantId, applicationId); Utils.checkNotNull(schemaFound); checkCtlSchemaReadScope( schemaFound.getMetaInfo().getTenantId(), schemaFound.getMetaInfo().getApplicationId()); switch (method) { case SHALLOW: return controlService.exportCtlSchemaShallow(schemaFound); case FLAT: return controlService.exportCtlSchemaFlat(schemaFound); case DEEP: return controlService.exportCtlSchemaDeep(schemaFound); default: throw new IllegalArgumentException( "The export method " + method.name() + " is not currently supported!"); } } catch (Exception cause) { throw Utils.handleException(cause); } }
@Test public void testScopeUpdate() { CtlSchemaMetaInfoDto metaInfo = appSchema3.getMetaInfo(); metaInfo.setApplicationId(null); ctlService.updateCtlSchemaMetaInfoScope(metaInfo); CTLSchemaDto found = ctlService.findCtlSchemaByFqnAndVerAndTenantIdAndApplicationId(metaInfo.getFqn(), appSchema3.getVersion(), metaInfo.getTenantId(), null); Assert.assertEquals(appSchema3, found); }
@Override public CTLSchemaDto getCtlSchemaByFqnVersionTenantIdAndApplicationId(String fqn, Integer version, String tenantId, String applicationId) throws KaaAdminServiceException { this.checkAuthority(KaaAuthorityDto.values()); try { this.checkCtlSchemaFqn(fqn); this.checkCtlSchemaVersion(version); if (!isEmpty(applicationId)) { this.checkApplicationId(applicationId); } CTLSchemaDto schemaFound = controlService.getCtlSchemaByFqnVersionTenantIdAndApplicationId( fqn, version, tenantId, applicationId); Utils.checkNotNull(schemaFound); checkCtlSchemaReadScope( schemaFound.getMetaInfo().getTenantId(), schemaFound.getMetaInfo().getApplicationId()); return schemaFound; } catch (Exception cause) { throw Utils.handleException(cause); } }
@Override public EventClassViewDto createEventClassFormCtlSchema(CtlSchemaFormDto ctlSchemaForm) throws KaaAdminServiceException { checkAuthority(KaaAuthorityDto.TENANT_ADMIN); try { checkTenantId(ctlSchemaForm.getMetaInfo().getTenantId()); EventClassDto eventClassDto = new EventClassDto(); eventClassDto.setName(ctlSchemaForm.getSchema().getDisplayNameFieldValue()); eventClassDto.setDescription(ctlSchemaForm.getSchema().getDescriptionFieldValue()); CtlSchemaFormDto savedCtlSchemaForm = ctlService.saveCtlSchemaForm( ctlSchemaForm, ConverterType.FORM_AVRO_CONVERTER); eventClassDto.setCtlSchemaId(savedCtlSchemaForm.getId()); return getEventClassViewByCtlSchemaId(eventClassDto); } catch (Exception ex) { throw Utils.handleException(ex); } }
@Test public void testFindByFqnAndVerAndTenantIdAndApplicationId() { CtlSchema found = ctlSchemaDao.findByFqnAndVerAndTenantIdAndApplicationId(firstSchema.getMetaInfo().getFqn(), firstSchema.getVersion(), firstSchema.getMetaInfo().getTenantId(), firstSchema.getMetaInfo().getApplicationId()); Assert.assertEquals(firstSchema, found.toDto()); }
@Test public void testRemoveCTLSchemaByFqnAndVerAndTenantIdAndApplicationId() { String schemaId = tenantSchema.getId(); ctlService.removeCtlSchemaByFqnAndVerAndTenantIdAndApplicationId(tenantSchema.getMetaInfo().getFqn(), tenantSchema.getVersion(), tenantSchema.getMetaInfo().getTenantId(), tenantSchema.getMetaInfo().getApplicationId()); Assert.assertNull(ctlService.findCtlSchemaById(schemaId)); }
@Test public void testRemoveCTLSchemaByFqnAndVerAndWithoutTenantId() { String schemaId = systemSchema.getId(); ctlService.removeCtlSchemaByFqnAndVerAndTenantIdAndApplicationId(systemSchema.getMetaInfo().getFqn(), systemSchema.getVersion(), systemSchema.getMetaInfo().getTenantId(), systemSchema.getMetaInfo().getApplicationId()); Assert.assertNull(ctlService.findCtlSchemaById(schemaId)); }
@Override public EventClassViewDto saveEventClassView(EventClassViewDto eventClassViewDto) throws KaaAdminServiceException { checkAuthority(KaaAuthorityDto.TENANT_ADMIN); try { EventClassDto eventClassDto = eventClassViewDto.getSchema(); String ctlSchemaId = eventClassDto.getCtlSchemaId(); CtlSchemaFormDto ctlSchemaForm = null; if (isEmpty(ctlSchemaId)) { if (eventClassViewDto.useExistingCtlSchema()) { CtlSchemaReferenceDto metaInfo = eventClassViewDto.getExistingMetaInfo(); CTLSchemaDto schema = controlService.getCtlSchemaByFqnVersionTenantIdAndApplicationId( metaInfo.getMetaInfo().getFqn(), metaInfo.getVersion(), metaInfo.getMetaInfo().getTenantId(), metaInfo.getMetaInfo().getApplicationId()); eventClassDto.setCtlSchemaId(schema.getId()); } else { ctlSchemaForm = ctlService.saveCtlSchemaForm( eventClassViewDto.getCtlSchemaForm(), ConverterType.FORM_AVRO_CONVERTER); eventClassDto.setCtlSchemaId(ctlSchemaForm.getId()); } } return getEventClassViewByCtlSchemaId(eventClassDto); } catch (Exception ex) { throw Utils.handleException(ex); } }
@Override public ProfileSchemaViewDto saveProfileSchemaView(ProfileSchemaViewDto profileSchemaView) throws KaaAdminServiceException { checkAuthority(KaaAuthorityDto.TENANT_DEVELOPER, KaaAuthorityDto.TENANT_USER); try { EndpointProfileSchemaDto profileSchema = profileSchemaView.getSchema(); String applicationId = profileSchema.getApplicationId(); checkApplicationId(applicationId); String ctlSchemaId = profileSchema.getCtlSchemaId(); if (isEmpty(ctlSchemaId)) { if (profileSchemaView.useExistingCtlSchema()) { CtlSchemaReferenceDto metaInfo = profileSchemaView.getExistingMetaInfo(); CTLSchemaDto schema = ctlService.getCtlSchemaByFqnVersionTenantIdAndApplicationId( metaInfo.getMetaInfo().getFqn(), metaInfo.getVersion(), metaInfo.getMetaInfo().getTenantId(), metaInfo.getMetaInfo().getApplicationId()); profileSchema.setCtlSchemaId(schema.getId()); } else { CtlSchemaFormDto ctlSchemaForm = ctlService.saveCtlSchemaForm( profileSchemaView.getCtlSchemaForm(), ConverterType.FORM_AVRO_CONVERTER); profileSchema.setCtlSchemaId(ctlSchemaForm.getId()); } } EndpointProfileSchemaDto savedProfileSchema = saveProfileSchema(profileSchema); return getProfileSchemaView(savedProfileSchema.getId()); } catch (Exception ex) { throw Utils.handleException(ex); } }
@Override public NotificationSchemaViewDto saveNotificationSchemaView( NotificationSchemaViewDto notificationSchemaView) throws KaaAdminServiceException { checkAuthority(KaaAuthorityDto.TENANT_DEVELOPER, KaaAuthorityDto.TENANT_USER); try { NotificationSchemaDto notificationSchema = notificationSchemaView.getSchema(); String applicationId = notificationSchema.getApplicationId(); checkApplicationId(applicationId); String ctlSchemaId = notificationSchema.getCtlSchemaId(); if (isEmpty(ctlSchemaId)) { if (notificationSchemaView.useExistingCtlSchema()) { CtlSchemaReferenceDto metaInfo = notificationSchemaView.getExistingMetaInfo(); CTLSchemaDto schema = ctlService.getCtlSchemaByFqnVersionTenantIdAndApplicationId( metaInfo.getMetaInfo().getFqn(), metaInfo.getVersion(), metaInfo.getMetaInfo().getTenantId(), metaInfo.getMetaInfo().getApplicationId()); notificationSchema.setCtlSchemaId(schema.getId()); } else { CtlSchemaFormDto ctlSchemaForm = ctlService.saveCtlSchemaForm( notificationSchemaView.getCtlSchemaForm(), ConverterType.FORM_AVRO_CONVERTER); notificationSchema.setCtlSchemaId(ctlSchemaForm.getId()); } } NotificationSchemaDto savedNotificationSchema = saveNotificationSchema(notificationSchema); return getNotificationSchemaView(savedNotificationSchema.getId()); } catch (Exception ex) { throw Utils.handleException(ex); } }