Commit 5faf6bf5 authored by doc@bgerp.org's avatar doc@bgerp.org

p12134 Code format, cleanup.

parent 923c2b93
C: Cleanup, format DAO, actions.
\ No newline at end of file
package ru.bgcrm.cache;
/* package ru.bgcrm.cache;
import java.sql.Connection;
......@@ -45,4 +45,4 @@ public class AddressCache
return result;
}
}
} */
\ No newline at end of file
package ru.bgcrm.cache;
public abstract class Cache<C extends Cache<C>>
{
protected abstract C newInstance();
protected Cache()
{}
public abstract class Cache<C extends Cache<C>> {
protected abstract C newInstance();
protected Cache() {}
}
\ No newline at end of file
......@@ -8,37 +8,31 @@ import org.apache.log4j.Logger;
import ru.bgcrm.util.sql.SQLUtils;
public class CacheHolder<C extends Cache<C>>
{
private C factory;
private volatile C cache;
private volatile Date lastAcces;
private Logger log = Logger.getLogger( this.getClass() );
public CacheHolder( C factory )
{
this.factory = factory;
}
public C getInstance()
{
synchronized( factory )
{
if( cache == null || !DateUtils.isSameDay( lastAcces, new Date() ) )
{
log.debug( "cache newInstance" );
cache = factory.newInstance();
lastAcces = new Date();
}
}
return cache;
}
public void flush( Connection con )
{
SQLUtils.commitConnection( con );
cache = null;
lastAcces = null;
}
public class CacheHolder<C extends Cache<C>> {
private C factory;
private volatile C cache;
private volatile Date lastAcces;
private Logger log = Logger.getLogger(this.getClass());
public CacheHolder(C factory) {
this.factory = factory;
}
public C getInstance() {
synchronized (factory) {
if (cache == null || !DateUtils.isSameDay(lastAcces, new Date())) {
log.debug("cache newInstance");
cache = factory.newInstance();
lastAcces = new Date();
}
}
return cache;
}
public void flush(Connection con) {
SQLUtils.commitConnection(con);
cache = null;
lastAcces = null;
}
}
\ No newline at end of file
......@@ -9,49 +9,40 @@ import ru.bgcrm.dao.work.WorkTypeDAO;
import ru.bgcrm.model.work.WorkType;
import ru.bgcrm.util.Setup;
public class CallboardCache
extends Cache<CallboardCache>
{
private static Logger log = Logger.getLogger( CallboardCache.class );
private static CacheHolder<CallboardCache> holder = new CacheHolder<CallboardCache>( new CallboardCache() );
public static WorkType getWorkType( int id )
{
return holder.getInstance().workTypeMap.get( id );
}
public static Map<Integer, WorkType> getWorkTypeMap()
{
return holder.getInstance().workTypeMap;
}
public static void flush( Connection con )
{
holder.flush( con );
}
// конец статической части
private Map<Integer, WorkType> workTypeMap;
@Override
protected CallboardCache newInstance()
{
CallboardCache result = new CallboardCache();
Connection con = Setup.getSetup().getDBConnectionFromPool();
try
{
WorkTypeDAO typeDao = new WorkTypeDAO( con );
result.workTypeMap = typeDao.getWorkTypeMap();
}
catch( Exception ex )
{
log.error( ex.getMessage(), ex );
}
return result;
}
public class CallboardCache extends Cache<CallboardCache> {
private static Logger log = Logger.getLogger(CallboardCache.class);
private static CacheHolder<CallboardCache> holder = new CacheHolder<CallboardCache>(new CallboardCache());
public static WorkType getWorkType(int id) {
return holder.getInstance().workTypeMap.get(id);
}
public static Map<Integer, WorkType> getWorkTypeMap() {
return holder.getInstance().workTypeMap;
}
public static void flush(Connection con) {
holder.flush(con);
}
// конец статической части
private Map<Integer, WorkType> workTypeMap;
@Override
protected CallboardCache newInstance() {
CallboardCache result = new CallboardCache();
Connection con = Setup.getSetup().getDBConnectionFromPool();
try {
WorkTypeDAO typeDao = new WorkTypeDAO(con);
result.workTypeMap = typeDao.getWorkTypeMap();
} catch (Exception ex) {
log.error(ex.getMessage(), ex);
}
return result;
}
}
\ No newline at end of file
......@@ -12,60 +12,47 @@ import ru.bgcrm.model.CustomerGroup;
import ru.bgcrm.util.Setup;
import ru.bgcrm.util.sql.SQLUtils;
public class CustomerGroupCache
extends Cache<CustomerGroupCache>
{
private static Logger log = Logger.getLogger( CustomerGroupCache.class );
private static CacheHolder<CustomerGroupCache> holder = new CacheHolder<CustomerGroupCache>( new CustomerGroupCache() );
public class CustomerGroupCache extends Cache<CustomerGroupCache> {
private static Logger log = Logger.getLogger(CustomerGroupCache.class);
public static List<CustomerGroup> getGroupList()
{
return holder.getInstance().groupList;
}
public static Map<Integer, CustomerGroup> getGroupMap()
{
return holder.getInstance().groupMapById;
}
public static void flush( Connection con )
{
holder.flush( con );
}
// конец статической части
private List<CustomerGroup> groupList;
private Map<Integer, CustomerGroup> groupMapById;
@Override
protected CustomerGroupCache newInstance()
{
CustomerGroupCache result = new CustomerGroupCache();
Setup setup = Setup.getSetup();
private static CacheHolder<CustomerGroupCache> holder = new CacheHolder<CustomerGroupCache>(new CustomerGroupCache());
Connection con = setup.getDBConnectionFromPool();
try
{
result.groupList = new CustomerGroupDAO( con ).getGroupList();
result.groupMapById = new HashMap<Integer, CustomerGroup>();
for( CustomerGroup group : result.groupList )
{
result.groupMapById.put( group.getId(), group );
}
}
catch( Exception e )
{
log.error( e.getMessage(), e );
}
finally
{
SQLUtils.closeConnection( con );
}
return result;
}
public static List<CustomerGroup> getGroupList() {
return holder.getInstance().groupList;
}
public static Map<Integer, CustomerGroup> getGroupMap() {
return holder.getInstance().groupMapById;
}
public static void flush(Connection con) {
holder.flush(con);
}
// конец статической части
private List<CustomerGroup> groupList;
private Map<Integer, CustomerGroup> groupMapById;
@Override
protected CustomerGroupCache newInstance() {
CustomerGroupCache result = new CustomerGroupCache();
Setup setup = Setup.getSetup();
Connection con = setup.getDBConnectionFromPool();
try {
result.groupList = new CustomerGroupDAO(con).getGroupList();
result.groupMapById = new HashMap<Integer, CustomerGroup>();
for (CustomerGroup group : result.groupList) {
result.groupMapById.put(group.getId(), group);
}
} catch (Exception e) {
log.error(e.getMessage(), e);
} finally {
SQLUtils.closeConnection(con);
}
return result;
}
}
\ No newline at end of file
package ru.bgcrm.cache;
/*
import java.util.HashMap;
import java.util.Map;
public class ObjectTypeCache
extends Cache<ObjectTypeCache>
{
private static CacheHolder<ParameterCache> holder = new CacheHolder<ParameterCache>( new ParameterCache() );
//public static String getEntityTypeTitle( String entityType )
// конец статической части
private Map<String, String> entityTitleMap;
@Override
public ObjectTypeCache newInstance()
{
ObjectTypeCache result = new ObjectTypeCache();
result.entityTitleMap = new HashMap<String, String>();
return result;
}
}
*/
This diff is collapsed.
......@@ -15,109 +15,89 @@ import ru.bgcrm.util.Setup;
import ru.bgcrm.util.Utils;
import ru.bgcrm.util.sql.SQLUtils;
public class ProcessQueueCache
extends Cache<ProcessQueueCache>
{
private static Logger log = Logger.getLogger( ProcessQueueCache.class );
private static CacheHolder<ProcessQueueCache> holder = new CacheHolder<ProcessQueueCache>( new ProcessQueueCache() );
public static Queue getQueue( int id, User user )
{
Queue result = holder.getInstance().queueMap.get( id );
// фильтр по разрешённым очередям процессов
if( result != null && user != null && !user.getQueueIds().contains( result.getId() ) )
{
result = null;
}
return result;
}
public static Queue getQueue( int id )
{
return holder.getInstance().queueMap.get( id );
}
public static Map<Integer, Queue> getQueueMap()
{
return holder.getInstance().queueMap;
}
public static List<Queue> getQueueList()
{
return holder.getInstance().queueList;
}
public static List<Queue> getUserQueueList( User user )
{
List<Queue> result = new ArrayList<Queue>();
for( Queue queue : holder.getInstance().queueList )
{
if( user.getQueueIds().contains( queue.getId() ) )
{
result.add( queue );
}
}
return result;
}
public static void flush( Connection con )
{
holder.flush( con );
}
// конец статической части
private Map<Integer, Queue> queueMap;
private List<Queue> queueList;
@Override
protected ProcessQueueCache newInstance()
{
ProcessQueueCache result = new ProcessQueueCache();
Connection con = Setup.getSetup().getDBConnectionFromPool();
try
{
result.queueMap = new HashMap<Integer, Queue>();
result.queueList = new ArrayList<Queue>();
QueueDAO queueDAO = new QueueDAO( con );
for( Queue queue : queueDAO.getQueueList() )
{
// выбор явно указанных в конфигурации очереди типов процессов
queue.setProcessTypeIds( queueDAO.getQueueProcessTypeIds( queue.getId() ) );
if( log.isDebugEnabled() )
{
log.debug( "Queue " + queue.getId() + " selected process types: " + Utils.toString( queue.getProcessTypeIds() ) );
}
// выбор дочерних типов привязанных процессов
queue.setProcessTypeIds( ProcessTypeCache.getTypeTreeRoot().getSelectedChildIds( queue.getProcessTypeIds() ) );
if( log.isDebugEnabled() )
{
log.debug( "Queue " + queue.getId() + " process types with childs: " + Utils.toString( queue.getProcessTypeIds() ) );
}
queue.extractFiltersAndSorts();
result.queueMap.put( queue.getId(), queue );
result.queueList.add( queue );
}
}
catch( Exception e )
{
log.error( e.getMessage(), e );
}
finally
{
SQLUtils.closeConnection( con );
}
return result;
}
public class ProcessQueueCache extends Cache<ProcessQueueCache> {
private static Logger log = Logger.getLogger(ProcessQueueCache.class);
private static CacheHolder<ProcessQueueCache> holder = new CacheHolder<ProcessQueueCache>(new ProcessQueueCache());
public static Queue getQueue(int id, User user) {
Queue result = holder.getInstance().queueMap.get(id);
// фильтр по разрешённым очередям процессов
if (result != null && user != null && !user.getQueueIds().contains(result.getId())) {
result = null;
}
return result;
}
public static Queue getQueue(int id) {
return holder.getInstance().queueMap.get(id);
}
public static Map<Integer, Queue> getQueueMap() {
return holder.getInstance().queueMap;
}
public static List<Queue> getQueueList() {
return holder.getInstance().queueList;
}
public static List<Queue> getUserQueueList(User user) {
List<Queue> result = new ArrayList<Queue>();
for (Queue queue : holder.getInstance().queueList) {
if (user.getQueueIds().contains(queue.getId())) {
result.add(queue);
}
}
return result;
}
public static void flush(Connection con) {
holder.flush(con);
}
// конец статической части
private Map<Integer, Queue> queueMap;
private List<Queue> queueList;
@Override
protected ProcessQueueCache newInstance() {
ProcessQueueCache result = new ProcessQueueCache();
Connection con = Setup.getSetup().getDBConnectionFromPool();
try {
result.queueMap = new HashMap<Integer, Queue>();
result.queueList = new ArrayList<Queue>();
QueueDAO queueDAO = new QueueDAO(con);
for (Queue queue : queueDAO.getQueueList()) {
// выбор явно указанных в конфигурации очереди типов процессов
queue.setProcessTypeIds(queueDAO.getQueueProcessTypeIds(queue.getId()));
if (log.isDebugEnabled()) {
log.debug("Queue " + queue.getId() + " selected process types: " + Utils.toString(queue.getProcessTypeIds()));
}
// выбор дочерних типов привязанных процессов
queue.setProcessTypeIds(ProcessTypeCache.getTypeTreeRoot().getSelectedChildIds(queue.getProcessTypeIds()));
if (log.isDebugEnabled()) {
log.debug("Queue " + queue.getId() + " process types with childs: " + Utils.toString(queue.getProcessTypeIds()));
}
queue.extractFiltersAndSorts();
result.queueMap.put(queue.getId(), queue);
result.queueList.add(queue);
}
} catch (Exception e) {
log.error(e.getMessage(), e);
} finally {
SQLUtils.closeConnection(con);
}
return result;
}
}
This diff is collapsed.
......@@ -12,50 +12,41 @@ import ru.bgcrm.util.Setup;
import ru.bgcrm.util.Utils;
import ru.bgcrm.util.sql.ConnectionSet;
public class UserGroupRoleCache
extends Cache<UserGroupRoleCache>
{
private static CacheHolder<UserGroupRoleCache> holder = new CacheHolder<UserGroupRoleCache>( new UserGroupRoleCache() );
static
{
EventProcessor.subscribe( new EventListener<SetupChangedEvent>()
{
@Override
public void notify( SetupChangedEvent e, ConnectionSet connectionSet )
{
holder.flush( connectionSet.getConnection() );
}
}, SetupChangedEvent.class );
}
public static List<IdTitle> getUserGroupRoleList()
{
return holder.getInstance().userGroupRoleList;
}
public static Map<Integer, IdTitle> getUserGroupRoleMap()
{
return holder.getInstance().userGroupRoleMap;
}
// конец статической части
private Map<Integer, IdTitle> userGroupRoleMap;
private List<IdTitle> userGroupRoleList;
@Override
protected UserGroupRoleCache newInstance()
{
UserGroupRoleCache result = new UserGroupRoleCache();
result.userGroupRoleList = Utils.parseIdTitleList( Setup.getSetup().get( "processGroupRoles", "0:Выполнение" ) );
result.userGroupRoleMap = new HashMap<Integer, IdTitle>( result.userGroupRoleList.size() );
for( IdTitle role : result.userGroupRoleList )
{
result.userGroupRoleMap.put( role.getId(), role );
}
return result;
}
public class UserGroupRoleCache extends Cache<UserGroupRoleCache> {
private static CacheHolder<UserGroupRoleCache> holder = new CacheHolder<UserGroupRoleCache>(new UserGroupRoleCache());
static {
EventProcessor.subscribe(new EventListener<SetupChangedEvent>() {
@Override
public void notify(SetupChangedEvent e, ConnectionSet connectionSet) {
holder.flush(connectionSet.getConnection());
}
}, SetupChangedEvent.class);
}
public static List<IdTitle> getUserGroupRoleList() {
return holder.getInstance().userGroupRoleList;
}
public static Map<Integer, IdTitle> getUserGroupRoleMap() {
return holder.getInstance().userGroupRoleMap;
}
// конец статической части
private Map<Integer, IdTitle> userGroupRoleMap;
private List<IdTitle> userGroupRoleList;
@Override
protected UserGroupRoleCache newInstance() {
UserGroupRoleCache result = new UserGroupRoleCache();
result.userGroupRoleList = Utils.parseIdTitleList(Setup.getSetup().get("processGroupRoles", "0:Выполнение"));
result.userGroupRoleMap = new HashMap<Integer, IdTitle>(result.userGroupRoleList.size());
for (IdTitle role : result.userGroupRoleList) {
result.userGroupRoleMap.put(role.getId(), role);
}
return result;
}
}
\ No newline at end of file
......@@ -25,105 +25,105 @@ import ru.bgcrm.util.Utils;
import ru.bgcrm.util.sql.SQLUtils;
public class UserNewsCache extends Cache<UserNewsCache> {
private static final Logger log = Logger.getLogger(UserNewsCache.class);
private static CacheHolder<UserNewsCache> holder = new CacheHolder<>(new UserNewsCache());
private static volatile long lastFullFlush = System.currentTimeMillis();
public static NewsInfoEvent getUserEvent(Connection con, int userId) throws Exception {
// периодический полный сброс кэша новостей, нужно:
// - в режиме нескольких серверов на одной базе, чтобы новости приходили
// - обновления информации о новых EMail сообщениях
long flushInterVal = Setup.getSetup().getLong("flush.news.everySeconds", 0) * 1000;
if (flushInterVal > 0) {
long currentTime = System.currentTimeMillis();
if (currentTime > lastFullFlush + flushInterVal)
flush(con);
}
NewsInfoEvent result = holder.getInstance().userInfoMap.get(userId);
if (result == null) {
final NewsDAO newsDao = new NewsDAO(con);
List<Integer> notPopupNews = new ArrayList<>();
// непрочитанные новости пользователя
SearchResult<News> searchResult = new SearchResult<>();
newsDao.searchNewsList(searchResult, userId, false, null);
List<Integer> popupNews = new ArrayList<>();
for (News news : searchResult.getList()) {
if (news.isPopup())
popupNews.add(news.getId());
else
notPopupNews.add(news.getId());
}
final User user = UserCache.getUser(