📄 query.java
字号:
package com.esri.solutions.jitk.services.personalization;
import java.lang.reflect.InvocationTargetException;
import java.text.MessageFormat;
import java.util.ArrayList;
import java.util.List;
import javax.xml.rpc.ServiceException;
import org.apache.commons.beanutils.BeanUtils;
import com.esri.solutions.jitk.personalization.dao.Criteria;
import com.esri.solutions.jitk.personalization.dao.IQueryDAO;
import com.esri.solutions.jitk.personalization.dao.InfoRecord;
import com.esri.solutions.jitk.personalization.dao.PageInfo;
import com.esri.solutions.jitk.personalization.dao.PersonalizationDAOException;
import com.esri.solutions.jitk.personalization.dao.QueryInfoRecord;
import com.esri.solutions.jitk.personalization.dao.QueryRecord;
import com.esri.solutions.jitk.personalization.data.beans.v1.QueryType;
import com.esri.solutions.jitk.services.common.ServicesException;
import com.esri.solutions.jitk.services.personalization.data.QueryData;
/**
* Web service endpoint for the Query service.
* <p>
* This class utilizes an {@link IQueryDAO} to perform the CRUD-type operations
* against the query datastore. The DAO is initialized via the {@link #onInit()}
* method, which retrieves a bean named {@code queryDAO} from the Spring
* configuration.
* </p>
* <p>
* If {@code queryDAO} is not configured properly in the Spring configuration
* file, this service will never initialize, and thus, will not be available.
* </p>
*/
public class Query extends PersonalizationService implements IQueryService {
/**
* The DAO object that is used to access the query datastore and perform the
* actual CRUD operations.
*/
private IQueryDAO _dao = null;
/**
* Default constructor
*/
public Query() {
}
/* (non-Javadoc)
* @see com.esri.solutions.jitk.services.personalization.PersonalizationService#onInit()
*/
@Override
protected void onInit() throws ServiceException {
super.onInit();
Object o = this.getWebApplicationContext().getBean("queryDAO");
if (o instanceof IQueryDAO && o != null) {
IQueryDAO dao = (IQueryDAO) o;
setDAO(dao);
} else {
throw new ServiceException(
"queryDAO is improperly configured and cannot be set on the Query service");
}
}
/* (non-Javadoc)
* @see com.esri.solutions.jitk.services.personalization.PersonalizationService#checkForId(java.lang.String)
*/
@Override
protected boolean checkForId(String id) throws ServicesException {
QueryData d = selectOne(id);
return (d != null) && (d.getData() != null);
}
/**
* Sets the {@link IQueryDAO} instance that is used to access and manipulate
* the query datastore.
*
* @param dao
* {@link IQueryDAO} instance, which cannot be null.
*/
public void setDAO(IQueryDAO dao) {
if (dao == null) {
throw new NullPointerException("dao cannot be null");
}
_dao = dao;
}
/*
* (non-Javadoc)
*
* @see com.esri.solutions.jitk.services.personalization.IQueryService#getDAO()
*/
public IQueryDAO getDAO() {
return _dao;
}
/*
* (non-Javadoc)
*
* @see com.esri.solutions.jitk.services.personalization.IQueryService#delete(java.lang.String)
*/
public void delete(String id) throws ServicesException {
if (_dao == null) {
throw new IllegalStateException("DAO has not yet been set.");
}
if (id == null) {
throw new NullPointerException("id cannot be null");
}
try {
_dao.delete(id);
} catch (PersonalizationDAOException e) {
throw new ServicesException(e);
}
}
/*
* (non-Javadoc)
*
* @see com.esri.solutions.jitk.services.personalization.IQueryService#insert(com.esri.solutions.jitk.services.personalization.data.QueryData)
*/
public void insert(QueryData data) throws ServicesException {
if (_dao == null) {
throw new IllegalStateException("DAO has not yet been set.");
}
if (data == null) {
throw new NullPointerException("data cannot be null");
}
try {
QueryRecord record = createRecord(data);
if(okToInsert(record.getId())) {
_dao.insert(record);
} else {
throw new ServicesException(MessageFormat.format(ERROR_ID_ALREADY_EXISTS,"Query",record.getId()));
}
} catch (IllegalAccessException e) {
throw new ServicesException(e);
} catch (InvocationTargetException e) {
throw new ServicesException(e);
} catch (PersonalizationDAOException e) {
throw new ServicesException(e);
}
}
/*
* (non-Javadoc)
*
* @see com.esri.solutions.jitk.services.personalization.IQueryService#selectAll(com.esri.solutions.jitk.personalization.dao.Criteria)
*/
public List<QueryData> selectAll(Criteria criteria)
throws ServicesException {
if (_dao == null) {
throw new IllegalStateException("DAO has not yet been set.");
}
if (criteria == null) {
throw new NullPointerException("criteria cannot be null");
}
List<QueryInfoRecord> originalResults = new ArrayList<QueryInfoRecord>();
List<QueryData> convertedResults = new ArrayList<QueryData>();
try {
originalResults = _dao.selectAll(criteria);
for (QueryInfoRecord record : originalResults) {
QueryData data = createData(record);
convertedResults.add(data);
}
} catch (IllegalAccessException e) {
throw new ServicesException(e);
} catch (InvocationTargetException e) {
throw new ServicesException(e);
} catch (PersonalizationDAOException e) {
throw new ServicesException(e);
}
return convertedResults;
}
/*
* (non-Javadoc)
*
* @see com.esri.solutions.jitk.services.personalization.IQueryService#selectAllCount(com.esri.solutions.jitk.personalization.dao.Criteria)
*/
public int selectAllCount(Criteria criteria) throws ServicesException {
if (_dao == null) {
throw new IllegalStateException("DAO has not yet been set.");
}
if (criteria == null) {
throw new NullPointerException("criteria cannot be null");
}
int results = -1;
try {
results = _dao.selectAllCount(criteria);
} catch (PersonalizationDAOException e) {
throw new ServicesException(e);
}
return results;
}
/*
* (non-Javadoc)
*
* @see com.esri.solutions.jitk.services.personalization.IQueryService#selectOne(java.lang.String)
*/
public QueryData selectOne(String id) throws ServicesException {
if (_dao == null) {
throw new IllegalStateException("DAO has not yet been set.");
}
if (id == null) {
throw new NullPointerException("id cannot be null");
}
QueryRecord original = null;
QueryData data = null;
try {
original = _dao.selectOne(id);
data = createData(original);
} catch (IllegalAccessException e) {
throw new ServicesException(e);
} catch (InvocationTargetException e) {
throw new ServicesException(e);
} catch (PersonalizationDAOException e) {
throw new ServicesException(e);
}
return data;
}
/*
* (non-Javadoc)
*
* @see com.esri.solutions.jitk.services.personalization.IQueryService#selectPage(com.esri.solutions.jitk.personalization.dao.Criteria,
* com.esri.solutions.jitk.personalization.dao.PageInfo)
*/
public List<QueryData> selectPage(Criteria criteria, PageInfo pageInfo)
throws ServicesException {
if (_dao == null) {
throw new IllegalStateException("DAO has not yet been set.");
}
if (criteria == null) {
throw new NullPointerException("criteria cannot be null");
}
if (pageInfo == null) {
throw new NullPointerException("pageInfo cannot be null");
}
List<QueryInfoRecord> originalResults = new ArrayList<QueryInfoRecord>();
List<QueryData> convertedResults = new ArrayList<QueryData>();
try {
originalResults = _dao.selectPage(criteria, pageInfo);
for (QueryInfoRecord record : originalResults) {
QueryData data = createData(record);
convertedResults.add(data);
}
} catch (IllegalAccessException e) {
throw new ServicesException(e);
} catch (InvocationTargetException e) {
throw new ServicesException(e);
} catch (PersonalizationDAOException e) {
throw new ServicesException(e);
}
return convertedResults;
}
/*
* (non-Javadoc)
*
* @see com.esri.solutions.jitk.services.personalization.IQueryService#update(com.esri.solutions.jitk.services.personalization.data.QueryData)
*/
public void update(QueryData data) throws ServicesException {
if (_dao == null) {
throw new IllegalStateException("DAO has not yet been set.");
}
if (data == null) {
throw new NullPointerException("data cannot be null");
}
try {
QueryRecord record = createRecord(data);
_dao.update(record);
} catch (IllegalAccessException e) {
throw new ServicesException(e);
} catch (InvocationTargetException e) {
throw new ServicesException(e);
} catch (PersonalizationDAOException e) {
throw new ServicesException(e);
}
}
/**
* Converts a {@link InfoRecord} into an {@link QueryData}
*
* @param record
* The {@link InfoRecord} to convert. {@code data} can be
* {@code null}, in which case an new, empty {@link QueryData}
* will be returned.
* @return {@link QueryData} representation of the {@link InfoRecord}
* @throws IllegalAccessException
* if an IllegalAccessException occurs when the bean properties
* are being copied
* @throws InvocationTargetException
* if an InvocationTargetException occurs when the bean
* properties are being copied
*/
private QueryData createData(InfoRecord record)
throws IllegalAccessException, InvocationTargetException {
QueryData data = new QueryData();
if (record != null) {
BeanUtils.copyProperties(data, record);
}
return data;
}
/**
* Converts a {@link QueryData} into an {@link QueryRecord}
*
* @param data
* The {@link QueryData} to convert. {@code data} can be
* {@code null}, in which case an new, empty {@link QueryRecord}
* will be returned.
* @return {@link QueryRecord} representation of the {@link QueryData}
* @throws IllegalAccessException
* if an IllegalAccessException occurs when the bean properties
* are being copied
* @throws InvocationTargetException
* if an InvocationTargetException occurs when the bean
* properties are being copied
*/
private QueryRecord createRecord(QueryData data)
throws IllegalAccessException, InvocationTargetException,
ServicesException {
QueryRecord record = new QueryRecord();
if (data != null) {
QueryType t = data.dataAsQuery();
BeanUtils.copyProperties(record, data);
BeanUtils.copyProperties(record, t);
}
record.setCreator(getUsername());
return record;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -