Skip to content
Permalink
Browse files

Made default process and document type name handling customizable; #112

  • Loading branch information...
phax committed Sep 27, 2019
1 parent a291bb4 commit 71b344647dd57878460e24f682356696df24b68a
@@ -0,0 +1,72 @@
/**
* Copyright (C) 2014-2019 Philip Helger and contributors
* philip[at]helger[dot]com
*
* 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.helger.phoss.smp.nicename;

import java.io.Serializable;

import javax.annotation.Nonnull;

import com.helger.commons.annotation.Nonempty;
import com.helger.commons.hashcode.HashCodeGenerator;
import com.helger.commons.string.ToStringGenerator;

public final class NiceNameEntry implements Serializable
{
private final String m_sName;
private final boolean m_bDeprecated;

public NiceNameEntry (@Nonnull @Nonempty final String sName, final boolean bDeprecated)
{
m_sName = sName;
m_bDeprecated = bDeprecated;
}

@Nonnull
@Nonempty
public String getName ()
{
return m_sName;
}

public boolean isDeprecated ()
{
return m_bDeprecated;
}

@Override
public boolean equals (final Object o)
{
if (o == this)
return true;
if (o == null || !getClass ().equals (o.getClass ()))
return false;
final NiceNameEntry rhs = (NiceNameEntry) o;
return m_sName.equals (rhs.m_sName) && m_bDeprecated == rhs.m_bDeprecated;
}

@Override
public int hashCode ()
{
return new HashCodeGenerator (this).append (m_sName).append (m_bDeprecated).getHashCode ();
}

@Override
public String toString ()
{
return new ToStringGenerator (this).append ("Name", m_sName).append ("Deprecated", m_bDeprecated).getToString ();
}
}
@@ -0,0 +1,144 @@
/**
* Copyright (C) 2014-2019 Philip Helger and contributors
* philip[at]helger[dot]com
*
* 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.helger.phoss.smp.nicename;

import javax.annotation.Nonnull;
import javax.annotation.Nullable;
import javax.annotation.concurrent.GuardedBy;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.helger.commons.annotation.ReturnsMutableCopy;
import com.helger.commons.collection.impl.CommonsLinkedHashMap;
import com.helger.commons.collection.impl.ICommonsOrderedMap;
import com.helger.commons.concurrent.SimpleReadWriteLock;
import com.helger.commons.io.resource.ClassPathResource;
import com.helger.commons.io.resource.FileSystemResource;
import com.helger.commons.io.resource.IReadableResource;
import com.helger.commons.string.StringHelper;
import com.helger.phoss.smp.app.SMPWebAppConfiguration;
import com.helger.xml.microdom.IMicroDocument;
import com.helger.xml.microdom.IMicroElement;
import com.helger.xml.microdom.serialize.MicroReader;

public final class NiceNameHandler
{
private static final Logger LOGGER = LoggerFactory.getLogger (NiceNameHandler.class);
private static final SimpleReadWriteLock RWLOCK = new SimpleReadWriteLock ();
@GuardedBy ("RWLOCK")
private static ICommonsOrderedMap <String, NiceNameEntry> DOCTYPE_IDS = new CommonsLinkedHashMap <> ();
@GuardedBy ("RWLOCK")
private static ICommonsOrderedMap <String, NiceNameEntry> PROCESS_IDS = new CommonsLinkedHashMap <> ();

static
{
reloadNames ();
}

private NiceNameHandler ()
{}

@Nonnull
@ReturnsMutableCopy
public static ICommonsOrderedMap <String, NiceNameEntry> readEntries (@Nonnull final IReadableResource aRes)
{
if (LOGGER.isInfoEnabled ())
LOGGER.info ("Trying to read nice name entries from " + aRes.getPath ());

final ICommonsOrderedMap <String, NiceNameEntry> ret = new CommonsLinkedHashMap <> ();
final IMicroDocument aDoc = MicroReader.readMicroXML (aRes);
if (aDoc != null && aDoc.getDocumentElement () != null)
{
for (final IMicroElement eChild : aDoc.getDocumentElement ().getAllChildElements ("item"))
{
final String sID = eChild.getAttributeValue ("id");
final String sName = eChild.getAttributeValue ("name");
final boolean bDeprecated = eChild.getAttributeValueAsBool ("deprecated", false);
ret.put (sID, new NiceNameEntry (sName, bDeprecated));
}
}
return ret;
}

public static void reloadNames ()
{
// Doc types
{
IReadableResource aDocTypeIDRes = null;
final String sPath = SMPWebAppConfiguration.getConfigFile ().getAsString ("webapp.nicename.doctypes.path");
if (StringHelper.hasText (sPath))
{
aDocTypeIDRes = new FileSystemResource (sPath);
if (!aDocTypeIDRes.exists ())
LOGGER.warn ("The configured nice name file '" + sPath + "' does not exist");
}
if (aDocTypeIDRes == null)
aDocTypeIDRes = new ClassPathResource ("codelists/doctypeid-mapping.xml");
final ICommonsOrderedMap <String, NiceNameEntry> aDocTypeIDs = readEntries (aDocTypeIDRes);
RWLOCK.writeLocked ( () -> DOCTYPE_IDS = aDocTypeIDs);
}

// Processes
{
IReadableResource aProcessIDRes = null;
final String sPath = SMPWebAppConfiguration.getConfigFile ().getAsString ("webapp.nicename.processes.path");
if (StringHelper.hasText (sPath))
{
aProcessIDRes = new FileSystemResource (sPath);
if (!aProcessIDRes.exists ())
LOGGER.warn ("The configured nice name file '" + sPath + "' does not exist");
}
if (aProcessIDRes == null)
aProcessIDRes = new ClassPathResource ("codelists/processid-mapping.xml");
final ICommonsOrderedMap <String, NiceNameEntry> aProcessIDs = readEntries (aProcessIDRes);
RWLOCK.writeLocked ( () -> PROCESS_IDS = aProcessIDs);
}
}

@Nullable
public static NiceNameEntry getDocTypeNiceName (@Nullable final String sID)
{
if (StringHelper.hasNoText (sID))
return null;
RWLOCK.readLock ().lock ();
try
{
return DOCTYPE_IDS.get (sID);
}
finally
{
RWLOCK.readLock ().unlock ();
}
}

@Nullable
public static NiceNameEntry getProcessNiceName (@Nullable final String sID)
{
if (StringHelper.hasNoText (sID))
return null;
RWLOCK.readLock ().lock ();
try
{
return PROCESS_IDS.get (sID);
}
finally
{
RWLOCK.readLock ().unlock ();
}
}
}
@@ -0,0 +1,94 @@
/**
* Copyright (C) 2014-2019 Philip Helger and contributors
* philip[at]helger[dot]com
*
* 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.helger.phoss.smp.nicename;

import javax.annotation.Nonnull;
import javax.annotation.Nullable;
import javax.annotation.concurrent.Immutable;

import com.helger.html.hc.IHCNode;
import com.helger.html.hc.html.textlevel.HCCode;
import com.helger.html.hc.html.textlevel.HCWBR;
import com.helger.html.hc.impl.HCNodeList;
import com.helger.peppolid.IDocumentTypeIdentifier;
import com.helger.peppolid.IProcessIdentifier;
import com.helger.photon.bootstrap4.badge.BootstrapBadge;
import com.helger.photon.bootstrap4.badge.EBootstrapBadgeType;

/**
* Common UI for nice names
*
* @author Philip Helger
*/
@Immutable
public final class NiceNameUI
{
private NiceNameUI ()
{}

@Nonnull
private static IHCNode _getWBRList (@Nonnull final String s)
{
final HCNodeList ret = new HCNodeList ();
String sRest = s;
final int nChars = 10;
while (sRest.length () > nChars)
{
ret.addChild (sRest.substring (0, nChars)).addChild (new HCWBR ());
sRest = sRest.substring (nChars);
}
if (sRest.length () > 0)
ret.addChild (sRest);
return ret;
}

@Nonnull
private static IHCNode _createID (@Nonnull final String sID, @Nullable final NiceNameEntry aNiceName)
{
final HCNodeList ret = new HCNodeList ();
if (aNiceName == null)
{
// No nice name present
ret.addChild (new BootstrapBadge (EBootstrapBadgeType.WARNING).addChild ("Non-standard identifier"));
}
else
{
ret.addChild (new BootstrapBadge (EBootstrapBadgeType.SUCCESS).addChild (aNiceName.getName ()));
if (aNiceName.isDeprecated ())
{
ret.addChild (" ")
.addChild (new BootstrapBadge (EBootstrapBadgeType.WARNING).addChild ("Identifier is deprecated"));
}
}
ret.addChild (" ").addChild (new HCCode ().addChild (_getWBRList (sID)));
return ret;
}

@Nonnull
public static IHCNode getDocumentTypeID (@Nonnull final IDocumentTypeIdentifier aDocTypeID)
{
final String sURI = aDocTypeID.getURIEncoded ();
return _createID (sURI, NiceNameHandler.getDocTypeNiceName (sURI));
}

@Nonnull
public static IHCNode getProcessID (@Nonnull final IProcessIdentifier aProcessID)
{
final String sURI = aProcessID.getURIEncoded ();
return _createID (sURI, NiceNameHandler.getProcessNiceName (sURI));
}
}
@@ -50,8 +50,6 @@
import com.helger.html.hc.html.grouping.HCUL;
import com.helger.html.hc.html.tabular.HCCol;
import com.helger.html.hc.html.textlevel.HCCode;
import com.helger.html.hc.html.textlevel.HCSpan;
import com.helger.html.hc.html.textlevel.HCWBR;
import com.helger.html.hc.impl.HCNodeList;
import com.helger.html.hc.impl.HCTextNode;
import com.helger.html.jquery.JQuery;
@@ -61,11 +59,7 @@
import com.helger.html.jscode.JSPackage;
import com.helger.html.jscode.JSVar;
import com.helger.html.jscode.html.JSHtml;
import com.helger.peppolid.IDocumentTypeIdentifier;
import com.helger.peppolid.IProcessIdentifier;
import com.helger.peppolid.peppol.doctype.EPredefinedDocumentTypeIdentifier;
import com.helger.peppolid.peppol.doctype.IPeppolDocumentTypeIdentifierParts;
import com.helger.peppolid.peppol.process.EPredefinedProcessIdentifier;
import com.helger.phoss.smp.domain.extension.ISMPHasExtension;
import com.helger.phoss.smp.ui.ajax.AjaxExecutorPublicLogin;
import com.helger.phoss.smp.ui.ajax.CAjax;
@@ -255,58 +249,6 @@ public static BootstrapTable createCertificateDetailsTable (@Nonnull final X509C
return aCertDetails;
}

@Nonnull
private static IHCNode _getWBRList (@Nonnull final String s)
{
final HCSpan ret = new HCSpan ();
String sRest = s;
final int nChars = 30;
while (sRest.length () > nChars)
{
ret.addChild (sRest.substring (0, nChars)).addChild (new HCWBR ());
sRest = sRest.substring (nChars);
}
if (sRest.length () > 0)
ret.addChild (sRest);
return ret.getChildCount () == 1 ? ret.getFirstChild () : ret;
}

@Nonnull
public static IHCNode getDocumentTypeID (@Nonnull final IDocumentTypeIdentifier aDocTypeID)
{
EPredefinedDocumentTypeIdentifier ePredefined = null;
for (final EPredefinedDocumentTypeIdentifier e : EPredefinedDocumentTypeIdentifier.values ())
if (e.getAsDocumentTypeIdentifier ().hasSameContent (aDocTypeID))
{
ePredefined = e;
break;
}

if (ePredefined != null)
return new HCNodeList ().addChild (ePredefined.getCommonName () + " ")
.addChild (new BootstrapBadge (EBootstrapBadgeType.INFO).addChild ("predefined"));

return _getWBRList (aDocTypeID.getURIEncoded ());
}

@Nonnull
public static IHCNode getProcessID (@Nonnull final IProcessIdentifier aDocTypeID)
{
EPredefinedProcessIdentifier ePredefined = null;
for (final EPredefinedProcessIdentifier e : EPredefinedProcessIdentifier.values ())
if (e.getAsProcessIdentifier ().hasSameContent (aDocTypeID))
{
ePredefined = e;
break;
}

if (ePredefined != null)
return new HCNodeList ().addChild (ePredefined.getValue () + " ")
.addChild (new BootstrapBadge (EBootstrapBadgeType.INFO).addChild ("predefined"));

return _getWBRList (aDocTypeID.getURIEncoded ());
}

@Nonnull
public static HCUL getDocumentTypeIDDetails (@Nonnull final IPeppolDocumentTypeIdentifierParts aParts)
{

0 comments on commit 71b3446

Please sign in to comment.
You can’t perform that action at this time.