Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 082fef1449d0775a73af15ba278a74b4a1d02cb6 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
/*
 * Copyright (c) 2009-2012, 2015 Eike Stepper (Loehne, Germany) and others.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *    Eike Stepper - initial API and implementation
 *
 *  Initial Publication:
 *    Eclipse Magazin - http://www.eclipse-magazin.de
 */
package org.gastro.internal.rcp;

import org.eclipse.emf.cdo.CDOObject;
import org.eclipse.emf.cdo.eresource.CDOResource;
import org.eclipse.emf.cdo.net4j.CDONet4jSession;
import org.eclipse.emf.cdo.net4j.CDONet4jSessionConfiguration;
import org.eclipse.emf.cdo.net4j.CDONet4jUtil;
import org.eclipse.emf.cdo.transaction.CDOTransaction;
import org.eclipse.emf.cdo.util.CommitException;
import org.eclipse.emf.cdo.view.CDOAdapterPolicy;
import org.eclipse.emf.cdo.view.CDOView;

import org.eclipse.net4j.Net4jUtil;
import org.eclipse.net4j.connector.IConnector;
import org.eclipse.net4j.util.WrappedException;
import org.eclipse.net4j.util.container.IPluginContainer;
import org.eclipse.net4j.util.lifecycle.Lifecycle;

import org.eclipse.emf.common.notify.AdapterFactory;
import org.eclipse.emf.common.util.EList;
import org.eclipse.emf.edit.EMFEditPlugin;
import org.eclipse.emf.edit.provider.ComposedAdapterFactory;

import org.gastro.business.BusinessDay;
import org.gastro.business.BusinessFactory;
import org.gastro.inventory.InventoryFactory;
import org.gastro.inventory.MenuCard;
import org.gastro.inventory.Restaurant;
import org.gastro.inventory.Station;
import org.gastro.rcp.IConfiguration;
import org.gastro.rcp.IModel;

import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * @author Eike Stepper
 */
public class Model extends Lifecycle implements IModel
{
  public static final Model INSTANCE = new Model();

  private final ComposedAdapterFactory adapterFactory;

  private CDONet4jSession session;

  private CDOView view;

  private Restaurant restaurant;

  private BusinessDay businessDay;

  private Station station;

  private Model()
  {
    adapterFactory = new ComposedAdapterFactory(EMFEditPlugin.getComposedAdapterFactoryDescriptorRegistry());
  }

  public AdapterFactory getAdapterFactory()
  {
    return adapterFactory;
  }

  public synchronized Restaurant getRestaurant()
  {
    if (restaurant == null)
    {
      String name = IConfiguration.INSTANCE.getRestaurant();
      String path = name + "/inventory";
      if (!view.hasResource(path))
      {
        CDOTransaction transaction = session.openTransaction();
        Restaurant restaurant = InventoryFactory.eINSTANCE.createRestaurant();
        restaurant.setName(name);

        try
        {
          CDOResource resource = transaction.createResource(path);
          resource.getContents().add(restaurant);
          transaction.commit();
        }
        catch (CommitException ex)
        {
          throw WrappedException.wrap(ex);
        }
        finally
        {
          transaction.close();
        }
      }

      CDOResource resource = view.getResource(path);
      restaurant = (Restaurant)resource.getContents().get(0);
    }

    return restaurant;
  }

  public synchronized BusinessDay getBusinessDay()
  {
    if (businessDay == null)
    {
      Restaurant restaurant = getRestaurant();
      Date date = IConfiguration.INSTANCE.getBusinessDay();
      SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
      String path = restaurant.getName() + "/" + formatter.format(date);
      if (!view.hasResource(path))
      {
        CDOTransaction transaction = session.openTransaction();
        Restaurant txRestaurant = transaction.getObject(restaurant);
        EList<MenuCard> menuCards = txRestaurant.getMenuCards();
        if (menuCards.isEmpty())
        {
          MenuCard menuCard = InventoryFactory.eINSTANCE.createMenuCard();
          menuCard.setTitle("Untitled");
          menuCards.add(menuCard);
        }

        BusinessDay businessDay = BusinessFactory.eINSTANCE.createBusinessDay();
        businessDay.setDate(date);
        businessDay.setMenuCard(menuCards.get(0));

        try
        {
          CDOResource resource = transaction.createResource(path);
          resource.getContents().add(businessDay);
          transaction.commit();
        }
        catch (CommitException ex)
        {
          throw WrappedException.wrap(ex);
        }
        finally
        {
          transaction.close();
        }
      }

      CDOResource resource = view.getResource(path);
      businessDay = (BusinessDay)resource.getContents().get(0);
    }

    return businessDay;
  }

  public synchronized Station getStation()
  {
    if (station == null)
    {
      String id = IConfiguration.INSTANCE.getStation();
      for (Station station : getRestaurant().getStations())
      {
        if (station.getStationID().equalsIgnoreCase(id))
        {
          this.station = station;
          break;
        }
      }
    }

    return station;
  }

  public <T extends CDOObject> Object modify(T object, ITransactionalOperation<T> operation)
  {
    CDOTransaction transaction = session.openTransaction();

    try
    {
      T transactionalObject = transaction.getObject(object);
      Object result = operation.execute(transactionalObject);
      transaction.commit();

      if (result instanceof CDOObject)
      {
        return view.getObject((CDOObject)result);
      }

      return result;
    }
    catch (CommitException ex)
    {
      throw WrappedException.wrap(ex);
    }
    finally
    {
      transaction.close();
    }
  }

  @Override
  protected void doActivate() throws Exception
  {
    super.doActivate();
    String server = IConfiguration.INSTANCE.getServer();
    String repository = IConfiguration.INSTANCE.getRepository();

    IConnector connector = Net4jUtil.getConnector(IPluginContainer.INSTANCE, "tcp", server);

    CDONet4jSessionConfiguration config = CDONet4jUtil.createNet4jSessionConfiguration();
    config.setConnector(connector);
    config.setRepositoryName(repository);

    session = config.openNet4jSession();
    view = session.openView();
    view.options().addChangeSubscriptionPolicy(CDOAdapterPolicy.ALL);
  }

  @Override
  protected void doDeactivate() throws Exception
  {
    session.close();
    session = null;
    view = null;
    restaurant = null;
    station = null;
    adapterFactory.dispose();
    super.doDeactivate();
  }
}

Back to the top