cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

Refresh documents using java api

ngibanel
Super Contributor

Hello,

 

I want initialize and refresh documents using Java API.

 

I have a MegaCollection with my documents.

MegaCollection listDocs = mgRoot.getSelection("Select [Document]", "Nom");

for (MegaObject oSelection : listDocs) {

// reinitialize and refresh document

}

 Which method use to refresh document.

 

 In VB this code works

oSelection.InitializeDocument oSelection.RefreshDocument

 but I don't know call this method in java.

 

Thanks for the response.

 

--

N. Gibanel

17 Replies

ngibanel
Super Contributor

"I'm surprised there's nothing about this document in the logfile."

I will try to refresh a document and look at the logfile.

 

"Are you asking if the logfile must have been created before the error?"

Sorry, I misspoke I was talking about the creation of the document

 

"I don't really understand you last question."

When I refresh a document, I need the name of the document.

 

For example I have 2 documents from the same template :

- document_french.doc (with french option) 

- document_english.doc (with english option)

 

If my current language is french, the name of the english document is "document_english.doc (EN)" in MEGA.

So when I try to refresh this document an error occurs because french property of the name is empty.

Currently I catch this exception and I change current language into english. Then I try to refresh document with the right name (without (EN)) 

My code :

 

for (MegaObject doc : docs) {

   try {

      // set current language (french)
      for (MegaLanguage l : mega.getMgEnv().languages()) {
         if (l.getName().equalsIgnoreCase("Français"))
            mega.getMgEnv().setCurrentLanguage(l);
      }

      // reinitialization of the document
      doc.invokeMethod("InitializeDocument", (Object) null);

      // refresh					
      doc.invokeMethod("RefreshDocument", (Object) null);

   } catch (Exception e1) { // the thrown exception may be due to a english document 

      // set current language (english)
      for (MegaLanguage l : mega.getMgEnv().languages()) {
         if (l.getName().equalsIgnoreCase("English"))
            mega.getMgEnv().setCurrentLanguage(l);
      }
	
      // try to refresh document				
      try {
         // reinitialization of the document
         doc.invokeMethod("InitializeDocument", (Object) null);

         // refresh
         doc.invokeMethod("RefreshDocument", (Object) null);
      } catch (Exception e2) {
         // error on the document
      }
   }
}

 I hope my explanation is clear.

I'm surprised there's nothing about this document in the logfile. 

Are you asking if the logfile must have been created before the error? If so, no, it's created by the application when an error occurs. 

 

I don't really understand you last question. There's an option in Document > Document Template > Language to set the language of a document, but if I understand you question, you're asking if it's possible to know what language has been used in the document ?

I mean, if the document is set as an English one, but French is used in, you want to set the name as (FR) right? If so, I think the answer is unfortunately no, we can't parse the content of the document to find out the language. That's why the option I mentionned above exists.

 



Patrick Bobo

ngibanel
Super Contributor

I look at log file and there is no error on the document that was being refreshed.

 

But I have a question : the file must be already created ?

If yes, how to distinguish documents in french or english to change current language (if current language is english and it's a document in french, the document name is <name>.doc (FR) )

My bad, the path I gave you works on Windows 7. 

Here is the path for Windows XP : C:\Documents and Settings\Administrator\Local Settings\Application Data\Mega



Patrick Bobo

invokeMethod is a Java specific method and does not exist in other languages.

callMethod is a method of the MegaItem component, and thus can be used in other languages but not for other components.

  Another difference is that you can pass a megaField string as a parameter for callMethod : in this case the given id may be an _operator or a macro identifier; so you can call a macro directly from a megaItem, if the macro implements the appropriate interface.

@pabobo

I don't find logfile on directory : C:\Documents and Settings\gibanel\Application Data\Mega (i'm on XP)

 

@cdebellegarde

Thank you for these useful explanations.

I understand better but what is the difference between callMethod and invokeMethhod ? It seems to me that the result is the same, just the object returned is different.

cdebellegarde
Retired

When trying to convert a Script Macro into a Java Macro, you often ask yourself the same question: “this function does not belong to the interface.  How can I use it?”

This question requires an explanation that is briefly exposed in the API documentation. The most important thing to understand is that almost all the MEGA Components you can use in a Script or Java Macro are ‘late bound’ component; this means that the functions available for this component are not known when you write the code, but only when you run it.

 

There are two reasons for this:

 

  1. For the MegaObject component (that can map a Mega Repository Object), a comprehensive way to access to the Properties, collections or Methods of the repository object is to use them as a function.

For example:

 

                myObject.name                   accesses to the “name” MetaAttribute

                myObject.component         accesses to the “Component” MetaassociationEnd

                myObject.explore                accesses to the “explore” _operator  (the MetaClass _operator enables to define a new method)

 

                As those MetaAttributes, MetaAssociationEnds and _Operators depend on the MetaModel, they are not known at writing time; they need a ‘late binding’ implementation to work.

               

  1. Most of the utilities components provided by MEGA are written in a script-based technology. For those components, no interface is defined, and all the methods are accessed in a ‘late binding’ mode

 

When you want to use such objects in a strict syntax based language such as JAVA or C#, it is not possible to call a function that is not defined in the interface, so you may figure that such a call is not possible… But there is a solution.

The first thing you have to know is this:

 

Unless unreported bugs, all (I mean ALL) the functions or methods of all the components provided by MEGA and usable in a script code can be used in a JAVA code (and probably in all .NET code). The only limitation in JAVA is that the functions must not have more than six parameters. Fortunately, none of the MEGA-provided method is in this case (as far as I know).

 

Knowing this, how to actually call those functions?

 

First of all, you have to know if you are in the first case or in the second case.

 

If you manage a MegaObject, you are probably in the first case, but you have to know that some late-bound functions (as .exists for example) had been added to the MegaObject component.

 

If you are in the first case, you must use the explicit form of the functions (that can also be used in a script code) :

 

                myObject.name             ->   myObject.getProp(“name”) or myObject.setProp(“name”,<value>)

                myObject.component   ->   myObject.getCollection(“component”)

                myObject.explore           ->   myObject.callMethod “explore”           (if the method is a function returning a value, you must use myObject.callFunction()

 

All the additional parameters – if any – must be set after the name of the property, collection or method.

               

When you are using such a function of a MegaObject, you probably know if it is a MetaAttribute or a MetaAssociationEnd; in others cases, you must check if the name is not the name of an _operator.

If you cannot find an _operator with this name in the metamodel, then you are in the second case explained below.

 

If you are in the second case, the solution differs within you are in .NET or Java.

 

In .NET you must refer to the Microsoft IDispatch interface that is implemented by all the MEGA components. To know how to call their methods, see Microsoft documentation

 

In JAVA:  all the MEGA components implement the MegaCOMObject interface that maps conveniently the IDispatch interop interface.

 

First of all, you can check if a function is implemented by the component with

        public boolean isMemberName(String MName);

This is not very useful but it can be used in debug time for example, to ensure that the given component is the expected one.

 

The subsequent examples will explain the different available conversion

 

Script Code

Java Code

A = myObject.propName

A = myObject.invokePropertyGet(“propName”);    ’use the appropriate cast

A = myObject.funcName()

A = myObject.invokeFunction(“funcName”);    ’use the appropriate cast

A = myObject.funcName(Param)

A = myObject.invokeFunction(“funcName”,Param); ’use the appropriate cast

myObject.propName = A

myObject.invokePropertyPut(“propName”, A);

myObject.propName(Option) = A

myObject.invokePropertyPut(“propName”, A, Option);

myObject.subName

myObject.invokeMethod(“subName”);

myObject.subName  Param1,Param2

myObject.invokeMethod(“subName”, Param1, Param2);

 

As you can see in the interface, the invokePropertyGet and invokeFunction functions return an Object. So you have to cast the result.

 

If the returned object is a component, you always can cast it with the MegaCOMObject interface.

 

If you often use such a component, you can define a wrapper to have get a convenient Java class for using this component. Some wrappers are defined in the com.mega.modeling.api.util package

Can you give me the logfile corresponding please ? 

 

You'll find it at this path : 

 

C:\Users\<Your User>\AppData\Local\Mega

 

It might help to find out what there error is about.



Patrick Bobo

ngibanel
Super Contributor

When I refresh a document, at the end I have this exception launch:

 

com.mega.modeling.api.MegaException: MEGA Error 80040a00

 

but my document seems correct.

 

What means this exception or error code ?

 

Thanks

ngibanel
Super Contributor

ok.

thanks for your help.

 

 

--

N.Gibanel