View Javadoc
1   /*
2    * This file is part of ***  M y C o R e  ***
3    * See http://www.mycore.de/ for details.
4    *
5    * MyCoRe is free software: you can redistribute it and/or modify
6    * it under the terms of the GNU General Public License as published by
7    * the Free Software Foundation, either version 3 of the License, or
8    * (at your option) any later version.
9    *
10   * MyCoRe is distributed in the hope that it will be useful,
11   * but WITHOUT ANY WARRANTY; without even the implied warranty of
12   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13   * GNU General Public License for more details.
14   *
15   * You should have received a copy of the GNU General Public License
16   * along with MyCoRe.  If not, see <http://www.gnu.org/licenses/>.
17   */
18  
19  package org.mycore.frontend.cli;
20  
21  import org.apache.logging.log4j.LogManager;
22  import org.apache.logging.log4j.Logger;
23  import org.jdom2.Element;
24  import org.jdom2.output.Format;
25  import org.jdom2.output.XMLOutputter;
26  import org.mycore.common.config.MCRConfiguration2;
27  import org.mycore.common.xml.MCRURIResolver;
28  import org.mycore.datamodel.metadata.MCRDerivate;
29  import org.mycore.datamodel.metadata.MCRMetadataManager;
30  import org.mycore.datamodel.metadata.MCRObject;
31  import org.mycore.datamodel.metadata.MCRObjectID;
32  import org.mycore.frontend.cli.annotation.MCRCommand;
33  import org.mycore.frontend.cli.annotation.MCRCommandGroup;
34  import org.mycore.services.i18n.MCRTranslation;
35  
36  import java.util.Map;
37  
38  /**
39   * This class contains commands that may be helpful during development.
40   *
41   * @author Torsten Krause
42   */
43  
44  @MCRCommandGroup(name = "Developer Commands")
45  public class MCRDeveloperCommands {
46  
47      private static final Logger LOGGER = LogManager.getLogger(MCRDeveloperCommands.class);
48  
49      @MCRCommand(
50          syntax = "show message {0} for {1}",
51          help = "Show message with key {0} for locale {1}",
52          order = 10)
53      public static void showMessage(String key, String lang) {
54          String value = MCRTranslation.translate(key, MCRTranslation.getLocale(lang));
55          if (value == null || (value.startsWith("???") && value.endsWith("???"))) {
56              LOGGER.info("Found no message for key {}", key);
57          } else {
58              LOGGER.info("Found message for key {}: {}", key, value);
59          }
60      }
61  
62      @MCRCommand(
63          syntax = "show messages {0} for {1}",
64          help = "Show messages with key prefix {0} for locale {1}",
65          order = 20)
66      public static void showMessages(String keyPrefix, String lang) {
67          Map<String, String> values = MCRTranslation.translatePrefix(keyPrefix, MCRTranslation.getLocale(lang));
68          if (values.isEmpty()) {
69              LOGGER.info("Found no messages for key prefix {}", keyPrefix);
70          } else {
71              values.forEach((key, value) -> {
72                  LOGGER.info("Found message for key {}: {}", key, value);
73              });
74          }
75      }
76  
77      @MCRCommand(
78          syntax = "show all messages for {0}",
79          help = "Show all messages for locale {0}",
80          order = 30)
81      public static void showMessages(String lang) {
82          Map<String, String> values = MCRTranslation.translatePrefix("", MCRTranslation.getLocale(lang));
83          if (values.isEmpty()) {
84              LOGGER.info("Found no messages");
85          } else {
86              values.forEach((key, value) -> {
87                  LOGGER.info("Found message for key {}: {}", key, value);
88              });
89          }
90      }
91  
92      @MCRCommand(
93          syntax = "show property {0}",
94          help = "Show configuration property with key {0}",
95          order = 40)
96      public static void showProperty(String key) {
97          String value = MCRConfiguration2.getPropertiesMap().get(key);
98          if (value == null) {
99              LOGGER.info("Found no value for key {}", key);
100         } else {
101             LOGGER.info("Found value for key {}: {}", key, value);
102         }
103     }
104 
105     @MCRCommand(
106         syntax = "show properties {0}",
107         help = "Show configuration properties starting with key prefix {0}",
108         order = 50)
109     public static void showProperties(String keyPrefix) {
110         Map<String, String> values = MCRConfiguration2.getSubPropertiesMap(keyPrefix);
111         if (values.isEmpty()) {
112             LOGGER.info("Found no values for key prefix {}", keyPrefix);
113         } else {
114             values.forEach((key, value) -> {
115                 LOGGER.info("Found value for key {}: {}", keyPrefix + key, value);
116             });
117         }
118     }
119 
120     @MCRCommand(
121         syntax = "show all properties",
122         help = "Show all configuration properties",
123         order = 60)
124     public static void showAllProperties() {
125         Map<String, String> values = MCRConfiguration2.getPropertiesMap();
126         if (values.isEmpty()) {
127             LOGGER.info("Found no values");
128         } else {
129             values.forEach((key, value) -> {
130                 LOGGER.info("Found value for key {}: {}", key, value);
131             });
132         }
133     }
134 
135     @MCRCommand(
136         syntax = "show resource {0}",
137         help = "Show resource with uri {0}",
138         order = 70)
139     public static void showResource(String uri) {
140         try {
141             Element resource = MCRURIResolver.instance().resolve(uri);
142             String xmlText = new XMLOutputter(Format.getPrettyFormat()).outputString(resource);
143             LOGGER.info("Resolved resource for uri {}:\n{}", uri, xmlText);
144         } catch (Exception e) {
145             LOGGER.info("Failed to resolve resource for uri " + uri, e);
146         }
147     }
148 
149     @MCRCommand(
150         syntax = "touch object {0}",
151         help = "Load and update object with id {0} without making any modifications",
152         order = 80)
153     public static void touchObject(String id) {
154         try {
155             MCRObjectID objectId = MCRObjectID.getInstance(id);
156             MCRObject object = MCRMetadataManager.retrieveMCRObject(objectId);
157             MCRMetadataManager.update(object);
158             LOGGER.info("Touched object with id {}", id);
159         } catch (Exception e) {
160             LOGGER.info("Failed to touch object with id " + id, e);
161         }
162     }
163 
164     @MCRCommand(
165         syntax = "touch derivate {0}",
166         help = "Load and update derivate with id {0} without making any modifications",
167         order = 90)
168     public static void touchDerivate(String id) {
169         try {
170             MCRObjectID derivateId = MCRObjectID.getInstance(id);
171             MCRDerivate derivate = MCRMetadataManager.retrieveMCRDerivate(derivateId);
172             MCRMetadataManager.update(derivate);
173             LOGGER.info("Touched derivate with id {}", id);
174         } catch (Exception e) {
175             LOGGER.info("Failed to touch derivate with id " + id, e);
176         }
177     }
178 
179 }