MediaSetNodeWriter.java 13.9 KB
/*
 * Decompiled with CFR 0_118.
 * 
 * Could not load the following classes:
 *  com.scene7.is.catalog.util.localization.LocaleMap
 *  com.scene7.is.catalog.util.localization.LocalizedText
 *  com.scene7.is.provider.ImageSetColorLabel
 *  com.scene7.is.provider.UserData
 *  com.scene7.is.provider.ZoomTargets
 *  com.scene7.is.sleng.Color
 *  com.scene7.is.sleng.HotSpot
 *  com.scene7.is.util.Factory
 *  com.scene7.is.util.SizeInt
 *  com.scene7.is.util.callbacks.Option
 *  com.scene7.is.util.text.coders.NetPathCoder
 *  org.jetbrains.annotations.NotNull
 *  org.jetbrains.annotations.Nullable
 */
package com.scene7.is.ps.provider.fvctx;

import com.scene7.is.catalog.util.localization.LocaleMap;
import com.scene7.is.catalog.util.localization.LocalizedText;
import com.scene7.is.provider.ImageSetColorLabel;
import com.scene7.is.provider.UserData;
import com.scene7.is.provider.ZoomTargets;
import com.scene7.is.ps.provider.IZoomException;
import com.scene7.is.ps.provider.fvctx.FvctxImageProps;
import com.scene7.is.ps.provider.fvctx.FvctxNode;
import com.scene7.is.ps.provider.fvctx.MediaSetItemType;
import com.scene7.is.ps.provider.fvctx.MediaSetMetaData;
import com.scene7.is.ps.provider.fvctx.MediaSetSetType;
import com.scene7.is.ps.provider.fvctx.MediaSetVideoExtension;
import com.scene7.is.ps.provider.fvctx.MediaSetVideoProps;
import com.scene7.is.sleng.Color;
import com.scene7.is.sleng.HotSpot;
import com.scene7.is.util.Factory;
import com.scene7.is.util.SizeInt;
import com.scene7.is.util.callbacks.Option;
import com.scene7.is.util.text.coders.NetPathCoder;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

public abstract class MediaSetNodeWriter<T>
implements FvctxNode,
Factory<T> {
    protected static final String E_FRAME = "item";
    protected static final String E_FRAMESET = "set";
    protected static final String E_MEDIA_SET = "set";
    protected static final String E_IMAGE = "i";
    protected static final String E_SWATCH = "s";
    protected static final String E_VIDEO = "v";
    protected static final String E_STATIC_CONTENT = "c";
    protected static final String E_ITEM = "item";
    protected static final String E_TARGETS = "targets";
    protected static final String E_HOT_SPOTS = "map";
    protected static final String E_USER_DATA = "userdata";
    protected static final String ATTR_WIDTH = "dx";
    protected static final String ATTR_HEIGHT = "dy";
    protected static final String ATTR_IMAGEVERSION = "iv";
    protected static final String ATTR_LABEL = "l";
    protected static final String ATTR_NAME = "n";
    protected static final String ATTR_PROTOCOLVERSION = "pv";
    protected static final String ATTR_SUFFIX = "suffix";
    protected static final String ATTR_MEDIA_SET_SET_TYPE = "type";
    protected static final String ATTR_MEDIA_SET_ITEM_TYPE = "type";
    protected static final String ATTR_IS_DEFAULT_IMAGE = "isDefault";
    protected static final String ATTR_LOCALE = "locale";
    protected static final String ATTR_MODIFIER = "mod";
    protected static final String ATTR_POST_MODIFIER = "pmod";
    protected static final String ATTR_SUPPRESSED_MAP = "map";
    protected static final String ATTR_SUPPRESSED_USERDATA = "userdata";
    protected static final String ATTR_ECAT_NUM_PAGES = "np";
    protected static final String ATTR_VIDEO_ID = "id";
    protected static final String ATTR_VIDEO_PATH = "path";
    protected static final String ATTR_VIDEO_BITRATE = "bitrate";
    protected static final String ATTR_VIDEO_FORMAT = "format";
    protected static final String ATTR_NAME_ECAT_COMPOSITE_TEMPLATE = "template";
    protected static final String ATTR_VALUE_ECAT_COMPOSITE_TEMPLATE = "$image0$?layer=0&extendN=0,0,1,0&originN=0,-0.5&layer=1&src=$image1$&originN=-0.5,-0.5&sizeN=0.5,1";
    protected static final double ECAT_PAGE_RATIO = 0.8;
    protected final int fvctxVersion;
    @NotNull
    protected final LocaleMap localeMap;
    @NotNull
    protected final String locale;
    @NotNull
    protected final String labelKey;
    @NotNull
    protected final MediaSetMetaData publicMetaData;
    @NotNull
    protected MediaSetSetType setType;
    @NotNull
    protected String netPath;
    private final int brochureLimit;
    private int brochureCount = 0;
    private boolean processingECat = false;

    protected MediaSetNodeWriter(int fvctxVersion, @NotNull LocaleMap localeMap, @NotNull String locale, @NotNull String labelKey, @NotNull MediaSetMetaData publicMetaData, @NotNull MediaSetSetType setType, @NotNull String netPath, int brochureLimit) {
        this.fvctxVersion = fvctxVersion;
        this.localeMap = localeMap;
        this.locale = locale;
        this.labelKey = labelKey;
        this.publicMetaData = publicMetaData;
        this.setType = setType;
        this.netPath = netPath;
        this.brochureLimit = brochureLimit;
    }

    abstract void postProcess() throws IZoomException;

    Map<String, String> getRootAttributes() {
        HashMap<String, String> map = new HashMap<String, String>();
        StringBuilder pv = new StringBuilder();
        pv.append(this.fvctxVersion);
        pv.append(".0");
        map.put("pv", pv.toString());
        map.put("type", this.setType.name().toLowerCase());
        if (this.setType == MediaSetSetType.ECAT || this.setType == MediaSetSetType.UNKNOWN) {
            this.processingECat = true;
            if (this.setType == MediaSetSetType.ECAT) {
                map.put("template", "$image0$?layer=0&extendN=0,0,1,0&originN=0,-0.5&layer=1&src=$image1$&originN=-0.5,-0.5&sizeN=0.5,1");
            }
        }
        map.put("n", this.netPath);
        if (this.locale.length() > 0) {
            map.put("locale", this.locale);
        }
        return map;
    }

    Map<String, String> getImageAttributes(@NotNull String netPath, @NotNull String query, @NotNull FvctxImageProps imageProps, boolean isMediaSetItem) {
        Map<String, String> map = this.getCommonImageAttributes(netPath, query, imageProps);
        if (imageProps.isDefaultImage()) {
            map.put("isDefault", "1");
        }
        if (isMediaSetItem) {
            this.processingECat = false;
        }
        return map;
    }

    Map<String, String> getCommonImageAttributes(@NotNull String netPath, @NotNull String query, @NotNull FvctxImageProps imageProps) {
        String postModifier;
        String modifier;
        HashMap<String, String> map = new HashMap<String, String>();
        StringBuilder name = new StringBuilder(NetPathCoder.netPathCoder().encode(netPath));
        if (!query.isEmpty()) {
            name.append('?').append(query);
        }
        map.put("n", name.toString());
        LocalizedText label = imageProps.getUserData().getFvctxLabel(this.labelKey);
        String labelAsString = label.get(this.localeMap, this.locale);
        if (!labelAsString.isEmpty()) {
            map.put("l", labelAsString);
        }
        if (!(modifier = imageProps.getModifier()).isEmpty()) {
            map.put("mod", modifier);
        }
        if (!(postModifier = imageProps.getPostModifier()).isEmpty()) {
            map.put("pmod", postModifier);
        }
        return map;
    }

    Map<String, String> getCommonVideoAttributes(@NotNull String netPath, @NotNull String query, @NotNull MediaSetVideoProps videoProps) {
        HashMap<String, String> map = new HashMap<String, String>();
        StringBuilder path = new StringBuilder(NetPathCoder.netPathCoder().encode(netPath));
        if (!query.isEmpty()) {
            path.append('?').append(query);
        }
        if (videoProps.isInCatalog()) {
            map.put("id", path.toString());
            map.put("path", NetPathCoder.netPathCoder().encode(MediaSetNodeWriter.stripDotSlash(videoProps.getFilePath())));
        } else {
            map.put("path", path.toString());
        }
        map.put("suffix", videoProps.getFileSuffix());
        if (videoProps.getBitRate().isDefined()) {
            map.put("bitrate", ((Long)videoProps.getBitRate().get()).toString());
        }
        if (videoProps.getVideoSize().isDefined()) {
            SizeInt videoSize = (SizeInt)videoProps.getVideoSize().get();
            map.put("dx", Integer.toString(videoSize.width));
            map.put("dy", Integer.toString(videoSize.height));
        }
        if (videoProps.getVideoFormat().isDefined()) {
            map.put("format", (String)videoProps.getVideoFormat().get());
        }
        this.processingECat = false;
        return map;
    }

    static Map<String, String> getVideoFrameAttributes(@NotNull String version, @NotNull String suffix) {
        HashMap<String, String> map = new HashMap<String, String>();
        MediaSetItemType itemType = MediaSetItemType.STATIC;
        if (MediaSetVideoExtension.isVideoFileExtension(suffix)) {
            itemType = MediaSetItemType.VIDEO;
        }
        map.put("type", itemType.name().toLowerCase());
        map.put("iv", version);
        return map;
    }

    Map<String, String> getFrameAttributes(SizeInt size, @NotNull String imageVersion, boolean hasNestedFrame, @NotNull String itemType) {
        HashMap<String, String> map = new HashMap<String, String>();
        map.put("iv", imageVersion);
        if (!hasNestedFrame) {
            map.put("dx", MediaSetNodeWriter.stringValueOf(size.width));
            map.put("dy", MediaSetNodeWriter.stringValueOf(size.height));
        }
        if (!itemType.equalsIgnoreCase("img")) {
            map.put("type", itemType);
        }
        return map;
    }

    static Map<String, String> getSuppressedMetaDataAttributes(@NotNull boolean[] metadataFlags) {
        HashMap<String, String> map = new HashMap<String, String>();
        if (metadataFlags[3]) {
            map.put("map", "1");
        }
        if (metadataFlags[4]) {
            map.put("userdata", "1");
        }
        return map;
    }

    Map<String, String> getStartItemAttributes(@NotNull MediaSetItemType itemType) {
        HashMap<String, String> map = new HashMap<String, String>();
        if (itemType != MediaSetItemType.IMG) {
            map.put("type", itemType.name().toLowerCase());
        }
        if (itemType == MediaSetItemType.ECAT || itemType == MediaSetItemType.UNKNOWN) {
            this.processingECat = true;
        }
        return map;
    }

    static Map<String, String> getEndItemAttributes(@NotNull String imageVersion) {
        HashMap<String, String> map = new HashMap<String, String>();
        map.put("iv", imageVersion);
        return map;
    }

    static Map<String, String> getStartSetAttributes(@NotNull String recordName, @NotNull MediaSetSetType setType) {
        HashMap<String, String> map = new HashMap<String, String>();
        map.put("n", recordName);
        map.put("type", setType.name().toLowerCase());
        if (setType == MediaSetSetType.ECAT) {
            map.put("template", "$image0$?layer=0&extendN=0,0,1,0&originN=0,-0.5&layer=1&src=$image1$&originN=-0.5,-0.5&sizeN=0.5,1");
        }
        return map;
    }

    static Map<String, String> getSwatchProps(@NotNull ImageSetColorLabel data, @NotNull LocaleMap localeMap, @NotNull String locale) {
        HashMap<String, String> map = new HashMap<String, String>();
        map.put("c", data.color.toHexStringNoAlpha());
        String swatchLabel = data.label.get(localeMap, locale);
        if (!swatchLabel.isEmpty()) {
            map.put("l", swatchLabel);
        }
        return map;
    }

    boolean[] shouldAppendMetadata(MediaSetMetaData metaData, boolean isPrivate) {
        boolean[] appendMetaData = new boolean[5];
        ZoomTargets targets = metaData.getTargets();
        appendMetaData[0] = !targets.isEmpty();
        List<HotSpot> maps = metaData.getMaps();
        appendMetaData[1] = !maps.isEmpty();
        boolean shouldAppendUserData = false;
        for (UserData userData : metaData.getUserData()) {
            if (userData.isEmpty(this.labelKey)) continue;
            shouldAppendUserData = true;
        }
        appendMetaData[2] = shouldAppendUserData;
        appendMetaData[3] = false;
        appendMetaData[4] = false;
        if (isPrivate && this.processingECat) {
            if (appendMetaData[1] || appendMetaData[2]) {
                ++this.brochureCount;
            }
            if (this.brochureCount > this.brochureLimit) {
                if (appendMetaData[1]) {
                    appendMetaData[1] = false;
                    appendMetaData[3] = true;
                }
                if (appendMetaData[2]) {
                    appendMetaData[2] = false;
                    appendMetaData[4] = true;
                }
            }
        }
        return appendMetaData;
    }

    boolean shouldAppendUserData(UserData userData) {
        return !userData.isEmpty(this.labelKey);
    }

    static boolean shouldSetPagesAttribute(@Nullable String aWidth, @Nullable String aHeight, @Nullable String bWidth, @Nullable String bHeight) {
        if (aWidth == null || aHeight == null || bWidth == null || bHeight == null) {
            return false;
        }
        try {
            int aWidthAsInt = Integer.parseInt(aWidth);
            int aHeightAsInt = Integer.parseInt(aHeight);
            int bWidthAsInt = Integer.parseInt(bWidth);
            int bHeightAsInt = Integer.parseInt(bHeight);
            double aspectRatio1 = (double)aWidthAsInt / (double)aHeightAsInt;
            double aspectRatio2 = (double)bWidthAsInt / (double)bHeightAsInt;
            return aspectRatio1 < aspectRatio2 * 0.8;
        }
        catch (NumberFormatException ignore) {
            return false;
        }
    }

    @NotNull
    private static String stringValueOf(int i) {
        return Integer.toString(i);
    }

    @NotNull
    private static String stripDotSlash(@NotNull String s) {
        if (s.isEmpty()) {
            return s;
        }
        if (s.length() == 2 && s.equals("./")) {
            return s;
        }
        if (s.length() >= 3 && s.startsWith("./")) {
            return s.substring(2);
        }
        return s;
    }
}