com.github.bogieclj.molecule.mods.ishell.ChangeDomainFunction Maven / Gradle / Ivy
/*
* Copyright 2019 Vijayakumar Mohan
*
* Licensed 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 com.github.bogieclj.molecule.mods.ishell;
import com.github.bogieclj.molecule.ishell.annotations.DomainStack;
import com.github.bogieclj.molecule.system.Param;
import com.github.bogieclj.molecule.system.annotations.Id;
import com.github.bogieclj.molecule.system.services.DomainService;
import javax.inject.Inject;
import java.util.List;
import java.util.Stack;
import java.util.function.Function;
import static com.github.bogieclj.molecule.commons.Constants.IN_PARAMS;
@Id("function://system/ishell/jline/changeDomainFun")
class ChangeDomainFunction implements Function {
private Stack domainStack;
private DomainService domainService;
@Inject
public ChangeDomainFunction(DomainService domainService, @DomainStack Stack domainStack) {
this.domainStack = domainStack;
this.domainService = domainService;
}
@Override
public Param apply(Param param) {
if (param.containsKey(IN_PARAMS)) {
List args = (List) param.get(IN_PARAMS);
if (args == null || args.isEmpty()) {
System.out.println("Please specify the destination domain (or '/' for root)");
}
String destinationDomain = args.get(0);
if(destinationDomain.equals("/")){
domainStack.clear();
domainStack.push(JLineInteractiveShell.ROOT_DOMAIN);
}else if(destinationDomain.equals("..")){
if(domainStack.size()>1){
domainStack.pop(); //navigate to previous domain till root domain, beyond which it is invalid
}
}else{
String fullyQualifiedDomain = JLineInteractiveShell.getFullyQualifiedDomain(domainStack);
String fullyQualifiedDomainPath = JLineInteractiveShell.getPrompt(domainStack,"");
if (domainService.isValidDomainAt(fullyQualifiedDomain, destinationDomain)) {
domainStack.push(destinationDomain);
fullyQualifiedDomain = JLineInteractiveShell.getFullyQualifiedDomain(domainStack);
} else {
System.out.println(String.format("Domain %s is not a valid domain under %s", destinationDomain, fullyQualifiedDomainPath));
}
}
} else {
System.out.println("Please specify the destination domain (or '/' for root)");
}
return param;
}
}