/**
* Copyright 1999-2009 The Pegadi Team
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.pegadi.artis.text;
import org.pegadi.model.LoginContext;
import org.pegadi.sources.Source;
import org.pegadi.sources.SourceFrame;
import org.pegadi.sources.search.NameTerm;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.rmi.RemoteException;
import java.util.ArrayList;
import java.util.Collections;
public class SourceDBPersonResolver implements PersonResolver {
private final Logger log = LoggerFactory.getLogger(getClass());
public SourceDBPersonResolver() {
}
public java.util.List<PersonInfo> search(String searchString) {
NameTerm nameterm = new NameTerm(searchString);
java.util.List<Source> matches;
try {
matches = LoginContext.server.getSourcesBySearchTerm(nameterm, LoginContext.sessionKey);
} catch (RemoteException e) {
return Collections.emptyList();
}
java.util.List<PersonInfo> persons = new ArrayList<PersonInfo>();
for(Source match : matches) {
persons.add(new PersonInfoWrapper(match));
}
return persons;
}
public PersonInfo addPerson(String person, PersonInText personInText) {
log.debug("To be implemented: " +person);
Source source = new Source();
source.setName(person);
SourceFrame f = SourceFrame.fetchUniqueSourceFrame(source);
f.pack();
f.setVisible(true);
if(source.getID() < 0) {
return null;
} else {
return new PersonInfoWrapper(source);
}
}
private class PersonInfoWrapper extends PersonInfo {
Source source;
public PersonInfoWrapper(Source source) {
this.source = source;
}
public String getName() {
return source.getName();
}
public String getId() {
return Integer.toString(source.getID());
}
public String getSource() {
return "PegadiSourceDB";
}
public String toString() {
return getName();
}
}
}