initial public commit

This commit is contained in:
2021-09-06 17:46:59 +03:00
commit b744b08829
824 changed files with 91593 additions and 0 deletions

View File

@ -0,0 +1,360 @@
/*-
* ~~~~~~licensing~~~~~~
* system-impl
* ==========
* Copyright (C) 2020 - 2021 EmDev LLC
* ==========
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
* ~~~~~~/licensing~~~~~~
*/
package ru.entaxy.esb.system.jpa.impl;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.query.Query;
import ru.entaxy.esb.system.jpa.SystemService;
import ru.entaxy.esb.system.jpa.entity.ExportAllowed;
import ru.entaxy.esb.system.jpa.entity.System;
import ru.entaxy.esb.system.management.bundle.jpa.entity.BundleEntity;
import javax.persistence.EntityGraph;
import javax.persistence.TypedQuery;
import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Root;
import java.util.Date;
import java.util.List;
import java.util.Optional;
import java.util.Set;
public class SystemServiceImpl implements SystemService {
private static final String JAVAX_PERSISTENCE_LOADGRAPH = "javax.persistence.loadgraph";
private static final String SYSTEM_ENTITY_GRAPH = "system-entity-graph";
private SessionFactory sessionFactory;
public void setSessionFactory(SessionFactory sessionFactory) {
this.sessionFactory = sessionFactory;
}
public Session getSession() {
return sessionFactory.openSession();
}
@Override
public List<System> getSystemList() {
List<System> list;
try (Session session = getSession()) {
session.getTransaction().begin();
CriteriaQuery<System> criteriaQuery = session.getCriteriaBuilder().createQuery(System.class);
Root<System> root = criteriaQuery.from(System.class);
criteriaQuery.select(root);
list = session.createQuery(criteriaQuery).getResultList();
session.getTransaction().commit();
}
return list;
}
@Override
public List<System> getFullSystemListByOriginIsEmpty() {
List<System> list;
try (Session session = getSession()) {
session.getTransaction().begin();
EntityGraph entityGraph = session.getEntityGraph(SYSTEM_ENTITY_GRAPH);
CriteriaQuery<System> criteriaQuery = session.getCriteriaBuilder().createQuery(System.class);
Root<System> root = criteriaQuery.from(System.class);
criteriaQuery.select(root);
criteriaQuery.where(session.getCriteriaBuilder().isNull(root.get("origin")));
TypedQuery<System> typedQuery = session.createQuery(criteriaQuery);
typedQuery.setHint(JAVAX_PERSISTENCE_LOADGRAPH, entityGraph);
list = typedQuery.getResultList();
session.getTransaction().commit();
}
return list;
}
@Override
public List<System> getFullSystemListByPermissionBySubject(String uuid) {
List<System> list;
try (Session session = getSession()) {
session.getTransaction().begin();
EntityGraph entityGraph = session.getEntityGraph(SYSTEM_ENTITY_GRAPH);
Query query = session.createQuery("SELECT DISTINCT session" +
" from System session " +
" join Permission p on p.objectId = session.id" +
" join System s1 on p.subjectId = cast(s1.id as string)" +
" where s1.uuid = :uuid");
query.setParameter("uuid", uuid);
query.setHint(JAVAX_PERSISTENCE_LOADGRAPH, entityGraph);
list = query.getResultList();
session.getTransaction().commit();
}
return list;
}
@Override
public List<System> getFullSystemListByPermissionByObject(String uuid) {
List<System> list;
try (Session session = getSession()) {
session.getTransaction().begin();
EntityGraph entityGraph = session.getEntityGraph(SYSTEM_ENTITY_GRAPH);
Query query = session.createQuery("SELECT DISTINCT session" +
" from System session " +
" join Permission p on p.subjectId = cast(session.id as string)" +
" join System s1 on p.objectId = s1.id" +
" where s1.uuid = :uuid");
query.setParameter("uuid", uuid);
query.setHint(JAVAX_PERSISTENCE_LOADGRAPH, entityGraph);
list = query.getResultList();
session.getTransaction().commit();
}
return list;
}
@Override
public List<System> getFullSystemListByNotOrigin() {
List<System> list;
try (Session session = getSession()) {
session.getTransaction().begin();
EntityGraph entityGraph = session.getEntityGraph(SYSTEM_ENTITY_GRAPH);
CriteriaQuery<System> criteriaQuery = session.getCriteriaBuilder().createQuery(System.class);
Root<System> root = criteriaQuery.from(System.class);
criteriaQuery.select(root);
criteriaQuery.where(session.getCriteriaBuilder().isNotNull(root.get("origin")));
TypedQuery<System> typedQuery = session.createQuery(criteriaQuery);
typedQuery.setHint(JAVAX_PERSISTENCE_LOADGRAPH, entityGraph);
list = typedQuery.getResultList();
session.getTransaction().commit();
}
return list;
}
@Override
public System get(long id) {
System system;
try (Session session = getSession()) {
session.getTransaction().begin();
EntityGraph entityGraph = session.getEntityGraph(SYSTEM_ENTITY_GRAPH);
CriteriaBuilder builder = session.getCriteriaBuilder();
CriteriaQuery<System> criteriaQuery = builder.createQuery(System.class);
Root<System> root = criteriaQuery.from(System.class);
criteriaQuery.where(builder.equal(root.get("id"), id));
TypedQuery<System> typedQuery = session.createQuery(criteriaQuery);
typedQuery.setHint(JAVAX_PERSISTENCE_LOADGRAPH, entityGraph);
system = typedQuery.getSingleResult();
session.getTransaction().commit();
}
return system;
}
@Override
public System getByUuid(String uuid) {
System system;
try (Session session = getSession()) {
session.getTransaction().begin();
CriteriaBuilder builder = session.getCriteriaBuilder();
CriteriaQuery<System> criteriaQuery = builder.createQuery(System.class);
Root<System> root = criteriaQuery.from(System.class);
criteriaQuery.where(builder.equal(root.get("uuid"), uuid));
system = session.createQuery(criteriaQuery).getSingleResult();
session.getTransaction().commit();
}
return system;
}
@Override
public System getByName(String name) {
System system;
try (Session session = getSession()) {
session.getTransaction().begin();
CriteriaBuilder builder = session.getCriteriaBuilder();
CriteriaQuery<System> criteriaQuery = builder.createQuery(System.class);
Root<System> root = criteriaQuery.from(System.class);
criteriaQuery.where(builder.equal(root.get("name"), name));
system = session.createQuery(criteriaQuery).getSingleResult();
session.getTransaction().commit();
}
return system;
}
@Override
public Optional<System> fetch(long id) {
Optional<System> system;
try (Session session = getSession()) {
session.getTransaction().begin();
CriteriaBuilder builder = session.getCriteriaBuilder();
CriteriaQuery<System> criteriaQuery = builder.createQuery(System.class);
Root<System> root = criteriaQuery.from(System.class);
criteriaQuery.where(builder.equal(root.get("id"), id));
system = session.createQuery(criteriaQuery).uniqueResultOptional();
session.getTransaction().commit();
}
return system;
}
@Override
public Optional<System> fetchByUuid(String uuid) {
Optional<System> system;
try (Session session = getSession()) {
session.getTransaction().begin();
CriteriaBuilder builder = session.getCriteriaBuilder();
CriteriaQuery<System> criteriaQuery = builder.createQuery(System.class);
Root<System> root = criteriaQuery.from(System.class);
criteriaQuery.where(builder.equal(root.get("uuid"), uuid));
system = session.createQuery(criteriaQuery).uniqueResultOptional();
session.getTransaction().commit();
}
return system;
}
@Override
public Optional<System> fetchByName(String name) {
Optional<System> system;
try (Session session = getSession()) {
session.getTransaction().begin();
CriteriaBuilder builder = session.getCriteriaBuilder();
CriteriaQuery<System> criteriaQuery = builder.createQuery(System.class);
Root<System> root = criteriaQuery.from(System.class);
criteriaQuery.where(builder.equal(root.get("name"), name));
system = session.createQuery(criteriaQuery).uniqueResultOptional();
session.getTransaction().commit();
}
return system;
}
@Override
public System setBundleEntity(System system, BundleEntity bundleEntity) {
BundleEntity origin = system.getBundleEntity();
if (origin != null) {
origin.setUrl(bundleEntity.getUrl());
origin.setStatus(bundleEntity.getStatus());
} else {
bundleEntity.setVersion(system.getVersion());
system.setBundleEntity(bundleEntity);
}
return this.saveOrUpdate(system);
}
@Override
public ExportAllowed addExportAllowed(System system, String exportAllowedName) {
ExportAllowed exportAllowed = getExportAllowed(system.getExportAlloweds(), exportAllowedName);
if (exportAllowed == null) {
exportAllowed = new ExportAllowed();
exportAllowed.setName(exportAllowedName);
system.getExportAlloweds().add(exportAllowed);
saveOrUpdate(system);
}
return exportAllowed;
}
@Override
public System create(System system) {
Optional<System> original = fetchByUuid(system.getUuid());
if (original.isPresent()) {
original.get().setDescription(system.getDescription());
original.get().setEditDate(system.getCreateDate());
original.get().setEditedBy(system.getCreatedBy());
}
return this.saveOrUpdate(original.orElse(system));
}
@Override
public System saveOrUpdate(System system) {
try (Session session = getSession()) {
session.getTransaction().begin();
session.saveOrUpdate(system);
session.getTransaction().commit();
}
return system;
}
@Override
public System saveOrUpdate(String systemName, String uuid, String description, Date createDate,
Date editDate, String createdBy, String editedBy,
String version, String type) {
System system = new System();
system.setUuid(uuid);
system.setName(systemName);
system.setDescription(description);
system.setCreateDate(createDate);
system.setCreatedBy(createdBy);
system.setEditDate(editDate);
system.setEditedBy(editedBy);
system.setVersion(version);
system.setType(type);
return this.saveOrUpdate(system);
}
@Override
public void remove(long id) {
try (Session session = getSession()) {
session.getTransaction().begin();
System system = get(id);
session.delete(System.class.getName(), system);
session.getTransaction().commit();
}
}
@Override
public void remove(System system) {
try (Session session = getSession()) {
session.getTransaction().begin();
session.delete(System.class.getName(), system);
session.getTransaction().commit();
}
}
@Override
public void removeExportAllowed(System system, String esbName) {
try (Session session = getSession()) {
session.getTransaction().begin();
ExportAllowed exportAllowed = getExportAllowed(system.getExportAlloweds(), esbName);
session.delete(ExportAllowed.class.getName(), exportAllowed);
session.getTransaction().commit();
}
}
private ExportAllowed getExportAllowed(Set<ExportAllowed> exportAllowedSet, String exportAllowedName) {
for (ExportAllowed allowed : exportAllowedSet) {
if (allowed.getName().equals(exportAllowedName)) {
return allowed;
}
}
return null;
}
}

View File

@ -0,0 +1,116 @@
<?xml version="1.0" encoding="UTF-8"?>
<!--
~~~~~~licensing~~~~~~
system-impl
==========
Copyright (C) 2020 - 2021 EmDev LLC
==========
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
~~~~~~/licensing~~~~~~
-->
<blueprint xmlns="http://www.osgi.org/xmlns/blueprint/v1.0.0"
xmlns:jpa="http://aries.apache.org/xmlns/jpa/v2.0.0"
xmlns:tx="http://aries.apache.org/xmlns/transactions/v1.2.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:cm="http://aries.apache.org/blueprint/xmlns/blueprint-cm/v1.1.0"
xsi:schemaLocation="http://www.osgi.org/xmlns/blueprint/v1.0.0 https://osgi.org/xmlns/blueprint/v1.0.0/blueprint.xsd">
<jpa:enable/>
<tx:enable-annotations/>
<cm:property-placeholder persistent-id="ru.entaxy.esb" update-strategy="reload">
<cm:default-properties>
</cm:default-properties>
</cm:property-placeholder>
<reference id="sessionFactory"
interface="org.hibernate.SessionFactory"
timeout="30000"/>
<service ref="SystemService"
interface="ru.entaxy.esb.system.jpa.SystemService"/>
<bean id="SystemService"
class="ru.entaxy.esb.system.jpa.impl.SystemServiceImpl">
<property name="sessionFactory" ref="sessionFactory"/>
</bean>
<camelContext id="test-hiber-context"
xmlns="http://camel.apache.org/schema/blueprint">
<route id="list" autoStartup="false">
<from uri="timer:foo?period=15000&amp;repeatCount=1"/>
<to uri="direct:SystemServiceTest"/>
</route>
<route>
<from uri="direct:SystemServiceTest"/>
<log message="TEST---------------------SystemService------------------"/>
<bean ref="SystemService" method="getSystemList"/>
<log message="All registry ${body}"/>
<split>
<simple>${body}</simple>
<setHeader name="SYSTEM">
<simple>${body}</simple>
</setHeader>
<setBody>
<simple>null</simple>
</setBody>
<bean ref="SystemService" method="get(${header.SYSTEM.getId()})"/>
<log message="Registry by id ${body}"/>
<bean ref="SystemService" method="getByUuid(${header.SYSTEM.getUuid()})"/>
<log message="Registry by uuid ${body}"/>
<!-- <doTry> -->
<!-- <toD uri="system:${header.SYSTEM.getName()}"/> -->
<!-- <doCatch> -->
<!-- <exception>ProfileNotFound</exception> -->
<!-- <log message="${exception.stacktrace}" loggingLevel="ERROR" /> -->
<!-- </doCatch> -->
<!-- </doTry> -->
</split>
<setHeader name="SYSTEM">
<simple>${body.get(0)}</simple>
</setHeader>
<log message="Set id 0 ${header.SYSTEM.setId(0)}"/>
<log message="Set methods null ${header.SYSTEM.setExportAlloweds(null)}"/>
<log message="Set uuid 777 ${header.SYSTEM.setUuid('777')}"/>
<log message="Set name TestSystem ${header.SYSTEM.setName('TestSystem')}"/>
<log message="CREATE NEW Registry with uuid 777"/>
<bean ref="SystemService" method="create(${header.SYSTEM})"/>
<bean ref="SystemService" method="getByUuid('777')"/>
<log message="NEW Registry by uuid ${body}"/>
<bean ref="SystemService" method="remove(${body.getId()})"/>
<doTry>
<bean ref="SystemService" method="getByUuid('777')"/>
<log message="NEW Registry by uuid AFTER DELETE ${body}"/>
<doCatch>
<exception>javax.persistence.NoResultException</exception>
<log message="System not found" loggingLevel="WARN"/>
</doCatch>
</doTry>
</route>
</camelContext>
</blueprint>

View File

@ -0,0 +1,44 @@
<?xml version='1.0' encoding='utf-8'?>
<!--
~~~~~~licensing~~~~~~
system-impl
==========
Copyright (C) 2020 - 2021 EmDev LLC
==========
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
~~~~~~/licensing~~~~~~
-->
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD//EN"
"http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<session-factory>
<property name="connection.datasource">osgi:service/entaxy.esb.storage</property>
<property name="hibernate.hbm2ddl.auto">validate</property>
<property name="hibernate.enable_lazy_load_no_trans">true</property>
<property name="show_sql">${hibernate.show_sql}</property>
<property name="format_sql">${hibernate.format_sql}</property>
<mapping class="ru.entaxy.esb.system.jpa.entity.System"/>
<mapping class="ru.entaxy.esb.system.management.bundle.jpa.entity.BundleEntity"/>
<mapping class="ru.entaxy.esb.system.jpa.entity.ExportAllowed"/>
<mapping class="ru.entaxy.esb.system.connector.entity.Connector"/>
<mapping class="ru.entaxy.esb.system.connector.entity.ConnectorParam"/>
<mapping class="ru.entaxy.esb.system.core.permission.jpa.entity.Permission"/>
</session-factory>
</hibernate-configuration>