Skip to content

Package: PersistTableStateServiceImpl$1

PersistTableStateServiceImpl$1

nameinstructionbranchcomplexitylinemethod
contextDisposed(ViewModelContext)
M: 9 C: 0
0%
M: 2 C: 0
0%
M: 2 C: 0
0%
M: 4 C: 0
0%
M: 1 C: 0
0%
{...}
M: 9 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 1 C: 0
0%
M: 1 C: 0
0%

Coverage

1: /*******************************************************************************
2: * Copyright (c) 2011-2016 EclipseSource Muenchen GmbH and others.
3: *
4: * All rights reserved. This program and the accompanying materials
5: * are made available under the terms of the Eclipse Public License 2.0
6: * which accompanies this distribution, and is available at
7: * https://www.eclipse.org/legal/epl-2.0/
8: *
9: * SPDX-License-Identifier: EPL-2.0
10: *
11: * Contributors:
12: * Johannes Faltermeier - initial API and implementation
13: ******************************************************************************/
14: package org.eclipse.emf.ecp.internal.view.table.ui.swt.persistedstate;
15:
16: import java.io.IOException;
17: import java.util.Collections;
18: import java.util.LinkedHashMap;
19: import java.util.LinkedHashSet;
20: import java.util.Map;
21: import java.util.Map.Entry;
22: import java.util.Set;
23:
24: import org.eclipse.emf.common.util.BasicEList;
25: import org.eclipse.emf.common.util.EList;
26: import org.eclipse.emf.common.util.TreeIterator;
27: import org.eclipse.emf.common.util.URI;
28: import org.eclipse.emf.ecore.EObject;
29: import org.eclipse.emf.ecore.resource.Resource;
30: import org.eclipse.emf.ecore.resource.ResourceSet;
31: import org.eclipse.emf.ecore.resource.impl.ResourceSetImpl;
32: import org.eclipse.emf.ecore.util.EcoreUtil;
33: import org.eclipse.emf.ecore.xmi.XMLResource;
34: import org.eclipse.emf.ecp.spi.view.table.ui.swt.persistedstate.PersistTableStateService;
35: import org.eclipse.emf.ecp.view.spi.context.ViewModelContext;
36: import org.eclipse.emf.ecp.view.spi.context.ViewModelContextDisposeListener;
37: import org.eclipse.emf.ecp.view.spi.model.VAttachment;
38: import org.eclipse.emf.ecp.view.spi.model.VControl;
39: import org.eclipse.emf.ecp.view.spi.model.VDomainModelReference;
40: import org.eclipse.emf.ecp.view.spi.model.VDomainModelReferenceSegment;
41: import org.eclipse.emf.ecp.view.spi.model.VElement;
42: import org.eclipse.emf.ecp.view.spi.model.util.VViewResourceFactoryImpl;
43: import org.eclipse.emf.ecp.view.spi.model.util.VViewResourceImpl;
44: import org.eclipse.emf.ecp.view.spi.table.model.VTableColumnConfiguration;
45: import org.eclipse.emf.ecp.view.spi.table.model.VTableControl;
46: import org.eclipse.emf.ecp.view.spi.table.model.VTableDomainModelReference;
47: import org.eclipse.emf.ecp.view.spi.table.model.VTableFactory;
48: import org.eclipse.emf.ecp.view.spi.table.model.VWidthConfiguration;
49: import org.eclipse.emfforms.common.Optional;
50: import org.eclipse.emfforms.spi.common.report.AbstractReport;
51: import org.eclipse.emfforms.spi.common.report.ReportService;
52: import org.eclipse.emfforms.view.spi.multisegment.model.VMultiDomainModelReferenceSegment;
53:
54: /**
55: * Implementation of the {@link PersistTableStateService}.
56: *
57: * @author Johannes Faltermeier
58: *
59: */
60: public class PersistTableStateServiceImpl implements PersistTableStateService {
61:
62:         private String viewID;
63:         private Set<VTableControl> tables;
64:         private LinkedHashSet<VControl> additionalControls;
65:         private URI uri;
66:         private VViewResourceImpl resource;
67:         private ReportService reportService;
68:
69:         /**
70:          * Default constructor.
71:          *
72:          * @param context the view model context
73:          */
74:         PersistTableStateServiceImpl(final ViewModelContext context) {
75:                 reportService = context.getService(ReportService.class);
76:                 context.registerDisposeListener(new ViewModelContextDisposeListener() {
77:
78:                         @Override
79:                         public void contextDisposed(ViewModelContext viewModelContext) {
80:•                                if (context != viewModelContext) {
81:                                         return;
82:                                 }
83:                                 persist();
84:                         }
85:                 });
86:
87:                 final VElement viewModel = context.getViewModel();
88:                 viewID = viewModel.getUuid();
89:                 // TODO proper fix should be implemented with Bug 495113.
90:                 if (viewID == null) {
91:                         return;
92:                 }
93:
94:                 tables = findTables(viewModel);
95:                 additionalControls = new LinkedHashSet<VControl>();
96:
97:                 final String stateLocation = Activator.getInstance().getStateLocation().toFile().getAbsolutePath();
98:                 uri = URI.createFileURI(stateLocation).appendSegment(viewID).appendFileExtension("xmi"); //$NON-NLS-1$
99:                 final ResourceSet resourceSet = createResourceSet();
100:                 if (!resourceSet.getURIConverter().exists(uri, null)) {
101:                         return;
102:                 }
103:                 resource = (VViewResourceImpl) resourceSet.getResource(uri, true);
104:                 for (final VTableControl realTable : tables) {
105:                         final Map<VDomainModelReference, VWidthConfiguration> realColumnDMRToConfig = getDMRToConfig(realTable);
106:                         final EObject persistedTable = resource.getIDToEObjectMap().get(realTable.getUuid());
107:                         if (!VTableControl.class.isInstance(persistedTable)) {
108:                                 continue;
109:                         }
110:
111:                         final VTableDomainModelReference realTableDMR = VTableDomainModelReference.class
112:                                 .cast(realTable.getDomainModelReference());
113:
114:                         final Map<VDomainModelReference, VWidthConfiguration> persistedDMRIDToConfig = getDMRToConfig(
115:                                 VTableControl.class.cast(persistedTable));
116:                         for (final Entry<VDomainModelReference, VWidthConfiguration> entry : persistedDMRIDToConfig.entrySet()) {
117:                                 /* find matching real column dmr */
118:                                 VDomainModelReference realMatchingDMR = null;
119:                                 for (final VDomainModelReference realDMR : getColumnDomainModelReferences(realTableDMR)) {
120:                                         if (EcoreUtil.equals(entry.getKey(), realDMR)) {
121:                                                 realMatchingDMR = realDMR;
122:                                                 break;
123:                                         }
124:                                 }
125:                                 if (realMatchingDMR == null) {
126:                                         continue;
127:                                 }
128:                                 /* get real width config for real dmr */
129:                                 if (realColumnDMRToConfig.containsKey(realMatchingDMR)) {
130:                                         fillWidthConfig(realColumnDMRToConfig.get(realMatchingDMR), entry.getValue());
131:                                 } else {
132:                                         final VWidthConfiguration widthConfiguration = VTableFactory.eINSTANCE.createWidthConfiguration();
133:                                         fillWidthConfig(widthConfiguration, entry.getValue());
134:                                         widthConfiguration.setColumnDomainReference(realMatchingDMR);
135:                                         realTable.getColumnConfigurations().add(widthConfiguration);
136:                                 }
137:                         }
138:                 }
139:
140:         }
141:
142:         private static EList<VDomainModelReference> getColumnDomainModelReferences(VTableDomainModelReference tableDmr) {
143:                 if (tableDmr.getSegments().size() > 0) {
144:                         final VDomainModelReferenceSegment lastSegment = tableDmr.getSegments()
145:                                 .get(tableDmr.getSegments().size() - 1);
146:                         if (!VMultiDomainModelReferenceSegment.class.isInstance(lastSegment)) {
147:                                 return new BasicEList<VDomainModelReference>();
148:                         }
149:                         return VMultiDomainModelReferenceSegment.class.cast(lastSegment)
150:                                 .getChildDomainModelReferences();
151:                 }
152:                 return tableDmr.getColumnDomainModelReferences();
153:         }
154:
155:         private static void fillWidthConfig(VWidthConfiguration toFill, VWidthConfiguration lookup) {
156:                 toFill.setWeight(lookup.getWeight());
157:                 toFill.setMinWidth(lookup.getMinWidth());
158:         }
159:
160:         private static Map<VDomainModelReference, VWidthConfiguration> getDMRToConfig(VTableControl table) {
161:                 final Map<VDomainModelReference, VWidthConfiguration> result = new LinkedHashMap<VDomainModelReference, VWidthConfiguration>();
162:                 for (final VTableColumnConfiguration configuration : table.getColumnConfigurations()) {
163:                         if (!VWidthConfiguration.class.isInstance(configuration)) {
164:                                 continue;
165:                         }
166:                         final VWidthConfiguration widthConfiguration = VWidthConfiguration.class.cast(configuration);
167:                         result.put(widthConfiguration.getColumnDomainReference(), widthConfiguration);
168:                 }
169:                 return result;
170:         }
171:
172:         private static ResourceSet createResourceSet() {
173:                 final ResourceSet resourceSet = new ResourceSetImpl();
174:                 final Map<String, Object> extensionToFactoryMap = resourceSet.getResourceFactoryRegistry()
175:                         .getExtensionToFactoryMap();
176:                 extensionToFactoryMap.put(Resource.Factory.Registry.DEFAULT_EXTENSION, new VViewResourceFactoryImpl());
177:                 return resourceSet;
178:         }
179:
180:         private static Set<VTableControl> findTables(final VElement viewModel) {
181:                 final Set<VTableControl> tables = new LinkedHashSet<VTableControl>();
182:                 final TreeIterator<EObject> viewModelContents = viewModel.eAllContents();
183:                 while (viewModelContents.hasNext()) {
184:                         final EObject next = viewModelContents.next();
185:                         if (!VTableControl.class.isInstance(next)) {
186:                                 continue;
187:                         }
188:                         tables.add((VTableControl) next);
189:                 }
190:                 return tables;
191:         }
192:
193:         private void persist() {
194:                 final ResourceSet resourceSet = createResourceSet();
195:                 final VViewResourceImpl resource = (VViewResourceImpl) resourceSet.createResource(uri);
196:                 for (final VTableControl tableControl : tables) {
197:                         final String uuid = tableControl.getUuid();
198:                         final VTableControl tableToPersist = EcoreUtil.copy(tableControl);
199:                         resource.getContents().add(tableToPersist);
200:                         resource.setID(tableToPersist, uuid);
201:                 }
202:                 for (final VControl control : additionalControls) {
203:                         for (final VAttachment attachment : control.getAttachments()) {
204:                                 if (!PersistTableStateServiceVAttachment.class.isInstance(attachment)) {
205:                                         continue;
206:                                 }
207:                                 final String uuid = control.getUuid();
208:                                 final VAttachment attachmentToPersist = EcoreUtil.copy(attachment);
209:                                 resource.getContents().add(attachmentToPersist);
210:                                 resource.setID(attachmentToPersist, uuid);
211:                                 break;
212:                         }
213:                 }
214:                 try {
215:                         resource.save(Collections.singletonMap(XMLResource.OPTION_ENCODING, "UTF-8")); //$NON-NLS-1$
216:                 } catch (final IOException ex) {
217:                         reportService.report(new AbstractReport(ex));
218:                 }
219:         }
220:
221:         @Override
222:         public void registerAdditionalControls(VControl... controls) {
223:                 for (final VControl control : controls) {
224:                         if (tables.contains(control)) {
225:                                 continue;
226:                         }
227:                         additionalControls.add(control);
228:                 }
229:         }
230:
231:         @Override
232:         public Optional<VAttachment> getPersistedState(VControl control) {
233:                 final String uuid = control.getUuid();
234:                 final EObject eObject = resource.getIDToEObjectMap().get(uuid);
235:                 if (!VAttachment.class.isInstance(eObject)) {
236:                         return Optional.empty();
237:                 }
238:                 return Optional.of(VAttachment.class.cast(eObject));
239:         }
240:
241: }