0

I am trying to extract data from the Samples namespace that comes with Intersystems Cache install. Specifically, I am trying to retrieve Sample.Company global data using XEP. Inorder to achieve this, I created Sample.Company class like this -

package Sample;

public class Company {

    public Long id;
    public String mission;
    public String name;
    public Long revenue;
    public String taxId;

    public Company(Long id, String mission, String name, Long revenue,
            String taxId) {
        this.id = id;
        this.mission = mission;
        this.name = name;
        this.revenue = revenue;
        this.taxId = taxId;
    }

    public Company() {
    }
}

XEP related code looks like this -

import java.util.ArrayList;
import java.util.List;

import org.springframework.stereotype.Service;

import Sample.Company;

import com.intersys.xep.Event;
import com.intersys.xep.EventPersister;
import com.intersys.xep.EventQuery;
import com.intersys.xep.EventQueryIterator;
import com.intersys.xep.PersisterFactory;
import com.intersys.xep.XEPException;

@Service
public class CompanyService {

    public List<Company> fetch() {
        EventPersister myPersister = PersisterFactory.createPersister();
        myPersister.connect("SAMPLES", "user", "pwd");
        try { // delete any existing SingleStringSample events, then import
                // new ones
            Event.isEvent("Sample.Company");
            myPersister.deleteExtent("Sample.Company");
            String[] generatedClasses = myPersister.importSchema("Sample.Company");
            for (int i = 0; i < generatedClasses.length; i++) {
                System.out.println("Event class " + generatedClasses[i]
                        + " successfully imported.");
            }
        } catch (XEPException e) {
            System.out.println("import failed:\n" + e);
            throw new RuntimeException(e);
        }
        EventQuery<Company> myQuery = null;
        List<Company> list = new ArrayList<Company>();
        try {
            Event newEvent = myPersister.getEvent("Sample.Company");
            String sql = "Select * from Sample.Company";
            myQuery = newEvent.createQuery(sql);
            newEvent.close();
            myQuery.execute();
            EventQueryIterator<Company> iterator = myQuery.getIterator();
            while (iterator.hasNext()) {
                Company c = iterator.next();
                System.out.println(c);
                list.add(c);
            }
            myQuery.close();
            myPersister.close();
            return list;
        } catch (XEPException e) {
            System.out.println("createQuery failed:\n" + e);
            throw new RuntimeException(e);
        }
    }

}

When I try executing the fetch() method of the above class, I am seeing the following exception -

 com.intersys.xep.XEPException: Cannot import - extent for Sample.Company not empty.
    at com.intersys.xep.internal.Generator.generate(Generator.java:52)
    at com.intersys.xep.EventPersister.importSchema(EventPersister.java:954)
    at com.intersys.xep.EventPersister.importSchema(EventPersister.java:363)

I got the simple string example working. Does it mean, we can not read the existing data using XEP? If we can read, Could some please help me in resolving the above issue? Thanks in advance.

user3600073
  • 1,773
  • 3
  • 18
  • 21

1 Answers1

0

You are trying to create a new class named Sample.Company in your instance:

String[] generatedClasses = myPersister.importSchema("Sample.Company");

But you still have data and an existing class there.

kazamatzuri
  • 413
  • 1
  • 3
  • 12