In large business systems, it is often necessary to query an entity object from the database. Before processing, it is necessary to detect the presence of the entity to enhance the robustness of the system. However, detection code flooding in the main business process will greatly reduce the clarity of business logic, it is best to focus on management. Therefore, a class is written to do this, and a single call is required to complete the detection of the corresponding entity object.
In the ointment, the location of the log has changed. Because the main goal of the log is to facilitate debugging and locating the wrong location, in order to compensate, one way is to invoke the method in accordance with the specific context of incoming custom error messages, depending on the error message can be located in the wrong location, or if you do not want each call to use a different custom error message, use the Log.error ("error", Bizex) prints the exception stack information and can also get to the location where the error was thrown.
Note: In order to comply with the confidentiality of the company code, the implicit introduction of the package name, you can draw on their ideas, the use of their own projects.
How to use:
Import Staticutil. checkentityexistmanager.checkentity;Importutil. Checkentityexistmanager.entitychecker; Public classEntityexistchecktester {Private Static FinalLog log = Logfactory.getlog (entityexistchecktester.class); Public Static voidMain (string[] args)throwsbizexception {demo (); Demo2 (); } Public StaticResultSet Demo () {Try { //Not need type castingDevicedo device = checkentity (Entitychecker.disk_id_check, "d-xxx"); SYSTEM.OUT.PRINTLN (device); Vmdo VMS= Checkentity (Entitychecker.vm_name_check, "Vm-name", "user-defined MSG:VM not exist when creating"); SYSTEM.OUT.PRINTLN (VM); return NewResultSet (ierrorcode.successful); } Catch(bizexception Bizex) {log.error ("Error", Bizex); return NewResultSet (Bizex.geterrorcode ()); } Catch(Exception ex) {log.error (ex); return NewResultSet (ierrorcode.failed); } } Public StaticResultSet Demo2 () {Try{Zonedo Zone= Checkentity (Entitychecker.zone_id_check, "Z-yy"); System.out.println (zone); return NewResultSet (ierrorcode.successful); } Catch(bizexception Bizex) {log.error ("Error", Bizex); return NewResultSet (Bizex.geterrorcode ()); } Catch(Exception ex) {log.error (ex); return NewResultSet (ierrorcode.failed); } }}
entity existence Centralized detection class: Checkentityexistmanager:
Public classCheckentityexistmanager {Private Static FinalLog log = Logfactory.getlog (Checkentityexistmanager.class); Private StaticCheckentityexistmanager checkentitymgr =getinstance (); PrivateCheckentityexistmanager () {}Private StaticCheckentityexistmanager getinstance () {if(Checkentitymgr = =NULL) {Checkentitymgr=NewCheckentityexistmanager (); } returnCheckentitymgr; } InterfaceIcheckentityexist<t>{T checkentityexist (string entityvalue, String msg)throwsbizexception; } Private StaticIcheckentityexist<devicedo> Diskidchecker = checkentitymgr.Newcheckdiskidexist (); Private StaticIcheckentityexist<vmdo> Vmnamechecker = checkentitymgr.Newcheckvmexist (); Private StaticIcheckentityexist<zonedo> Zonechecker = checkentitymgr.Newcheckzoneexist (); Public enumEntitychecker {disk_id_check (Diskidchecker, Deviceerrorcode.disk_not_exist), Vm_name_check (VmNameCheck Er, vmerrorcode.error_vm_not_exist), Zone_id_check (Zonechecker, zoneerrorcode.zone_not_exist); Entitychecker (Icheckentityexist<?>Entitychecker, Ierrorcode errorCode) { This. Entitychecker =Entitychecker; This. ErrorCode =ErrorCode; } Private Staticmap<string, ierrorcode> errormap =NewHashmap<string, ierrorcode>(); Static { for(Entitychecker entityChecker:EntityChecker.values ()) {icheckentityexist<?> Checker =Entitychecker.getentitychecker (); Errormap.put (Checker.getclass (). Getsimplename (), Entitychecker.geterrorcode ()); } } Private Staticierrorcode GetErrorCode (String checkclasssimplename) {returnErrormap.get (checkclasssimplename); } PrivateIcheckentityexist<?>Entitychecker; PrivateIerrorcode ErrorCode; PublicIcheckentityexist<?>Getentitychecker () {returnEntitychecker; } Public voidSetentitychecker (icheckentityexist<?>Entitychecker) { This. Entitychecker =Entitychecker; } PublicIerrorcode GetErrorCode () {returnErrorCode; } Public voidSeterrorcode (Ierrorcode errorCode) { This. ErrorCode =ErrorCode; }} @AutowiredPrivateDeviceservice Deviceservice; @AutowiredPrivateVmservice Vmservice; @AutowiredPrivateZoneservice Zoneservice; classCheckdiskidexistImplementsIcheckentityexist<devicedo>{@Override PublicDevicedo checkentityexist (String diskId, String msg)throwsbizexception {ierrorcode ErrorCode= Entitychecker.geterrorcode (checkdiskidexist.class. Getsimplename ()); String message= (msg = =NULL?errorcode.getmessage (): msg); //Devicedo dev = deviceservice.getdevice (diskId); Devicedo dev =NewDevicedo (); returnCheckentityexistmanager.throwexorreturn (Dev, message, errorCode); } } classCheckvmexistImplementsIcheckentityexist<vmdo>{@Override PublicVmdo checkentityexist (String vmName, String msg)throwsbizexception {ierrorcode ErrorCode= Entitychecker.geterrorcode (checkvmexist.class. Getsimplename ()); String message= (msg = =NULL?errorcode.getmessage (): msg); //vmdo vm = Vmservice.queryvmbyname (vmName);Vmdo VM =NULL; returnCheckentityexistmanager.throwexorreturn (VM, message, ErrorCode); } } classCheckzoneexistImplementsIcheckentityexist<zonedo>{@Override PublicZonedo checkentityexist (String ZoneID, String msg)throwsbizexception {ierrorcode ErrorCode= Entitychecker.geterrorcode (checkzoneexist.class. Getsimplename ()); String message= (msg = =NULL?errorcode.getmessage (): msg); //Zonedo zone = Zoneservice.queryzonebyid (ZoneID);Zonedo zone =NULL; returnCheckentityexistmanager.throwexorreturn (zone, message, errorCode); } } Public Static<T>T Throwexorreturn (t entity, String message, Ierrorcode ErrorCode) throwsbizexception {if(Entity = =NULL) {log.error (message); Throw Newbizexception (ErrorCode); } returnentity; } Public Static<T> T checkentity (entitychecker entitychecker, String entityvalue, String custommsg)throwsbizexception {icheckentityexist<T> Checker = (icheckentityexist<t>) Entitychecker.getentitychecker (); returnchecker.checkentityexist (Entityvalue, custommsg); } Public Static<T> T checkentity (entitychecker entitychecker, String entityvalue)throwsbizexception {icheckentityexist<T> Checker = (icheckentityexist<t>) Entitychecker.getentitychecker (); returnChecker.checkentityexist (Entityvalue,NULL); } }
Centralized management of object existence detection