📄 bookmark.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.BookmarkInfoRecord;
import com.esri.solutions.jitk.personalization.dao.BookmarkRecord;
import com.esri.solutions.jitk.personalization.dao.Criteria;
import com.esri.solutions.jitk.personalization.dao.IBookmarkDAO;
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.data.beans.v1.BookmarkType;
import com.esri.solutions.jitk.services.common.ServicesException;
import com.esri.solutions.jitk.services.personalization.data.BookmarkData;
/**
* Web service endpoint for the Bookmark service.
* <p>
* This class utilizes an {@link IBookmarkDAO} to perform the CRUD-type
* operations against the bookmark datastore. The DAO is initialized via the
* {@link #onInit()} method, which retrieves a bean named {@code bookmarkDAO}
* from the Spring configuration.
* </p>
* <p>
* If {@code bookmarkDAO} is not configured properly in the Spring configuration
* file, this service will never initialize, and thus, will not be available.
* </p>
*/
public class Bookmark extends PersonalizationService implements
IBookmarkService {
/**
* The DAO object that is used to access the bookmark datastore and perform
* the actual CRUD operations.
*/
private IBookmarkDAO _dao = null;
/**
* Default constructor
*/
public Bookmark() {
}
@Override
protected void onInit() throws ServiceException {
super.onInit();
Object o = this.getWebApplicationContext().getBean("bookmarkDAO");
if (o instanceof IBookmarkDAO && o != null) {
IBookmarkDAO dao = (IBookmarkDAO) o;
setDAO(dao);
} else {
throw new ServiceException(
"bookmarkDAO is improperly configured and cannot be set on the Bookmark service");
}
}
/*
* (non-Javadoc)
*
* @see com.esri.solutions.jitk.services.personalization.PersonalizationService#checkForId(java.lang.String)
*/
@Override
protected boolean checkForId(String id) throws ServicesException {
BookmarkData d = selectOne(id);
return (d != null) && (d.getData() != null);
}
/**
* Sets the {@link IBookmarkDAO} instance that is used to access and
* manipulate the datastore.
*
* @param dao
* {@link IBookmarkDAO} instance, which cannot be null.
*/
public void setDAO(IBookmarkDAO dao) {
if (dao == null) {
throw new NullPointerException("dao cannot be null");
}
_dao = dao;
}
/*
* (non-Javadoc)
*
* @see com.esri.solutions.jitk.services.personalization.IBookmarkService#getDAO()
*/
public IBookmarkDAO getDAO() {
return _dao;
}
/*
* (non-Javadoc)
*
* @see com.esri.solutions.jitk.services.personalization.IBookmarkService#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.IBookmarkService#insert(com.esri.solutions.jitk.services.personalization.data.BookmarkData)
*/
public void insert(BookmarkData 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 {
BookmarkRecord record = createRecord(data);
if (okToInsert(record.getId())) {
_dao.insert(record);
} else {
throw new ServicesException(MessageFormat.format(
ERROR_ID_ALREADY_EXISTS, "Bookmark", 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.IBookmarkService#selectAll(com.esri.solutions.jitk.personalization.dao.Criteria)
*/
public List<BookmarkData> 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<BookmarkInfoRecord> originalResults = new ArrayList<BookmarkInfoRecord>();
List<BookmarkData> convertedResults = new ArrayList<BookmarkData>();
try {
originalResults = _dao.selectAll(criteria);
for (BookmarkInfoRecord record : originalResults) {
BookmarkData 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.IBookmarkService#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.IBookmarkService#selectOne(java.lang.String)
*/
public BookmarkData 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");
}
BookmarkRecord original = null;
BookmarkData 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.IBookmarkService#selectPage(com.esri.solutions.jitk.personalization.dao.Criteria,
* com.esri.solutions.jitk.personalization.dao.PageInfo)
*/
public List<BookmarkData> 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<BookmarkInfoRecord> originalResults = new ArrayList<BookmarkInfoRecord>();
List<BookmarkData> convertedResults = new ArrayList<BookmarkData>();
try {
originalResults = _dao.selectPage(criteria, pageInfo);
for (BookmarkInfoRecord record : originalResults) {
BookmarkData 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.IBookmarkService#update(com.esri.solutions.jitk.services.personalization.data.BookmarkData)
*/
public void update(BookmarkData 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 {
BookmarkRecord 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 BookmarkData}
*
* @param record
* The {@link InfoRecord} to convert. {@code data} can be
* {@code null}, in which case an new, empty
* {@link BookmarkData} will be returned.
* @return {@link BookmarkData} 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 BookmarkData createData(InfoRecord record)
throws IllegalAccessException, InvocationTargetException {
BookmarkData data = new BookmarkData();
if (record != null) {
BeanUtils.copyProperties(data, record);
}
return data;
}
/**
* Converts a {@link BookmarkData} into an {@link BookmarkRecord}
*
* @param data
* The {@link BookmarkData} to convert. {@code data} can be
* {@code null}, in which case an new, empty
* {@link BookmarkRecord} will be returned.
* @return {@link BookmarkRecord} representation of the {@link BookmarkData}
* @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 BookmarkRecord createRecord(BookmarkData data)
throws IllegalAccessException, InvocationTargetException,
ServicesException {
BookmarkRecord record = new BookmarkRecord();
if (data != null) {
BookmarkType t = data.dataAsBookmark();
BeanUtils.copyProperties(record, data);
BeanUtils.copyProperties(record, t);
}
record.setCreator(getUsername());
return record;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -