${this.getCurrentTabTitle()}
${this.getCurrentTabDescription()}
/** * EDC Management Component * * Main application shell for managing Eclipse Dataspace Connector resources. * This component uses the refactored service layer and provides a foundation * for future modular component architecture. * * @customElement edc-management */ import { html, LitElement, nothing } from "lit"; import { customElement, property, state } from "lit/decorators.js"; import { classMap } from "lit/directives/class-map.js"; import { repeat } from "lit/directives/repeat.js"; import { msg } from '@lit/localize'; import './language-selector.js'; // Import types and services import type { Asset, AssetInput, PolicyDefinition, TabId, Tab, ConnectionStatus, EdcStoreInterface } from '../types/edc-types'; import { createEdcStore } from '../services/store-factory'; // Import styles import { layoutStyles } from '../styles/layout-styles'; import { managementStyles } from '../styles/management-styles'; import { buttonStyles, formStyles } from '../styles/shared-styles'; import { responsiveStyles } from '../styles/responsive-styles'; @customElement("edc-management") export class EdcManagement extends LitElement { // ============================================================================ // Properties // ============================================================================ @property({ attribute: "api-endpoint" }) apiEndpoint?: string; @property({ attribute: "api-key" }) apiKey?: string; @property({ attribute: "participant-id" }) participantId?: string; // ============================================================================ // State // ============================================================================ @state() private activeTab: TabId = 'assets'; @state() private connectionStatus: ConnectionStatus = 'mock'; @state() private isLoading: boolean = false; // Assets state @state() private assets: Asset[] = []; @state() private isLoadingAssets: boolean = false; @state() private showCreateAssetForm: boolean = false; @state() private editingAsset: Asset | null = null; @state() private searchTerm: string = ''; @state() private sortBy: 'id' | 'title' | 'created' = 'id'; @state() private sortDirection: 'asc' | 'desc' = 'asc'; // Policy state @state() private policies: PolicyDefinition[] = []; @state() private isLoadingPolicies: boolean = false; @state() private showCreatePolicyForm: boolean = false; @state() private editingPolicy: PolicyDefinition | null = null; @state() private policySearchTerm: string = ''; @state() private policySortBy: 'id' | 'type' | 'created' = 'id'; @state() private policySortDirection: 'asc' | 'desc' = 'asc'; // Messages @state() private errorMessage: string = ''; @state() private successMessage: string = ''; // ============================================================================ // Private Properties // ============================================================================ private store!: EdcStoreInterface; private get tabs(): Tab[] { return [ { id: 'assets', label: msg('Assets', { id: 's2ea91b543c17d318' }), icon: '📦', description: msg('Manage data assets in the connector', { id: 's5b05c52215b75bd2' }) }, { id: 'policies', label: msg('Policies', { id: 's8cb7bb82e96d5d77' }), icon: '📋', description: msg('Define and manage access policies', { id: 'sace09165c81b1816' }) }, { id: 'contracts', label: msg('Contract Definitions', { id: 's3bc572e386a2b67d' }), icon: '📝', description: msg('Create and manage contract definitions', { id: 'sbc2baca40846dfdd' }) }, { id: 'negotiations', label: msg('Negotiations', { id: 'sbe2c9616bee67b33' }), icon: '🤝', description: msg('Monitor active contract negotiations', { id: 's2e547b157b4ccda1' }) } ]; } // ============================================================================ // Lifecycle Methods // ============================================================================ connectedCallback() { super.connectedCallback(); console.log('🚀 EDC Management Interface initializing...'); this.initializeStore(); // Listen for locale changes from language selector this.addEventListener('locale-changed', this.handleLocaleChange); } disconnectedCallback() { super.disconnectedCallback(); this.removeEventListener('locale-changed', this.handleLocaleChange); } // ============================================================================ // Store Initialization // ============================================================================ private initializeStore() { const { store, status } = createEdcStore({ apiEndpoint: this.apiEndpoint, apiKey: this.apiKey, participantId: this.participantId }); this.store = store; this.connectionStatus = status; console.log(`✅ EDC store initialized (status: ${status})`); // Load data for the active tab if (this.activeTab === 'assets') { setTimeout(() => this.loadAssets(), 100); } else if (this.activeTab === 'policies') { setTimeout(() => this.loadPolicies(), 100); } } // ============================================================================ // Event Handlers // ============================================================================ private handleLocaleChange = (event: CustomEvent) => { console.log('Locale changed to:', event.detail.locale); this.requestUpdate(); }; private switchTab(tabId: TabId) { console.log(`🔄 Switching to tab: ${tabId}`); this.activeTab = tabId; // Load data when switching to a tab if (tabId === 'assets' && this.assets.length === 0) { this.loadAssets(); } else if (tabId === 'policies' && this.policies.length === 0) { this.loadPolicies(); } } // ============================================================================ // Assets CRUD Operations // ============================================================================ private async loadAssets() { if (!this.store) { this.showError('Store not initialized.'); return; } this.isLoadingAssets = true; this.clearMessages(); try { console.log('🔍 Loading assets...'); const assets = await this.store.getAllAssets(); this.assets = Array.isArray(assets) ? assets : []; console.log(`✅ Loaded ${this.assets.length} assets`); } catch (error: any) { console.error('❌ Failed to load assets:', error); this.showError(`Failed to load assets: ${error.message}`); } finally { this.isLoadingAssets = false; } } private async createAsset(assetData: AssetInput) { if (!this.store) return false; try { console.log('🔨 Creating asset:', assetData); await this.store.createAsset(assetData); this.showSuccess(msg('Asset created successfully!', { id: 'sassetcreatedsuccessfully' })); this.showCreateAssetForm = false; await this.loadAssets(); return true; } catch (error: any) { this.showError(`Failed to create asset: ${error.message}`); return false; } } private async updateAsset(assetId: string, assetData: AssetInput) { if (!this.store) return false; try { console.log('✏️ Updating asset:', assetId, assetData); await this.store.updateAsset(assetId, assetData); this.showSuccess(msg('Asset updated successfully!', { id: 'sassetupdatedsuccessfully' })); this.editingAsset = null; await this.loadAssets(); return true; } catch (error: any) { this.showError(`Failed to update asset: ${error.message}`); return false; } } private async deleteAsset(assetId: string) { if (!this.store) return; if (!confirm(`Are you sure you want to delete asset '${assetId}'?`)) { return; } try { console.log('🗑️ Deleting asset:', assetId); await this.store.deleteAsset(assetId); this.showSuccess(msg('Asset deleted successfully!', { id: 'sassetdeletedsuccessfully' })); await this.loadAssets(); } catch (error: any) { this.showError(`Failed to delete asset: ${error.message}`); } } // ============================================================================ // Policies CRUD Operations // ============================================================================ private async loadPolicies() { if (!this.store) { this.showError('Store not initialized.'); return; } this.isLoadingPolicies = true; this.clearMessages(); try { console.log('🔍 Loading policies...'); const policies = await this.store.getAllPolicies(); this.policies = Array.isArray(policies) ? policies : []; console.log(`✅ Loaded ${this.policies.length} policies`); } catch (error: any) { console.error('❌ Failed to load policies:', error); this.showError(`Failed to load policies: ${error.message}`); } finally { this.isLoadingPolicies = false; } } private async createPolicy(policyData: PolicyDefinition) { if (!this.store) return false; try { console.log('🔨 Creating policy:', policyData); await this.store.createPolicy(policyData); this.showSuccess(msg('Policy created successfully!', { id: 'spolicycreatedsuccessfully' })); this.showCreatePolicyForm = false; await this.loadPolicies(); return true; } catch (error: any) { this.showError(`Failed to create policy: ${error.message}`); return false; } } private async updatePolicy(policyId: string, policyData: PolicyDefinition) { if (!this.store) return false; try { console.log(`✏️ Updating policy ${policyId}:`, policyData); await this.store.updatePolicy(policyId, policyData); this.showSuccess(msg('Policy updated successfully!', { id: 'spolicyupdatedsuccessfully' })); this.editingPolicy = null; await this.loadPolicies(); return true; } catch (error: any) { this.showError(`Failed to update policy: ${error.message}`); return false; } } private async deletePolicy(policyId: string) { if (!this.store) return; if (!confirm(`Are you sure you want to delete policy '${policyId}'? This action cannot be undone.`)) { return; } try { console.log(`🗑️ Deleting policy: ${policyId}`); await this.store.deletePolicy(policyId); this.showSuccess(msg('Policy deleted successfully!', { id: 'spolicydeletedsuccessfully' })); await this.loadPolicies(); } catch (error: any) { this.showError(`Failed to delete policy: ${error.message}`); } } // ============================================================================ // Utility Methods // ============================================================================ private showError(message: string) { this.errorMessage = message; this.successMessage = ''; setTimeout(() => { this.errorMessage = ''; }, 5000); } private showSuccess(message: string) { this.successMessage = message; this.errorMessage = ''; setTimeout(() => { this.successMessage = ''; }, 3000); } private clearMessages() { this.errorMessage = ''; this.successMessage = ''; } private get filteredAssets() { let filtered = [...this.assets]; if (this.searchTerm) { const term = this.searchTerm.toLowerCase(); filtered = filtered.filter(asset => { const id = asset['@id']?.toLowerCase() || ''; const title = asset.properties?.['dcterms:title']?.toLowerCase() || ''; const description = asset.properties?.['dcterms:description']?.toLowerCase() || ''; return id.includes(term) || title.includes(term) || description.includes(term); }); } // Sort filtered.sort((a, b) => { let aVal: string | number; let bVal: string | number; switch (this.sortBy) { case 'id': aVal = a['@id'] || ''; bVal = b['@id'] || ''; break; case 'title': aVal = a.properties?.['dcterms:title'] || a['@id'] || ''; bVal = b.properties?.['dcterms:title'] || b['@id'] || ''; break; case 'created': aVal = a.createdAt || 0; bVal = b.createdAt || 0; break; default: aVal = a['@id'] || ''; bVal = b['@id'] || ''; } if (typeof aVal === 'string' && typeof bVal === 'string') { aVal = aVal.toLowerCase(); bVal = bVal.toLowerCase(); } let result = 0; if (aVal < bVal) result = -1; else if (aVal > bVal) result = 1; return this.sortDirection === 'desc' ? -result : result; }); return filtered; } private get filteredPolicies() { let filtered = [...this.policies]; if (this.policySearchTerm) { const term = this.policySearchTerm.toLowerCase(); filtered = filtered.filter(policy => { const id = policy['@id']?.toLowerCase() || ''; const type = policy.policy['@type']?.toLowerCase() || ''; const policyId = policy.policy['@id']?.toLowerCase() || ''; return id.includes(term) || type.includes(term) || policyId.includes(term); }); } // Sort filtered.sort((a, b) => { let aVal: string | number; let bVal: string | number; switch (this.policySortBy) { case 'id': aVal = a['@id'] || ''; bVal = b['@id'] || ''; break; case 'type': aVal = a.policy['@type'] || ''; bVal = b.policy['@type'] || ''; break; case 'created': aVal = a.createdAt || 0; bVal = b.createdAt || 0; break; default: aVal = a['@id'] || ''; bVal = b['@id'] || ''; } if (typeof aVal === 'string' && typeof bVal === 'string') { aVal = aVal.toLowerCase(); bVal = bVal.toLowerCase(); } let result = 0; if (aVal < bVal) result = -1; else if (aVal > bVal) result = 1; return this.policySortDirection === 'desc' ? -result : result; }); return filtered; } private getCurrentTabTitle(): string { const currentTab = this.tabs.find(tab => tab.id === this.activeTab); return currentTab ? currentTab.label : 'EDC Management'; } private getCurrentTabDescription(): string { const currentTab = this.tabs.find(tab => tab.id === this.activeTab); return currentTab ? currentTab.description : 'Select a section from the sidebar'; } // ============================================================================ // Form Handlers // ============================================================================ private handleCreateAsset = async (e: Event) => { e.preventDefault(); const form = e.target as HTMLFormElement; const formData = new FormData(form); const assetData: AssetInput = { '@id': formData.get('assetId') as string, properties: { 'dcterms:title': formData.get('title') as string, 'dcterms:description': formData.get('description') as string } }; const dataAddressType = formData.get('dataAddressType') as string; const baseUrl = formData.get('baseUrl') as string; if (dataAddressType) { assetData.dataAddress = { '@type': 'DataAddress', type: dataAddressType }; if (baseUrl) { assetData.dataAddress.baseUrl = baseUrl; } } await this.createAsset(assetData); }; private handleEditAsset = async (e: Event) => { e.preventDefault(); if (!this.editingAsset) return; const form = e.target as HTMLFormElement; const formData = new FormData(form); const assetData: AssetInput = { '@id': this.editingAsset['@id'], properties: { 'dcterms:title': formData.get('title') as string, 'dcterms:description': formData.get('description') as string } }; const dataAddressType = formData.get('dataAddressType') as string; const baseUrl = formData.get('baseUrl') as string; if (dataAddressType) { assetData.dataAddress = { '@type': 'DataAddress', type: dataAddressType }; if (baseUrl) { assetData.dataAddress.baseUrl = baseUrl; } } await this.updateAsset(this.editingAsset['@id'], assetData); }; private handleCreatePolicy = async (e: Event) => { e.preventDefault(); const form = e.target as HTMLFormElement; const formData = new FormData(form); const policyData: PolicyDefinition = { '@type': 'PolicyDefinition', '@id': formData.get('policyId') as string, policy: { '@type': formData.get('policyType') as any, '@id': formData.get('innerPolicyId') as string || undefined, target: formData.get('target') as string || undefined, permission: [{ '@type': 'Permission', action: formData.get('permissionAction') as string, constraint: [] }] } }; await this.createPolicy(policyData); }; private handleEditPolicy = async (e: Event) => { e.preventDefault(); if (!this.editingPolicy) return; const form = e.target as HTMLFormElement; const formData = new FormData(form); const policyData: PolicyDefinition = { '@type': 'PolicyDefinition', '@id': this.editingPolicy['@id'], policy: { '@type': formData.get('policyType') as any, '@id': formData.get('innerPolicyId') as string || undefined, target: formData.get('target') as string || undefined, permission: [{ '@type': 'Permission', action: formData.get('permissionAction') as string, constraint: [] }] } }; await this.updatePolicy(this.editingPolicy['@id'], policyData); }; // ========================================================================== // Render Methods // // NOTE: For brevity, the render methods from edc-management-simple have been // extracted to a separate file. In the complete implementation, these would // be included here or imported from separate component modules (Phase 6.4-6.5) // ========================================================================== render() { return html`
${this.getCurrentTabDescription()}
${status}
${msg('Create your first asset to get started.', { id: 'screateyourfirstasset' })}
${msg('Create your first ODRL policy to get started.', { id: 'screateyourfirstodrlpolicy' })}