blob: f91c26af0b46a635b5c6f17c92644a6c9034e99b [file] [log] [blame]
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with this
* work for additional information regarding copyright ownership. The ASF
* licenses this file to You under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
* License for the specific language governing permissions and limitations
* under the License.
*/
package org.apache.hugegraph.auth;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.apache.hugegraph.backend.id.Id;
import org.apache.hugegraph.schema.EdgeLabel;
import org.apache.tinkerpop.gremlin.structure.Edge;
import org.apache.tinkerpop.gremlin.structure.Graph.Hidden;
import org.apache.tinkerpop.gremlin.structure.T;
import org.apache.hugegraph.HugeGraphParams;
import org.apache.hugegraph.auth.SchemaDefine.Relationship;
public class HugeBelong extends Relationship {
private static final long serialVersionUID = -7242751631755533423L;
private final Id user;
private final Id group;
private String description;
public HugeBelong(Id user, Id group) {
this.user = user;
this.group = group;
this.description = null;
}
@Override
public ResourceType type() {
return ResourceType.GRANT;
}
@Override
public String label() {
return P.BELONG;
}
@Override
public String sourceLabel() {
return P.USER;
}
@Override
public String targetLabel() {
return P.GROUP;
}
@Override
public Id source() {
return this.user;
}
@Override
public Id target() {
return this.group;
}
public String description() {
return this.description;
}
public void description(String description) {
this.description = description;
}
@Override
public String toString() {
return String.format("HugeBelong(%s->%s)%s",
this.user, this.group, this.asMap());
}
@Override
protected boolean property(String key, Object value) {
if (super.property(key, value)) {
return true;
}
switch (key) {
case P.DESCRIPTION:
this.description = (String) value;
break;
default:
throw new AssertionError("Unsupported key: " + key);
}
return true;
}
@Override
protected Object[] asArray() {
List<Object> list = new ArrayList<>(10);
list.add(T.label);
list.add(P.BELONG);
if (this.description != null) {
list.add(P.DESCRIPTION);
list.add(this.description);
}
return super.asArray(list);
}
@Override
public Map<String, Object> asMap() {
Map<String, Object> map = new HashMap<>();
map.put(Hidden.unHide(P.USER), this.user);
map.put(Hidden.unHide(P.GROUP), this.group);
if (this.description != null) {
map.put(Hidden.unHide(P.DESCRIPTION), this.description);
}
return super.asMap(map);
}
public static HugeBelong fromEdge(Edge edge) {
HugeBelong belong = new HugeBelong((Id) edge.outVertex().id(),
(Id) edge.inVertex().id());
return fromEdge(edge, belong);
}
public static Schema schema(HugeGraphParams graph) {
return new Schema(graph);
}
public static final class P {
public static final String BELONG = Hidden.hide("belong");
public static final String LABEL = T.label.getAccessor();
public static final String USER = HugeUser.P.USER;
public static final String GROUP = HugeGroup.P.GROUP;
public static final String DESCRIPTION = "~belong_description";
public static String unhide(String key) {
final String prefix = Hidden.hide("belong_");
if (key.startsWith(prefix)) {
return key.substring(prefix.length());
}
return key;
}
}
public static final class Schema extends SchemaDefine {
public Schema(HugeGraphParams graph) {
super(graph, P.BELONG);
}
@Override
public void initSchemaIfNeeded() {
if (this.existEdgeLabel(this.label)) {
return;
}
String[] properties = this.initProperties();
// Create edge label
EdgeLabel label = this.schema().edgeLabel(this.label)
.sourceLabel(P.USER)
.targetLabel(P.GROUP)
.properties(properties)
.nullableKeys(P.DESCRIPTION)
.enableLabelIndex(true)
.build();
this.graph.schemaTransaction().addEdgeLabel(label);
}
private String[] initProperties() {
List<String> props = new ArrayList<>();
props.add(createPropertyKey(P.DESCRIPTION));
return super.initProperties(props);
}
}
}