xe:beanNamePicker,无法将我的值从注释视图中获取到结果集中

时间:2017-10-05 19:50:29

标签: java xpages xpages-extlib

我已经设置了一个我想用于xe:beanNamePicker的java类。不知何故,我无法将创建的SimplePickerResult添加到结果集中。

package se.myorg.myproject.app;

import java.io.IOException;
import java.util.List;
import java.util.Properties;
import java.util.TreeSet;

import se.sebank.namis.utils.Utils;

import lotus.domino.Database;
import lotus.domino.Document;
import lotus.domino.DocumentCollection;
import lotus.domino.NotesException;
import lotus.domino.View;

import com.ibm.xsp.complex.ValueBindingObjectImpl;
import com.ibm.xsp.extlib.component.picker.data.INamePickerData;
import com.ibm.xsp.extlib.component.picker.data.IPickerEntry;
import com.ibm.xsp.extlib.component.picker.data.IPickerOptions;
import com.ibm.xsp.extlib.component.picker.data.IPickerResult;
import com.ibm.xsp.extlib.component.picker.data.SimplePickerResult;

public class DirectoryNamePicker extends ValueBindingObjectImpl implements INamePickerData {

    private Utils utils;

    Properties props;

    public DirectoryNamePicker(){
        //constructor
        utils = new Utils();
        utils.printToConsole(this.getClass().getSimpleName().toString() + " - DirectoryNamePicker() // constructor");
        try {
            props = utils.getDataSourceProperties();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }

  public String[] getSourceLabels () {
    // TODO Auto-generated method stub
    return null;
  }

  public boolean hasCapability (final int arg0) {
    // TODO Auto-generated method stub
    return false;
  }

  public List<IPickerEntry> loadEntries (final Object[] arg0, final String[] arg1) {
    // TODO Auto-generated method stub
    return null;
  }

  @SuppressWarnings("unchecked")
public IPickerResult readEntries (final IPickerOptions options) {
    String startKey = options.getStartKey();
    int count = options.getCount();
    TreeSet<IPickerEntry> entries = new TreeSet<IPickerEntry>();
    if (startKey != null) {
        // User is performing a search
        try {
          entries = this.dirLookup(startKey, count);
        } catch (NotesException e) {
          System.err.println("Exception trying to perform directory lookup: " + e.getMessage());
          e.printStackTrace();
        }
      }
    return new SimplePickerResult((List<IPickerEntry>) entries, -1);
  }

  public TreeSet<IPickerEntry> dirLookup(final String search, final int limit) throws NotesException {
    TreeSet<IPickerEntry> result = new TreeSet<IPickerEntry>();

    String server = props.getProperty("server_notesname");
    String filepath = props.getProperty("db_project_data");
    Database db = utils.getSession().getDatabase(server, filepath);

    View vw = db.getView("vw_all_todo_namespicker");
    vw.setAutoUpdate(false);

    DocumentCollection dc = vw.getAllDocumentsByKey(search, false);
    int count = 0;
    Document tmpdoc;
    Document doc = dc.getFirstDocument();

    while (doc != null && count < limit) {
        String person = doc.getItemValueString("app_ProjMgrName");
        IPickerEntry entry = new SimplePickerResult.Entry(person, person);
        result.add(entry);
        // result.add(entry does not seem to work
        tmpdoc = dc.getNextDocument();
        doc.recycle();
        doc = tmpdoc;
        count = count +1;
      }    
    vw.setAutoUpdate(true);
    return result;
  }

}

有没有人可以告诉我我做错了什么?我选择了一个树集而不是一个arraylist。这是因为我进入了一个包含大量多个条目的视图,所以我不想重复,并按值排序。

1 个答案:

答案 0 :(得分:2)

您将TreeSet转换为(List)行:

return new SimplePickerResult((List<IPickerEntry>) entries, -1);

因为SimplePickerResult需要一个List(它不接受Collection),但是TreeSet没有实现List,所以强制转换会失败。 您可能必须将其更改回ArrayList。 要进行排序,请尝试使用java.util.Collections.sort(List list,Comparator c)和自定义比较器来比较entry.getLabel()值,因为SimplePickerResult.Entry没有内置比较方法。