001/*
002 *  Licensed to the Apache Software Foundation (ASF) under one
003 *  or more contributor license agreements.  See the NOTICE file
004 *  distributed with this work for additional information
005 *  regarding copyright ownership.  The ASF licenses this file
006 *  to you under the Apache License, Version 2.0 (the
007 *  "License"); you may not use this file except in compliance
008 *  with the License.  You may obtain a copy of the License at
009 *
010 *        http://www.apache.org/licenses/LICENSE-2.0
011 *
012 *  Unless required by applicable law or agreed to in writing,
013 *  software distributed under the License is distributed on an
014 *  "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
015 *  KIND, either express or implied.  See the License for the
016 *  specific language governing permissions and limitations
017 *  under the License.
018 */
019package org.apache.isis.extensions.hsqldbmgr.dom.services;
020
021import javax.inject.Inject;
022
023import org.hsqldb.util.DatabaseManagerSwing;
024
025import org.apache.isis.applib.IsisModuleApplib;
026import org.apache.isis.applib.annotation.Action;
027import org.apache.isis.applib.annotation.ActionLayout;
028import org.apache.isis.applib.annotation.DomainService;
029import org.apache.isis.applib.annotation.DomainServiceLayout;
030import org.apache.isis.applib.annotation.MemberOrder;
031import org.apache.isis.applib.annotation.NatureOfService;
032import org.apache.isis.applib.annotation.RestrictTo;
033import org.apache.isis.applib.annotation.SemanticsOf;
034import org.apache.isis.commons.internal.base._Strings;
035import org.apache.isis.commons.internal.context._Context;
036import org.apache.isis.core.config.IsisConfiguration;
037
038@DomainService(
039        nature = NatureOfService.VIEW,
040        objectType = "isisExtHsqldbMgr.HsqlDbManagerMenu"
041        )
042@DomainServiceLayout(
043        named = "Prototyping",
044        menuBar = DomainServiceLayout.MenuBar.SECONDARY
045        )
046public class HsqlDbManagerMenu {
047
048    private final String url;
049
050    @Inject
051    public HsqlDbManagerMenu(IsisConfiguration isisConfiguration) {
052        this.url = isisConfiguration
053                .getPersistence().getJdoDatanucleus().getImpl().getJavax().getJdo().getOption().getConnectionUrl();
054    }
055
056    public static class ActionDomainEvent extends IsisModuleApplib.ActionDomainEvent<HsqlDbManagerMenu> { }
057
058    @Action(
059            semantics = SemanticsOf.SAFE,
060            restrictTo = RestrictTo.PROTOTYPING,
061            domainEvent = ActionDomainEvent.class
062            )
063    @ActionLayout(
064            named = "HSQL DB Manager",
065            cssClassFa = "database"
066            )
067    @MemberOrder(sequence = "500.800")
068    public void hsqlDbManager() {
069        String[] args = {"--url", url, "--noexit" };
070        DatabaseManagerSwing.main(args);
071    }
072    public boolean hideHsqlDbManager() {
073        try {
074            // hsqldb is configured as optional in the applib's pom.xml
075            _Context.loadClass("org.hsqldb.util.DatabaseManagerSwing");
076        } catch (ClassNotFoundException | NoClassDefFoundError e) {
077            return true;
078        }
079        return _Strings.isNullOrEmpty(url) || !url.contains("hsqldb:mem");
080    }
081
082}