View Javadoc
1   package fr.ifremer.quadrige3.synchro.vo;
2   
3   /*-
4    * #%L
5    * Quadrige3 Core :: Quadrige3 Synchro Core
6    * $Id:$
7    * $HeadURL:$
8    * %%
9    * Copyright (C) 2017 Ifremer
10   * %%
11   * This program is free software: you can redistribute it and/or modify
12   * it under the terms of the GNU Affero General Public License as published by
13   * the Free Software Foundation, either version 3 of the License, or
14   * (at your option) any later version.
15   * 
16   * This program is distributed in the hope that it will be useful,
17   * but WITHOUT ANY WARRANTY; without even the implied warranty of
18   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19   * GNU General Public License for more details.
20   * 
21   * You should have received a copy of the GNU Affero General Public License
22   * along with this program.  If not, see <http://www.gnu.org/licenses/>.
23   * #L%
24   */
25  
26  import com.google.common.collect.HashMultimap;
27  import com.google.common.collect.ImmutableList;
28  import com.google.common.collect.Maps;
29  import com.google.common.collect.Multimap;
30  import org.apache.commons.collections4.CollectionUtils;
31  
32  import java.io.Serializable;
33  import java.util.List;
34  import java.util.Map;
35  
36  /**
37   * Table relation class
38   * <p>
39   * Created by Ludovic on 29/11/2016.
40   */
41  public class SynchroTableRelationsVO implements Serializable {
42  
43  	private final Map<String, SynchroTableMetaVO> tableMap;
44  
45  	private final Multimap<SynchroTableMetaVO, SynchroTableMetaVO> childTableMap;
46  
47  	public SynchroTableRelationsVO() {
48  		tableMap = Maps.newTreeMap();
49  		childTableMap = HashMultimap.create();
50  	}
51  
52  	public Map<String, SynchroTableMetaVO> getTableMap() {
53  		return tableMap;
54  	}
55  
56  	public List<String> getTableNames() {
57  		return ImmutableList.copyOf(tableMap.keySet());
58  	}
59  
60  	public List<SynchroTableMetaVO> getTables() {
61  		return ImmutableList.copyOf(tableMap.values());
62  	}
63  
64  	public Multimap<SynchroTableMetaVO, SynchroTableMetaVO> getChildTableMap() {
65  		return childTableMap;
66  	}
67  
68  	public void addChildTable(SynchroTableMetaVO table, SynchroTableMetaVO childTable) {
69  		// add child relation
70  		childTableMap.put(table, childTable);
71  	}
72  
73  	public SynchroTableMetaVO getOrCreateTable(String tableName, boolean tableIsRoot, boolean tableIsUpdatable, boolean tableIsData) {
74  		// get existing table or create it if not
75  		SynchroTableMetaVO table = tableMap.get(tableName);
76  		if (table == null) {
77  			table = new SynchroTableMetaVO(tableName, tableIsRoot, tableIsUpdatable, tableIsData);
78  			tableMap.put(tableName, table);
79  		}
80  		return table;
81  	}
82  
83  	public boolean exists(String tableName, boolean isData) {
84  		SynchroTableMetaVO table = tableMap.get(tableName);
85  		return table != null && table.isData() == isData;
86  	}
87  
88  	public List<SynchroTableMetaVO> getChildTables(SynchroTableMetaVO table) {
89  		return ImmutableList.copyOf(childTableMap.get(table));
90  	}
91  
92  	public boolean hasChild(SynchroTableMetaVO table) {
93  		return CollectionUtils.isNotEmpty(childTableMap.get(table));
94  	}
95  
96  }