diff --git a/maxkey-persistence/src/main/java/org/dromara/maxkey/persistence/service/OrganizationsService.java b/maxkey-persistence/src/main/java/org/dromara/maxkey/persistence/service/OrganizationsService.java index 8a4c6d045..db08cd588 100644 --- a/maxkey-persistence/src/main/java/org/dromara/maxkey/persistence/service/OrganizationsService.java +++ b/maxkey-persistence/src/main/java/org/dromara/maxkey/persistence/service/OrganizationsService.java @@ -102,13 +102,14 @@ public class OrganizationsService extends JpaService{ return false; } + public void reorgNamePath(String instId) { _logger.debug("instId {}", instId); - if(StringUtils.isBlank(instId)) { + if (StringUtils.isBlank(instId)) { instId = "1"; } - - HashMap reorgOrgMap = new HashMap<>(); + + HashMap reorgOrgMap = new HashMap<>(); List orgList = find(" where instid ='" + instId + "'"); List originOrgList = new ArrayList<>(); Organizations rootOrg = null; @@ -117,7 +118,7 @@ public class OrganizationsService extends JpaService{ if (isRootOrg(org)) { rootOrg = org; } - Organizations cloneOrg = new Organizations(); + Organizations cloneOrg = new Organizations(); BeanUtils.copyProperties(org, cloneOrg); originOrgList.add(cloneOrg); } @@ -126,10 +127,10 @@ public class OrganizationsService extends JpaService{ _logger.debug("reorged ."); long reorgCount = 0; for (Organizations originOrg : originOrgList) { - Organizations reorgOrg = reorgOrgMap.get( originOrg.getId()); - _logger.trace("reorged Organization {}" ,reorgOrg); - if(originOrg.getNamePath()== null || !originOrg.getNamePath().equals(reorgOrg.getNamePath())) { - _logger.debug("update reorgCount {} , Organization {}" ,++reorgCount,reorgOrg); + Organizations reorgOrg = reorgOrgMap.get(originOrg.getId()); + _logger.trace("reorged Organization {}", reorgOrg); + if (originOrg.getNamePath() == null || !originOrg.getNamePath().equals(reorgOrg.getNamePath())) { + _logger.debug("update reorgCount {} , Organization {}", ++reorgCount, reorgOrg); getMapper().updateNamePath(reorgOrg); } } @@ -138,32 +139,30 @@ public class OrganizationsService extends JpaService{ _logger.error("reorgNamePath Exception ", e); } } - - public static boolean isRootOrg(Organizations rootOrg){ - return ( - rootOrg.getParentId() == null - || rootOrg.getParentId().equalsIgnoreCase("-1") - || rootOrg.getParentId().equalsIgnoreCase("0") + + boolean isRootOrg(Organizations rootOrg) { + if (rootOrg.getParentId() == null || rootOrg.getParentId().equalsIgnoreCase("-1") || rootOrg.getParentId().equalsIgnoreCase(rootOrg.getId()) - || rootOrg.getParentId().equalsIgnoreCase(rootOrg.getInstId()) - ); + || rootOrg.getParentId().equalsIgnoreCase(rootOrg.getInstId())) { + return true; + } + return false; } - void reorg(HashMap orgMap, List orgList,Organizations rootOrg) { + void reorg(HashMap orgMap, List orgList, Organizations rootOrg) { if (isRootOrg(rootOrg)) { - rootOrg.setCodePath("/" + rootOrg.getId()+"/"); - rootOrg.setNamePath("/" + rootOrg.getOrgName()+"/"); + rootOrg.setCodePath("/" + rootOrg.getId() + "/"); + rootOrg.setNamePath("/" + rootOrg.getOrgName() + "/"); } else { Organizations parent = orgMap.get(rootOrg.getParentId()); - rootOrg.setCodePath(parent.getCodePath() + rootOrg.getId()+ "/"); - rootOrg.setNamePath(parent.getNamePath() + rootOrg.getOrgName()+ "/"); + rootOrg.setCodePath(parent.getCodePath() + rootOrg.getId() + "/"); + rootOrg.setNamePath(parent.getNamePath() + rootOrg.getOrgName() + "/"); } rootOrg.setReorgNamePath(true); for (Organizations org : orgList) { - if (org.isReorgNamePath()) { + if (org.isReorgNamePath()) continue; - } if (org.getParentId().equalsIgnoreCase(rootOrg.getId())) { reorg(orgMap, orgList, org); } diff --git a/maxkey-synchronizers/maxkey-synchronizer-reorgdept/build.gradle b/maxkey-synchronizers/maxkey-synchronizer-reorgdept/build.gradle deleted file mode 100644 index d1ec0e5b9..000000000 --- a/maxkey-synchronizers/maxkey-synchronizer-reorgdept/build.gradle +++ /dev/null @@ -1,14 +0,0 @@ -description = "maxkey-synchronizer-reorgdept" - -apply plugin: 'java' - -dependencies { - //local jars - implementation fileTree(dir: '../maxkey-lib/*/', include: '*.jar') - - implementation project(":maxkey-common") - implementation project(":maxkey-core") - implementation project(":maxkey-persistence") - implementation project(":maxkey-synchronizers:maxkey-synchronizer") - -} \ No newline at end of file diff --git a/maxkey-synchronizers/maxkey-synchronizer-reorgdept/src/main/java/org/dromara/maxkey/synchronizer/reorg/ReorgDeptSynchronizerService.java b/maxkey-synchronizers/maxkey-synchronizer-reorgdept/src/main/java/org/dromara/maxkey/synchronizer/reorg/ReorgDeptSynchronizerService.java deleted file mode 100644 index f65109129..000000000 --- a/maxkey-synchronizers/maxkey-synchronizer-reorgdept/src/main/java/org/dromara/maxkey/synchronizer/reorg/ReorgDeptSynchronizerService.java +++ /dev/null @@ -1,60 +0,0 @@ -/* - * Copyright [2021] [MaxKey of copyright http://www.maxkey.top] - * - * 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. - */ - - -package org.dromara.maxkey.synchronizer.reorg; - -import org.dromara.maxkey.entity.Synchronizers; -import org.dromara.maxkey.synchronizer.ISynchronizerService; -import org.dromara.maxkey.synchronizer.reorg.service.ReorgDeptService; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; -import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.stereotype.Service; - -@Service -public class ReorgDeptSynchronizerService implements ISynchronizerService { - static final Logger _logger = LoggerFactory.getLogger(ReorgDeptSynchronizerService.class); - Synchronizers synchronizer; - - @Autowired - ReorgDeptService reorgDeptService; - - - public ReorgDeptSynchronizerService() { - super(); - } - - public void sync() throws Exception { - _logger.info("Sync ..."); - reorgDeptService.setSynchronizer(synchronizer); - reorgDeptService.sync(); - - } - - - - public void setReorgDeptService(ReorgDeptService reorgDeptService) { - this.reorgDeptService = reorgDeptService; - } - - @Override - public void setSynchronizer(Synchronizers synchronizer) { - this.synchronizer = synchronizer; - - } - -} diff --git a/maxkey-synchronizers/maxkey-synchronizer-reorgdept/src/main/java/org/dromara/maxkey/synchronizer/reorg/service/ReorgDeptService.java b/maxkey-synchronizers/maxkey-synchronizer-reorgdept/src/main/java/org/dromara/maxkey/synchronizer/reorg/service/ReorgDeptService.java deleted file mode 100644 index d7325b305..000000000 --- a/maxkey-synchronizers/maxkey-synchronizer-reorgdept/src/main/java/org/dromara/maxkey/synchronizer/reorg/service/ReorgDeptService.java +++ /dev/null @@ -1,36 +0,0 @@ -/* - * Copyright [2021] [MaxKey of copyright http://www.maxkey.top] - * - * 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. - */ - - -package org.dromara.maxkey.synchronizer.reorg.service; - -import org.dromara.maxkey.synchronizer.AbstractSynchronizerService; -import org.dromara.maxkey.synchronizer.ISynchronizerService; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; -import org.springframework.stereotype.Service; - -@Service -public class ReorgDeptService extends AbstractSynchronizerService implements ISynchronizerService{ - static final Logger _logger = LoggerFactory.getLogger(ReorgDeptService.class); - - public void sync() { - _logger.info("Sync Organizations ..."); - organizationsService.reorgNamePath(this.synchronizer.getInstId()); - - } - -} diff --git a/maxkey-webs/maxkey-web-mgt/src/main/java/org/dromara/maxkey/autoconfigure/MaxKeyMgtListenerConfig.java b/maxkey-webs/maxkey-web-mgt/src/main/java/org/dromara/maxkey/autoconfigure/MaxKeyMgtListenerConfig.java index 92ad6ba5d..b3d2ec691 100644 --- a/maxkey-webs/maxkey-web-mgt/src/main/java/org/dromara/maxkey/autoconfigure/MaxKeyMgtListenerConfig.java +++ b/maxkey-webs/maxkey-web-mgt/src/main/java/org/dromara/maxkey/autoconfigure/MaxKeyMgtListenerConfig.java @@ -22,9 +22,11 @@ import org.dromara.maxkey.configuration.ApplicationConfig; import org.dromara.maxkey.listener.DynamicGroupsListenerAdapter; import org.dromara.maxkey.listener.ListenerAdapter; import org.dromara.maxkey.listener.ListenerParameter; +import org.dromara.maxkey.listener.ReorgDeptListenerAdapter; import org.dromara.maxkey.listener.SessionListenerAdapter; import org.dromara.maxkey.persistence.service.ConnectorsService; import org.dromara.maxkey.persistence.service.GroupsService; +import org.dromara.maxkey.persistence.service.OrganizationsService; import org.dromara.maxkey.provision.thread.ProvisioningRunner; import org.dromara.maxkey.provision.thread.ProvisioningRunnerThread; import org.quartz.Scheduler; @@ -55,6 +57,20 @@ public class MaxKeyMgtListenerConfig { return "sessionListenerAdapter"; } + @Bean + public String reorgDeptListenerAdapter( + Scheduler scheduler, + OrganizationsService organizationsService) throws SchedulerException { + ListenerAdapter.addListener( + scheduler, + ReorgDeptListenerAdapter.class, + "0 0/30 * * * ?",//30 minutes + new ListenerParameter().add("organizationsService",organizationsService).build() + ); + logger.debug("ReorgDept ListenerAdapter inited ."); + return "reorgDeptListenerAdapter"; + } + @Bean public String dynamicGroupsListenerAdapter( Scheduler scheduler, diff --git a/maxkey-webs/maxkey-web-mgt/src/main/java/org/dromara/maxkey/listener/ReorgDeptListenerAdapter.java b/maxkey-webs/maxkey-web-mgt/src/main/java/org/dromara/maxkey/listener/ReorgDeptListenerAdapter.java new file mode 100644 index 000000000..de7842a4f --- /dev/null +++ b/maxkey-webs/maxkey-web-mgt/src/main/java/org/dromara/maxkey/listener/ReorgDeptListenerAdapter.java @@ -0,0 +1,59 @@ +/* + * Copyright [2024] [MaxKey of copyright http://www.maxkey.top] + * + * 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. + */ +package org.dromara.maxkey.listener; + +import java.io.Serializable; + +import org.dromara.maxkey.persistence.service.OrganizationsService; +import org.quartz.Job; +import org.quartz.JobExecutionContext; +import org.quartz.JobExecutionException; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +public class ReorgDeptListenerAdapter extends ListenerAdapter implements Job , Serializable { + static final Logger _logger = LoggerFactory.getLogger(ReorgDeptListenerAdapter.class); + + private static final long serialVersionUID = 4782358765969474833L; + + transient OrganizationsService organizationsService; + + @Override + public void execute(JobExecutionContext context) throws JobExecutionException { + if(jobStatus == JOBSTATUS.RUNNING) {return;} + init(context); + + _logger.debug("running ... " ); + jobStatus = JOBSTATUS.RUNNING; + try { + organizationsService.reorgNamePath(null); + _logger.debug("finished " ); + jobStatus = JOBSTATUS.FINISHED; + }catch(Exception e) { + jobStatus = JOBSTATUS.ERROR; + _logger.error("Exception " ,e); + } + + } + + @Override + protected void init(JobExecutionContext context){ + super.init(context); + if(organizationsService == null) { + organizationsService = getParameter("organizationsService",OrganizationsService.class); + } + } +} diff --git a/maxkey-webs/maxkey-web-mgt/src/main/java/org/dromara/maxkey/listener/SessionListenerAdapter.java b/maxkey-webs/maxkey-web-mgt/src/main/java/org/dromara/maxkey/listener/SessionListenerAdapter.java index 73b0d6f1c..f40796950 100644 --- a/maxkey-webs/maxkey-web-mgt/src/main/java/org/dromara/maxkey/listener/SessionListenerAdapter.java +++ b/maxkey-webs/maxkey-web-mgt/src/main/java/org/dromara/maxkey/listener/SessionListenerAdapter.java @@ -48,7 +48,7 @@ public class SessionListenerAdapter extends ListenerAdapter implements Job , S for (HistoryLogin login : sessionManager.querySessions()) { Session session = sessionManager.get(login.getSessionId()); if(session == null) { - logger.debug("user {} session {} Login at {} and TimeOut at {} ." , + logger.debug("TimeOut user {} session {} Login at {} and at {} ." , login.getUsername(), login.getId(), login.getLoginTime(), diff --git a/settings.gradle b/settings.gradle index e23846fbc..1fc2a658e 100644 --- a/settings.gradle +++ b/settings.gradle @@ -1,5 +1,5 @@ /* - * Copyright [2021] [MaxKey of copyright http://www.maxkey.top] + * Copyright [2024] [MaxKey of copyright http://www.maxkey.top] * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -45,7 +45,6 @@ include ('maxkey-synchronizers:maxkey-synchronizer-dingtalk') include ('maxkey-synchronizers:maxkey-synchronizer-feishu') include ('maxkey-synchronizers:maxkey-synchronizer-jdbc') include ('maxkey-synchronizers:maxkey-synchronizer-ldap') -include ('maxkey-synchronizers:maxkey-synchronizer-reorgdept') include ('maxkey-synchronizers:maxkey-synchronizer-workweixin') //Protocol