package com.mega.bizpro.client.utils; import java.util.Date; import com.mega.modeling.api.MegaApplication; import com.mega.modeling.api.MegaCOMObject; import com.mega.modeling.api.MegaCollection; import com.mega.modeling.api.MegaEnvironment; import com.mega.modeling.api.MegaObject; import com.mega.modeling.api.MegaRoot; import com.mega.modeling.api.MegaToolkit; import com.mega.modeling.api.MegaTransaction; import com.mega.modeling.api.log.ErrorLogFormater; /** * This class allows to use HOPEX from a Java application, instead of calling * Java from HOPEX.
* You need a Java x86 to run it successfully, preferably that one from HOPEX * itself. * * @author RIA/JFO * @version 0.1 */ public class HopexHandler { private static MegaApplication mgAppl = null; private static MegaEnvironment mgEnv = null; private static MegaToolkit mgToolkit = null; private static MegaTransaction mgTransaction = null; private static MegaRoot mgRoot = null; private static HopexHandler mgInstance = null; private static ErrorLogFormater elf = null; //Parameters TODO: read them from any other source, avoid hardcoding private static String paramEnvironment = null; private static String paramUser = null; private static String paramPassword = null; private static String paramRepository = null; private static String paramLogin = null; private static String paramProfile = null; private static String paramOpenMode = null; private static String paramTranType = null; private static String paramLogType = null; private HopexHandler() { } /** * put this in a properties file or database and manage it in another class * * @param Environment The environment name as it is shown in the HOPEX * Administrator, even if it's shown as a shared folder * instead of a local one * @param User The current user name * @param Password The current user password * @param Repository The data repository to connect to (Like in HOPEX, you have * to specify it even if you only have one) * @param Login The absolute identifier of the login belonging to the * current user * @param Profile The absolute identifier of the profile the current user * needs to connect with, it has to be already assigned to * the user * @param OpenMode Either if it's a read-only or a read/write session R|W * @param TranType The type of HOPEX transaction, be it private, public or * micro Public|Private|Micro */ private void init() { Date d1 = new Date(); Date d2 = null; loadParameters(); if (mgAppl != null) { mgAppl.release(); } mgAppl = new MegaApplication(); if (mgEnv != null) { mgEnv.release(); } mgEnv = mgAppl.getEnvironments().get(paramEnvironment); mgEnv.setCurrentAdministrator(paramUser); mgEnv.setCurrentPassword(paramPassword); if (mgTransaction != null) { mgTransaction.release(); } mgTransaction = mgEnv.transactions().create(mgEnv.databases().get(paramRepository), paramUser); if (mgRoot != null) { mgRoot.release(); } mgRoot = mgTransaction.getOwnedDatabase() .openEx(new StringBuffer("Login=").append(paramLogin) .append(";Profile=").append(paramProfile) .append(";OpenMode=").append(paramOpenMode) .append(";TranType=").append(paramTranType) .toString()); if (mgToolkit != null) { mgToolkit.release(); } mgToolkit = mgRoot.currentEnvironment().toolkit(); d2 = new Date(); elf = new ErrorLogFormater(); elf.openSession(mgRoot); logMessage("Handler initialized in {1} ms".replaceAll("\\{1\\}", Long.toString(d2.getTime() - d1.getTime()))); } /** * Replace this method to read from another source */ public static void loadParameters() { paramEnvironment = "C:\\Users\\Public\\Documents\\HOPEX V4\\JFO"; paramUser = "jfo"; paramPassword = "Hopex123#"; paramRepository = "Data"; paramLogin = "E(OlnBfPVbOS"; paramProfile = "757wuc(SGjpJ";//757wuc(SGjpJ paramOpenMode = "W"; paramTranType = "Private"; paramLogType = "A"; } /** * This is the proper way to initialize your HopexHandler, the details are in * the init() method (private) * * @return The only instance of the HopexHandler */ public static HopexHandler getInstance() { if (mgInstance == null) { mgInstance = new HopexHandler(); mgInstance.init(); } return mgInstance; } /** * Returns the MegaRoot belonging to the current Hopex instance * * @return MegaRoot */ public MegaRoot getRoot() { return mgRoot; } /** * Returns the Hopex log formater (To use instead of JLog or any Java logger) * * @return ErrorLogFormater */ public ErrorLogFormater getErrorLogFormater() { return elf; } /** * This method discards the current transaction and closes the HOPEX process, * saving the users from doing so manually */ public void close() { mgRoot.megaRollback(); if (mgTransaction != null) { mgTransaction.abort(); mgTransaction = null; } mgRoot.release(); } /** * This method saves the current transaction and closes the HOPEX process, * saving the users from doing so manually */ public void save() { mgRoot.megaCommit(); if (mgTransaction != null) { mgTransaction.dispatch(); mgTransaction = null; } mgRoot.release(); } /** * This method saves the current transaction and creates a new one to replace it */ public void dispatch() { mgRoot.megaCommit(); if (mgTransaction != null) { mgTransaction.dispatch(); mgTransaction = null; } init(); } /** * This method releases the current Mega Object to avoid memory issues * * @param obj The object to release */ public void release(MegaObject obj) { if (null != obj) { obj.release(); } } /** * This method releases the current Mega Object to avoid memory issues * * @param obj The object to release */ public void release(MegaCollection obj) { if (null != obj) { obj.release(); } } /** * This method releases the current Mega Object to avoid memory issues * * @param obj The object to release */ public void release(MegaEnvironment obj) { if (null != obj) { obj.release(); } } /** * This method releases the current Mega Object to avoid memory issues * * @param obj The object to release */ public void release(MegaCOMObject obj) { if (null != obj) { obj.release(); } } /** * This method releases the current Mega Object to avoid memory issues * * @param obj The object to release */ public void release(MegaToolkit obj) { if (null != obj) { obj.release(); } } /** * Logs the messages other than errors in the Java console, the HOPEX logfile or * both (Depending on the parameters) C|I|A
* If you want to avoid logging at all, just write any other value
* To log an error, use the method * {@code getErrorLogFormater().logError("Error Message")} instead * * @param message The message to log */ public void logMessage(String message) { if (paramLogType.equals("C")) { System.out.println(message); } else if (paramLogType.equals("I")) { elf.logMessage(message); } else if (paramLogType.equals("A")) { System.out.println(message); elf.logMessage(message); } } public MegaApplication getMgAppl() { return mgAppl; } public MegaEnvironment getMgEnv() { return mgEnv; } public MegaToolkit getMgToolkit() { return mgToolkit; } public MegaTransaction getMgTransaction() { return mgTransaction; } public HopexHandler getMgInstance() { return mgInstance; } }