1 /*******************************************************************************
2 * Copyright (c) 2007, 2010 Association for Decentralized Information Management
4 * All rights reserved. This program and the accompanying materials
5 * are made available under the terms of the Eclipse Public License v1.0
6 * which accompanies this distribution, and is available at
7 * http://www.eclipse.org/legal/epl-v10.html
10 * VTT Technical Research Centre of Finland - initial API and implementation
11 *******************************************************************************/
12 package org.simantics.spreadsheet.graph;
14 import java.util.ArrayList;
15 import java.util.Collection;
16 import java.util.Collections;
17 import java.util.HashSet;
20 import org.simantics.databoard.Bindings;
21 import org.simantics.databoard.binding.mutable.Variant;
22 import org.simantics.db.ReadGraph;
23 import org.simantics.db.Resource;
24 import org.simantics.db.common.request.UnaryRead;
25 import org.simantics.db.common.utils.Logger;
26 import org.simantics.db.exception.DatabaseException;
27 import org.simantics.db.layer0.variable.ConstantChildVariable;
28 import org.simantics.db.layer0.variable.ConstantPropertyVariableBuilder;
29 import org.simantics.db.layer0.variable.Variable;
30 import org.simantics.db.layer0.variable.Variables;
31 import org.simantics.spreadsheet.ClientModel;
32 import org.simantics.spreadsheet.resource.SpreadsheetResource;
33 import org.simantics.utils.datastructures.Pair;
35 public class Cells extends UnaryRead<Variable, Collection<Pair<String, Variable>>> {
37 public Cells(Variable variable) {
41 private static Set<String> CLASSIFICATIONS = new HashSet<String>();
44 CLASSIFICATIONS.add(SpreadsheetResource.URIs.Attribute);
48 public Collection<Pair<String, Variable>> perform(ReadGraph graph) throws DatabaseException {
49 SpreadsheetResource SHEET = SpreadsheetResource.getInstance(graph);
50 ArrayList<Pair<String, Variable>> result = new ArrayList<Pair<String, Variable>>();
53 System.out.println("Executing Cells for parameter " + parameter.getURI(graph));
55 for(Variable child : parameter.getChildren(graph)) {
56 Resource type = child.getPossibleType(graph);
58 if(graph.isInheritedFrom(type, SHEET.Range)) {
62 Collection<Pair<String, Variable>> rangeCells = graph.sync(new Cells(child));
63 result.addAll(rangeCells);
65 } catch (DatabaseException e) {
67 String location = child.getPossiblePropertyValue(graph, SHEET.Range_location, Bindings.STRING);
68 if(location == null) location = "A1";
70 result.add(error(graph, child, location, e));
72 Logger.defaultLogError(e);
74 } else if(graph.isInheritedFrom(type, SHEET.Cell)) {
78 String name = child.getName(graph);
79 result.add(Pair.make(name, child));
81 } catch (DatabaseException e) {
84 Logger.defaultLogError(e);
90 for (Variable child : parameter.getParent(graph).getChildren(graph)) {
92 Resource type = child.getPossibleType(graph);
94 if (type != null && type.equals(SHEET.Style)) {
95 String name = child.getName(graph);
96 result.add(Pair.make(name, child));
102 private static Pair<String, Variable> error(ReadGraph graph, Variable child, String location, DatabaseException e) throws DatabaseException {
104 SpreadsheetResource SHEET = SpreadsheetResource.getInstance(graph);
106 ArrayList<ConstantPropertyVariableBuilder> builders = new ArrayList<ConstantPropertyVariableBuilder>();
108 builders.add(new ConstantPropertyVariableBuilder(ClientModel.CONTENT, Variant.ofInstance(e.getExplanation(graph)), Bindings.VARIANT, Collections.<ConstantPropertyVariableBuilder>emptyList(), CLASSIFICATIONS));
109 builders.add(new ConstantPropertyVariableBuilder(Variables.TYPE, SHEET.Cell, null, Collections.<ConstantPropertyVariableBuilder>emptyList(), Collections.<String>emptySet()));
111 Variable var = new ConstantChildVariable(child, location, builders);
112 return new Pair<String, Variable>(location, var);