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