001 /*
002 GRANITE DATA SERVICES
003 Copyright (C) 2012 GRANITE DATA SERVICES S.A.S.
004
005 This file is part of Granite Data Services.
006
007 Granite Data Services is free software; you can redistribute it and/or modify
008 it under the terms of the GNU Library General Public License as published by
009 the Free Software Foundation; either version 2 of the License, or (at your
010 option) any later version.
011
012 Granite Data Services is distributed in the hope that it will be useful, but
013 WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
014 FITNESS FOR A PARTICULAR PURPOSE. See the GNU Library General Public License
015 for more details.
016
017 You should have received a copy of the GNU Library General Public License
018 along with this library; if not, see <http://www.gnu.org/licenses/>.
019 */
020
021 package org.granite.client.tide.cdi;
022
023 import javax.annotation.PostConstruct;
024 import javax.enterprise.context.ApplicationScoped;
025 import javax.enterprise.inject.Produces;
026 import javax.enterprise.inject.spi.BeanManager;
027 import javax.inject.Inject;
028
029 import org.granite.client.tide.Context;
030 import org.granite.client.tide.EventBus;
031 import org.granite.client.tide.Platform;
032 import org.granite.client.tide.data.Conflicts;
033 import org.granite.client.tide.data.DataConflictListener;
034 import org.granite.client.tide.data.EntityManager;
035 import org.granite.client.tide.data.EntityManager.UpdateKind;
036 import org.granite.client.tide.data.spi.DataManager;
037 import org.granite.client.tide.impl.SimpleContextManager;
038
039 /**
040 * @author William DRAI
041 */
042 @ApplicationScoped
043 public class CDIContextManager extends SimpleContextManager {
044
045 @Inject
046 private BeanManager beanManager;
047
048 protected CDIContextManager() {
049 super();
050 // CDI proxying...
051 }
052
053 @Inject
054 public CDIContextManager(Platform platform, EventBus eventBus) {
055 super(platform, eventBus);
056 }
057
058 @PostConstruct
059 public void init() {
060 setInstanceStoreFactory(new CDIInstanceStoreFactory(beanManager));
061 getContext().getEntityManager().addListener(new CDIDataConflictListener());
062 }
063
064 @Produces
065 public Context getContext() {
066 return getContext(null);
067 }
068
069 @Produces
070 public EntityManager getEntityManager() {
071 return getContext(null).getEntityManager();
072 }
073
074 @Produces
075 public DataManager getDataManager() {
076 return getContext(null).getDataManager();
077 }
078
079 private final class CDIDataConflictListener implements DataConflictListener {
080 @Override
081 public void onConflict(EntityManager entityManager, Conflicts conflicts) {
082 TideApplicationEvent event = new TideApplicationEvent(getContext(null), UpdateKind.CONFLICT.eventName(), conflicts);
083 beanManager.fireEvent(event);
084 }
085 }
086 }