Chinaunix首页 | 论坛 | 博客
  • 博客访问: 313922
  • 博文数量: 118
  • 博客积分: 2010
  • 博客等级: 大尉
  • 技术积分: 1163
  • 用 户 组: 普通用户
  • 注册时间: 2009-08-27 12:09
文章分类

全部博文(118)

文章存档

2023年(20)

2022年(3)

2021年(1)

2020年(1)

2019年(7)

2013年(2)

2011年(1)

2010年(37)

2009年(46)

我的朋友

分类: WINDOWS

2009-10-13 15:33:07

Sample


HOW TO USE
==========

Follow these steps to write the program:

  1. Make sure your programming environment is set up correctly. You need:
    • Java Development Kit (JDK) 5 or later. (Java SE 5 is also know as Java 1.5.0.)
    • The AR System Java API files. (See for installation and verification.)
  2. Create a Java project in your IDE.
  3. Include the arapi71.jar and the other required JAR files in the AR System API lib directory in the class path.
  4. Create a new class for the methods that call the Java API.
  5. Import the com.bmc.arsys.api package and other packages you might use in your program. The API uses collection classes, so you are likely to need java.util.ArrayList, java.util.List, and java.util.Map.
  6. Instantiate an ARServerUser object. Set the user name, password, server, and other connection attributes. If the program needs to interact with different servers or as different users, it can create more than one ARServerUser object.
  7. Use the ARServerUser login method to open the connection to the server.
  8. Perform the required operations using the ARServerUser methods and other API objects and methods to create, retrieve, update, and delete AR System system objects as needed and creating criteria objects and using server object methods as required.
  9. Use the ARServerUser logout method to close the connection to the server.
  10. 另外要确保arapi71.dll以及arapi71.dll引用到的DLL放入Path里面。比如可以放在C:\Program Files\Java\jre1.5.0_15\bin\或者C:\WINDOWS\system32下面。

Sample Code
==========

The following sample code illustrates how to use the Java API to create, modify, and query records in AR System:

 

package com.bmc.arsys.demo.samples;
import com.bmc.arsys.api.*;
import java.util.*;
public class JavaAPITest {
    private ARServerUser server;
    private String formName= "JavaAPITest";
    
    public JavaAPITest() {
        server = new ARServerUser();
        server.setServer("localhost");
        server.setUser("Demo");
        server.setPassword("");
    }
    
    public static void main(String[] args) {
        JavaAPITest test = new JavaAPITest();
        test.connect();
        test.createEntry("Demo","1","test1");
        test.createEntry("Demo","2","test2");
        String entryID = test.createEntry("Demo","3","test3");
        test.modifyEntry(entryID);
        test.queryEntrysByID(entryID);
        test.queryEntrysByQual(
            "( \'Create Date\' > \"1/1/2004 12:00:00 AM\" )");
        test.queryEntrysByQual("( \'Create Date\' > \"1/1/2010\" )");
        test.cleanup();
    }
    // Connect the current user to the server.

    void connect() {
        System.out.println();
        System.out.println("Connecting to AR Server...");
        try {
            server.verifyUser();
        } catch (ARException e) {
            //This exception is triggered by a bad server, password or,

            //if guest access is turned off, by an unknown username.

            ARExceptionHandler(e, "Cannot verify user " +
                server.getUser() + ".");
            System.exit(1);
        }
        System.out.println("Connected to AR Server " +
            server.getServer());
    }
    
    // Create an entry in a form using the given field values.

    public String createEntry (String submitter, String status,
        String shortDesc) {
        String entryIdOut= "";
        try {
            Entry entry = new Entry();
            entry.put(Constants.AR_CORE_SUBMITTER, new Value(submitter));
            entry.put(Constants.AR_CORE_STATUS,
                new Value(status, DataType.ENUM));
            entry.put(Constants.AR_CORE_SHORT_DESCRIPTION,
                new Value(shortDesc));
            entryIdOut = server.createEntry(formName, entry);
            System.out.println();
            System.out.println("Entry created. The id # is " +
                entryIdOut);
        } catch (ARException e) {
            ARExceptionHandler(e, "Cannot create the entry." );
        }
        return entryIdOut;
    }
    
    // Modify the short description field on the specified entry.

    void modifyEntry(String entryId) {
        try {
           Entry entry = server.getEntry(formName, entryId, null);
           entry.put(Constants.AR_CORE_SHORT_DESCRIPTION,
               new Value("Modified by JavaAPITest"));
           server.setEntry(formName, entryId, entry, null, 0);
           System.out.println();
           System.out.println("Entry #" + entryId +
               " modified successfully.");
        }
        catch(ARException e) {
            ARExceptionHandler(e,"Cannot modify the entry. ");
        }
    }
    // Retrive an entry by its entry ID and print out the number of

    // fields in the entry. For each field in the entry, print out the

    // value, and the field info (name, id and the type).

    void queryEntrysByID(String entryId) {
        System.out.println();
        System.out.println("Retrieving entry with entry ID#" + entryId);
        try {
            Entry entry = server.getEntry(formName, entryId, null);
            if( entry == null ){
                System.out.println("No data found for ID#" + entryId);
                return;
            } else
                System.out.println("Number of fields: " + entry.size());
        
            // Retrieve all properties of fields in the entry.

            Set<Integer> fieldIds = entry.keySet();
            for (Integer fieldId : fieldIds){
                Field field = server.getField(formName,
                    fieldId.intValue());
                Value val = entry.get(fieldId);
                // Output field's name, value, ID, and type.

                System.out.print(field.getName().toString());
                System.out.print(": " + val);
                System.out.print(" , ID: " + field.getFieldID());
                System.out.print(" , Field type: " +
                    field.getDataType());
                // Handle DateTime value.

                if ( field instanceof DateTimeField ){
                    System.out.print(", DateTime value: ");
                    Timestamp callDateTimeTS = (Timestamp)val.getValue();
                    if (callDateTimeTS != null)
                        System.out.print(callDateTimeTS.toDate());
                }
                System.out.println("");
            }
        } catch( ARException e ){
            ARExceptionHandler (e,
                "Problem while querying by entry ID.");
        }
    }
    // Retrieve entries from the form using the given qualification. With

    // the returned entry set, print out the ID of each entry and the

    // contents in its shortDescription field.

    void queryEntrysByQual(String qualStr) {
        System.out.println();
        System.out.println ("Retrieving entryies with qualification " +
            qualStr);
        try {
            // Retrieve the detail info of all fields from the form.

            List <Field> fields = server.getListFieldObjects(formName);
            // Create the search qualifier.

            QualifierInfo qual = server.parseQualification(qualStr,
                fields, null, Constants.AR_QUALCONTEXT_DEFAULT);
 
            int[] fieldIds = {2, 7, 8};
            OutputInteger nMatches = new OutputInteger();
            List<SortInfo> sortOrder = new ArrayList<SortInfo>();
            sortOrder.add(new SortInfo(2, Constants.AR_SORT_DESCENDING));
            // Retrieve entries from the form using the given

            // qualification.

            List<Entry> entryList = server.getListEntryObjects(
                formName, qual, 0, Constants.AR_NO_MAX_LIST_RETRIEVE,
                sortOrder, fieldIds, true, nMatches);
            
            System.out.println ("Query returned " + nMatches +
                " matches.");
            if( nMatches.intValue() > 0){
                // Print out the matches.

                System.out.println("Request Id " +
                    "Short Description" );
                for( int i = 0; i < entryList.size(); i++ ){
                    System.out.println (entryList.get(i).getEntryId() +
                        " " +
              entryList.get(i).get(Constants.AR_CORE_SHORT_DESCRIPTION));
                }
            }
        } catch( ARException e ) {
            ARExceptionHandler(e,
                "Problem while querying by qualifier. ");
        }
    }
    
    public void ARExceptionHandler(ARException e, String errMessage){
        System.out.println(errMessage);
        printStatusList(server.getLastStatus());
        System.out.print("Stack Trace:");
        e.printStackTrace();
    }
    public void printStatusList(List<StatusInfo> statusList) {
        if (statusList == null || statusList.size()==0) {
            System.out.println("Status List is empty.");
            return;
        }
        System.out.print("Message type: ");
        switch(statusList.get(0).getMessageType())
        {
            case Constants.AR_RETURN_OK:
                System.out.println("Note");
                break;
            case Constants.AR_RETURN_WARNING:
                System.out.println("Warning");
                break;
            case Constants.AR_RETURN_ERROR:
                System.out.println("Error");
                break;
            case Constants.AR_RETURN_FATAL:
                System.out.println("Fatal Error");
                break;
            default:
                System.out.println("Unknown (" +
                    statusList.get(0).getMessageType() + ")");
                break;
        }
        System.out.println("Status List:");
        for (int i=0; i < statusList.size(); i++) {
            System.out.println(statusList.get(i).getMessageText());
            System.out.println(statusList.get(i).getAppendedText());
        }
    }
    
    public void cleanup() {
        // Logout the user from the server. This releases the resource

        // allocated on the server for the user.

        server.logout();
        System.out.println();
        System.out.println("User logged out.");
    }
}


阅读(3093) | 评论(1) | 转发(0) |
给主人留下些什么吧!~~

chinaunix网友2009-10-16 15:10:40

现在3G网络,都不怎么好,也就中移动拔尖点。现在中国移动还大力推广OPhone 手机操作系统和手机,先进的平台和人性化的操作界面,将得到广大手机用户的追捧,为手机软件市场带来巨大商机。欢迎程序员访问。http://www.ophonesdn.com/